I have a problem that I am facing OuOfMemory Error while printing a string after encoding bytes into String through Base64 encoder. Here are the following Code related details:
Error Stack:
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): FATAL EXCEPTION: main
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): java.lang.OutOfMemoryError
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:89)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at java.lang.StringBuffer.<init>(StringBuffer.java:83)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at com.example.TestCryptoActivity.onCreate(TestCryptoActivity.java:54)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.os.Looper.loop(Looper.java:123)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at java.lang.reflect.Method.invoke(Method.java:521)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-05 12:25:30.995: ERROR/AndroidRuntime(2660): at dalvik.system.NativeStart.main(Native Method)
Code(The Last line of the code is responsible for the error):
File file = new File("/sdcard/E0022505.mp4");
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
byte[] fileData = new byte[2097152];
int read = 0;
while(read != fileData.length) {
try {
read += is.read(fileData, read, fileData.length - read);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(fileData,0,read);
bytes = bos.toByteArray();
StringBuffer strNew = new StringBuffer(Base64.encodeToString(fileData, 0));
System.out.println("The bytes array:"+strNew);---> This line makes error
Removing those three lines that don’t do anything useful would at least avoid at least 2097152 bytes in memory :
For the rest, this code causes the exception to happen, but it’s maybe because some other part of the code, executed before is too memory-hungry.
Try using a profiler to detect where the memory is used.
But storing a whole large byte array, along with its copy encoded in base64, in memory in an android program is of course a bad idea.