UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 77984

Is there any event or way exists in android to check if memory is low?

friends,

any one guide me if there is any event of useful way exits in android to know if my android application is running in low memory?

actually , i am caching many objects so i want to know a central place or area where i could release them if memory is low.

any help would be appreciated.

Upvotes: 1

Views: 10007

Answers (3)

bigstones
bigstones

Reputation: 15267

yes, see onLowMemory(), Activity and others implement this.

edit: this is actually wrong, application-wise. See Nik Reiman's answer.

Upvotes: 2

user3117987
user3117987

Reputation:

OnLowMemory() only gets called when the system is running out of memory so with that you can implement following methods to check available memory before you decode the images.

Runtime.getRuntime().maxMemory();
Runtime.getRuntime().totalMemory();
Runtime.getRuntime().freeMemory();

Upvotes: 1

Nik Reiman
Nik Reiman

Reputation: 40390

Contrary to what @bigstones says, onLowMemory() doesn't exactly do what you would expect. My understanding of this call is that it is only reached when the system is low on memory and asks your application to free extra memory. onLowMemory() will never be called when the system has plenty of memory, but your app has reached its memory limit.

It's also a bad idea to try/catch the OutOfMemory exceptions. If you want to know your own app's memory usage, the best way to get it is via the ActivityManager class, namely in the getMemoryClass() and getProcessMemoryInfo() methods.

Also, you should read this SO answer (from an Android engineer), which explains process memory use in a much more detailed way.

Upvotes: 15

Related Questions