Out of memory error

Put your problem here if it does not fit any of the other categories.

Out of memory error

Postby Erykgecko » Mon Jul 27, 2009 11:38 pm


I'm trying to stresstest my app and make sure it works well without crashing. I'm running it on my g1 hooked up to my pc through eclipse and logcat is running. Now the app used to randomly crash, occasionally when trying to open the main menu, occasionally when you push a button to load a new page. It always gave me a java.lang.outofmemoryerror in logcat.

I assumed this was maybe because the string i'm loading and parsing was perhaps too long. 10,000 characters give or take a few. So I dropped this down to 7500 and seemed to get much better results in that it wouldn't just randomly crash when i least expect it. It now seems to crash if i exit and enter the app too much. Again it seems to give the same outofmemory error in logcat, which I'll post below.

my concern is that it's not releasing the memory resources it's using while running. From what I've gathered from various sources, this should be automatic, and it does require me to load the app then hit the home button like 7-8 times before it does crash now. but im wondering if perhaps my overall setup for the app is incorrect somewhere.

most of the magic is called from and takes place in the oncreate method, I'm just wondering if this is standard, or should some other method be called that does the main majority of the processing once the app has started... as obviously everytime i close the app then launch it again, it's doing the entire process from scratch, when the process is really already running from the last time i hit the home button...

any thoughts, or pointers in the right direction?

Logcat error below
07-27 23:15:51.511: INFO/ActivityManager(56): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.erykgecko/com.erykgecko.erykgecko} }
07-27 23:15:51.611: INFO/global(2726): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-27 23:15:51.771: DEBUG/dalvikvm(2726): GC freed 546 objects / 181440 bytes in 150ms
07-27 23:15:51.781: INFO/dalvikvm-heap(2726): Grow heap (frag case) to 13.199MB for 2469492-byte allocation
07-27 23:15:51.921: DEBUG/dalvikvm(2726): GC freed 110 objects / 148768 bytes in 141ms
07-27 23:15:52.061: DEBUG/dalvikvm(2726): GC freed 0 objects / 0 bytes in 138ms
07-27 23:15:52.061: INFO/dalvikvm-heap(2726): Forcing collection of SoftReferences for 1234754-byte allocation
07-27 23:15:52.191: DEBUG/dalvikvm(2726): GC freed 0 objects / 0 bytes in 130ms
07-27 23:15:52.191: ERROR/dalvikvm-heap(2726): Out of memory on a 1234754-byte allocation.
07-27 23:15:52.211: INFO/dalvikvm(2726): "main" prio=5 tid=3 RUNNABLE
07-27 23:15:52.211: INFO/dalvikvm(2726): | group="main" sCount=0 dsCount=0 s=0 obj=0x40018e70
07-27 23:15:52.211: INFO/dalvikvm(2726): | sysTid=2726 nice=0 sched=0/0 handle=-1096487780
07-27 23:15:52.271: INFO/dalvikvm(2726): at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:~45)
07-27 23:15:52.271: INFO/dalvikvm(2726): at java.nio.ReadWriteHeapByteBuffer.<init>(ReadWriteHeapByteBuffer.java:49)
07-27 23:15:52.302: INFO/dalvikvm(2726): at java.nio.BufferFactory.newByteBuffer(BufferFactory.java:51)
07-27 23:15:52.302: INFO/dalvikvm(2726): at java.nio.ByteBuffer.allocate(ByteBuffer.java:54)
07-27 23:15:52.302: INFO/dalvikvm(2726): at java.io.InputStreamReader.read(InputStreamReader.java:416)
07-27 23:15:52.302: INFO/dalvikvm(2726): at java.io.BufferedReader.read(BufferedReader.java:311)
07-27 23:15:52.311: INFO/dalvikvm(2726): at java.io.Reader.read(Reader.java:162)
07-27 23:15:52.311: INFO/dalvikvm(2726): at com.erykgecko.erykgecko.findpages(erykgecko.java:192)
07-27 23:15:52.311: INFO/dalvikvm(2726): at com.erykgecko.erykgecko.onCreate(erykgecko.java:82)
07-27 23:15:52.342: INFO/dalvikvm(2726): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
07-27 23:15:52.351: INFO/dalvikvm(2726): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
07-27 23:15:52.351: INFO/dalvikvm(2726): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
07-27 23:15:52.351: INFO/dalvikvm(2726): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
07-27 23:15:52.351: INFO/dalvikvm(2726): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
07-27 23:15:52.351: INFO/dalvikvm(2726): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 23:15:52.401: INFO/dalvikvm(2726): at android.os.Looper.loop(Looper.java:123)
07-27 23:15:52.401: INFO/dalvikvm(2726): at android.app.ActivityThread.main(ActivityThread.java:3948)
07-27 23:15:52.401: INFO/dalvikvm(2726): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 23:15:52.421: INFO/dalvikvm(2726): at java.lang.reflect.Method.invoke(Method.java:521)
07-27 23:15:52.471: INFO/dalvikvm(2726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-27 23:15:52.471: INFO/dalvikvm(2726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-27 23:15:52.471: INFO/dalvikvm(2726): at dalvik.system.NativeStart.main(Native Method)
07-27 23:15:52.701: DEBUG/AndroidRuntime(2726): Shutting down VM
07-27 23:15:52.711: WARN/dalvikvm(2726): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
07-27 23:15:52.711: ERROR/AndroidRuntime(2726): Uncaught handler: thread main exiting due to uncaught exception
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): java.lang.OutOfMemoryError
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:45)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.nio.ReadWriteHeapByteBuffer.<init>(ReadWriteHeapByteBuffer.java:49)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.nio.BufferFactory.newByteBuffer(BufferFactory.java:51)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.nio.ByteBuffer.allocate(ByteBuffer.java:54)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.io.InputStreamReader.read(InputStreamReader.java:416)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.io.BufferedReader.read(BufferedReader.java:311)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.io.Reader.read(Reader.java:162)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at com.erykgecko.erykgecko.findpages(erykgecko.java:192)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at com.erykgecko.erykgecko.onCreate(erykgecko.java:82)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.os.Looper.loop(Looper.java:123)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at android.app.ActivityThread.main(ActivityThread.java:3948)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at java.lang.reflect.Method.invoke(Method.java:521)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-27 23:15:52.751: ERROR/AndroidRuntime(2726): at dalvik.system.NativeStart.main(Native Method)
07-27 23:15:52.871: INFO/Process(56): Sending signal. PID: 2726 SIG: 3
07-27 23:15:52.871: INFO/dalvikvm(2726): threadid=7: reacting to signal 3
07-27 23:15:53.082: INFO/dalvikvm(2726): Wrote stack trace to '/data/anr/traces.txt'
Junior Developer
Junior Developer
Posts: 20
Joined: Sat May 16, 2009 5:32 pm


