0

below is logcat result which shows out of memory problem.

it may be involved with too big byte[] array.


04-17 22:21:38.773: W/dalvikvm(4963): threadid=1: thread exiting with uncaught exception (group=0x40a4d1f8) 04-17 22:21:38.781: E/AndroidRuntime(4963): FATAL EXCEPTION: main 04-17 22:21:38.781: E/AndroidRuntime(4963): java.lang.OutOfMemoryError 04-17 22:21:38.781: E/AndroidRuntime(4963): at java.nio.CharBuffer.put(CharBuffer.java:509) 04-17 22:21:38.781: E/AndroidRuntime(4963): at java.nio.charset.CharsetDecoder.allocateMore(CharsetDecoder.java:238) 04-17 22:21:38.781: E/AndroidRuntime(4963): at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:195) 04-17 22:21:38.781: E/AndroidRuntime(4963): at java.nio.charset.Charset.decode(Charset.java:487) 04-17 22:21:38.781: E/AndroidRuntime(4963): at java.lang.String.(String.java:174) 04-17 22:21:38.781: E/AndroidRuntime(4963): at java.lang.String.(String.java:141)

04-17 22:21:38.781: E/AndroidRuntime(4963): at exam.militarymgrs.Draw_LOS.readFile(Draw_LOS.java:182)

Draw_LOS.java:182 is as follows. (String s = new String(dat);)

-------Source code--------------

public static ArrayList<String> readFile(String fileName) {
//map data access
String sdPath;
sdPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

File file = new File(sdPath+fileName);
//String[] k = new String[1440000];
//String line = "";

ArrayList<String> data = new ArrayList<String>();//consider using ArrayList<int>
try {
FileInputStream fis = new FileInputStream(file);
byte[] dat = new byte[fis.available()];
String s = new String(dat);
String[] k = s.split("[\\r\\n]+");
while(fis.read(dat) != -1) {;}
fis.close();

}
catch(FileNotFoundException fN) {
fN.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
}
return data; 
}

A file which should be loaded is as follows

12 32 43 ...

How can I read the data file without Out of Memory with fileStream??

2
  • The solution is to start with at least 8,640,000 bytes of available heap. Or use the 3-argument version of String() (assuming UTF8 input) that would only require 4,320,000 bytes.
    – Hot Licks
    Apr 22, 2012 at 11:48
  • Jeez, you may wish to delete that code fragment, as it does not do anything usefull. Please visit the IO tutorials instead, and check out how to handle streams and readers. Dec 2, 2012 at 17:53

2 Answers 2

1

try to use this:

public static String toBinaryString(byte n) {
StringBuilder sb = new StringBuilder("00000000");
for (int bit = 0; bit < 8; bit++) {
if (((n >> bit) & 1) > 0) {
sb.setCharAt(7 - bit, '1');
}
}
return sb.toString();
}
0

Instead of juggling the bytes, you'd better try a different approach, leave string parsing to the string parser and just collect the output:

public static ArrayList<int> readFile(String fileName) {  // or ArrayList<short>
    String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

    Scanner s = new Scanner(new FileInputStream(new File(sdPath + fileName)));

    ArrayList<int> data = new ArrayList<int>();

    while( s.hasNext() ) {
        data.add( s.nextInt() );
    }

    return data; 
}
2
  • this code causes GC_CONCURRENT problem and does not work properly, thank you anyway Apr 26, 2012 at 10:53
  • GC_CONCURRENT is not a problem, it's the garbage collector taking care of your garbage. You may try to (temporary) replace data.add( s.nextInt() ); with s.nextInt(); to see if your "problem" is related to Scanner or ArrayList. If, after replacement, the "problem" goes away, you just need to choose another suitable container for your data. And, at least, you don't get Out of Memory exceptions, don't you?
    – lenik
    May 1, 2012 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.