0

My program is hanging when it invokes readObject in FileInputStream.

I am not using a socket connection or anything like that, nor am I using an output stream (except for console output).

here is the code:


package com.syd;

import java.util.*;
import java.io.*;


public class PerfectCounter {

  private static final String path = "/storage/emulated/0/JavaNIDE/trap13counter/app/src/main/java/com/syd/sequences.data";

  public static void main(String[] args) {
    new PerfectCounter().run(System.out);
    System.out.println("TERMINATED");
    
  }
  
  public void run(PrintStream out){
    out.println(System.getProperty("user.dir"));
    try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(path))){
      out.println("reading...");
      HashMap<List<Integer>,Integer> sequences = (HashMap<List<Integer>, Integer>) in.readObject();
      out.println("read success");
      in.close();
    
    
      //TODO
    
    }catch(Exception e){
      out.printf("err> %s: %s%n", e, e.getMessage());
      e.printStackTrace(out);
    }
  }

}

output:

/
reading...

The file sequences.data is 90MB and I am running this on my android phone (as I do not have a computer).

Searching for answers proved very difficult as only socket connections and the like appeared.

Any help is appreciated. Thanks,

MrOtto
  • 25
  • 1
  • 8
  • 3
    Reading 90MB on an Android phone may take a while. How long did you wait? – Fildor Dec 15 '20 at 08:44
  • i waited about 5mins – MrOtto Dec 15 '20 at 08:45
  • 3
    Is it still running? If so, grab a coffee, walk the dog ... see if it's finished when you return. – Fildor Dec 15 '20 at 08:47
  • well i have to terminate it to switch app......... and will also be running this read many times..... i will make a sequences-header.data file which will be a fraction of the size and use that for testing i suppose. – MrOtto Dec 15 '20 at 08:50
  • Try a new BufferedInputStream on the FileInputStream. Also consider storing the data not with an ObjectOutputStream as that has an overhead; (key valueCount value*)*. – Joop Eggen Dec 15 '20 at 08:55
  • Also you can track how the progress goes by wrapping the `FileInputStream` into a progress-tracking decorator, say something like https://stackoverflow.com/questions/1339437/inputstream-or-reader-wrapper-for-progress-reporting -- or fuse this approach directly to the `ObjectInputStream` to track how and when each object is read. – terrorrussia-keeps-killing Dec 15 '20 at 08:56
  • success: it was indeed the file size. thanks for all the comments. i will be more patient next time. – MrOtto Dec 15 '20 at 08:59
  • 1
    I don't think reading that large of a file in android is a design friendly decision. Alongside, android is known to kill apps in case of memory crunch. – Ashish Gupta Dec 15 '20 at 09:09
  • ultimately it will be run on google cloud compute engine. local for testing only (as the full run takes over 24hrs on my phone, less than 4 on the cloud). – MrOtto Dec 15 '20 at 09:21
  • also have changed it to byte arrays due to other memory issues. – MrOtto Dec 15 '20 at 18:24

0 Answers0