Did you fix this problem

Postby jasmeet » Sat Aug 08, 2009 1:15 am


I am facing a same problem of out of memory error.If you get any way to fix this problem.
Please reply

Jasmeet Singh
Posts: 2
Joined: Tue Jun 30, 2009 1:50 am

Postby Erykgecko » Sun Aug 09, 2009 12:56 am

I did fix it in the end,

I closed out the buffer once i was done with it, which seems to have fixed the problem...

Depending on what you are doing, I'd look into ways of manually releasing the resources once you;ve used them. Thats what fixed the issue for me :)
Junior Developer
Junior Developer
Posts: 20
Joined: Sat May 16, 2009 5:32 pm

Postby IHeartAndroid » Tue Oct 13, 2009 10:59 am


I am facing a similar problem. However closing the stream did not solve my problem:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                    Log.v(TAG, "Using cached file..");
  4.                                         File cacheDir = this.context.getCacheDir();
  5.                                         File cachedFile = new File(cacheDir, fi.getmFilePath());
  6.                                         FileInputStream resultInputStream = new FileInputStream(
  7.                                                         cachedFile);
  10.                                         // SET GUI WITH INPUTSTREAM
  11.                                         byte buf[] = new byte[1024];
  12.                                         while ((resultInputStream.read(buf)) > 0) {
  13.                                                 textViewText += new String(buf);
  14.                                         }
  16.                                         resultInputStream.close();
Parsed in 0.012 seconds, using GeSHi

When I repeat this code block the LogCat tells me something like:

INFO/dalvikvm-heap(4672): Grow heap (frag case) to 5.156MB for 815188-byte allocation

(whereas the sizes vary..)

After growing the heap for a while (which I don't understand why), the application crashes because the device (in that case the emulator) is out of memory.

Does anyone understand this ?
User avatar
Junior Developer
Junior Developer
Posts: 24
Joined: Wed Jul 08, 2009 8:51 am

Postby IHeartAndroid » Tue Oct 13, 2009 11:51 am

Problem solved:

textViewText += new String(buf);

This line appended the inputstream instead of replacing the content of the string, which is why the string object got really big....
User avatar
Junior Developer
Junior Developer
Posts: 24
Joined: Wed Jul 08, 2009 8:51 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests