views:

601

answers:

5

Well, the title says it.

It would also be handy to know how many memory is still available. I am writing a memory hungry application that tends to crash randomly (in native Code), and my suspicion is that it gets out-of-memory.

+3  A: 

I think you'd struggle to find a more comprehensive answer than this on the subject:
http://stackoverflow.com/questions/2298208/how-to-discovery-memory-usage-on-my-application-in-android/2299813#2299813

Christopher
A: 

You might want to catch the OutOfMemoryException and then call System.gc() to perform a manual garbage collection, then retry that piece of code that failed. You might be able to use native exceptions with JNI to detect when the C++ code fails due to lack of memory, or anticipate.

Chris Dennett
+1  A: 

Applications on Android generally don't crash due to low memory. If you are using a lot of memory, you may cause most all other applications to be killed. If you keep on using memory, you may cause the system to kill your app as well (not crashing it, just killing it), though you will probably get to the point of noticeable paging before that happens.

If you are dealing with native code, the more likely explanation is that you are corrupting memory somewhere.

hackbod
I'm currently seeing an issue in my app where the whole phone is crashing. Just before the phone reboots, I see messages about memory allocations failing. When there's no memory to allocate, whichever lucky app who tries to allocate next takes the hit, and if it's a core application, then it's lights out for the OS. I just wish there was a way to catch it before this happens.
Brad Hein
Applications shouldn't be allowed to crash the OS. It would be great if you could file bug on the bug tracker with a sample app and steps to reproduce the problem (and details about the device it is crashing, though best would be steps to crash the emulator).
hackbod
+2  A: 

i agree with hackbod's reply. As far as my understanding goes, your app wont crash , rather it will be killed. you may find this discussion interesting
http://stackoverflow.com/questions/2248995/is-there-a-need-to-check-for-null-after-allocating-memory-when-kernel-uses-over.
I guess there is some call back to know lowmemory conditions(onLowMemory()), you can use it to identify low memory conditions, I havent tried it though.

FL4SOF
A: 

Check http://kohlerm.blogspot.com/2010/02/android-memory-usage-analysis-slides.html to find out which java objects use the most memory

kohlerm