-1

I am new to android and my app sometimes resulted in an outOfMemoryException. Therefore I have to check how much ram my app is using and try to reduce it.

Two questions:

  1. How to know the ram size that my app is using?
  2. Normally, what is the safe threshold for an app's ram size so that it would not cause an outOfMemoryException ?

Thanks a lot for your help!

Einsambr
  • 189
  • 2
  • 11
  • visit this here is good explanation :http://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android/9428660#9428660 – Harshad Pansuriya Sep 13 '16 at 05:01
  • when are you getting `OutOfMemory Exception`? are you getting this `Exception` when you trying to load Images on `ImageView`? – Learn Pain Less Sep 13 '16 at 05:01
  • Yes, but strangely, I seldom get `OutOfMemory Exception` when running on the emulator while ALWAYS gets `OutOfMemory Exception` when running on the phone. – Einsambr Sep 13 '16 at 05:10
  • in your app are you loading multiple images on ImageView? or loading big images on ImageView? – Learn Pain Less Sep 13 '16 at 05:11
  • I am loading multiple (about 100 ) small images on ImageView, and I was visiting [this page](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966) trying to solve the problem. – Einsambr Sep 13 '16 at 05:16
  • yes thats the reason. loading images cause OutOFMemory, its not about RAM. – Learn Pain Less Sep 13 '16 at 05:17
  • you can use inSampleSize – Learn Pain Less Sep 13 '16 at 05:17

3 Answers3

1

Check out Android Studio's in-built memory monitor. that you will find under Android Monitor > Monitors

and for the second part of your problem you can use ActivtyManager class' getMemoryClass () method. from the android doc..

Return the approximate per-application memory class of the current device. This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best. The returned value is in megabytes; the baseline Android memory class is 16 (which happens to be the Java heap limit of those devices); some device with more memory may return 24 or even higher numbers.

Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
0

Check that the image size is smaller than the available memory before attempting to load it. So the most efficient way to handle OutOfMemoryException is to architecture your application in such a way that it never attempts to load lots of data into memory in order to avoid the exception.

Before loading images into memory compress your images using

Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

Log.e("Original   dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());  
Sangram Haladkar
  • 707
  • 9
  • 22
0

1.How to know the ram size that my app is using? This question quite hard to answer. Different type of apps requires different memory.

2.Normally, what is the safe threshold for an app's ram size so that it would not cause an outOfMemoryException? Different devices have different threshold. You can normally use

Runtime rt=Runtime.getRuntime();
long maxMemory=rt.maxMemory();
Log.i("maxMemory:",Long.toString(maxMemory/(1024*1024)));

to see a size of maxMemory you can use. But this is not exactly.

Oscar Zhang
  • 153
  • 2
  • 7