0

I have googled but can not seem to find out how to determine the max heap the Vendor has set the Android java heap to. I know how to get the max heap and the max heap you should use for a app from this great thread: Android heap size on different phones/devices and OS versions

But when I run these two methods and compare the results to what Android Studio Memory Profiler gc's at it is half the value of what the ActivityManager getMemoryClass says. So I suspect the Vendor set it lower than that value. I have a LG G4 and getMemoryClass indicates 256MB but I see GC's occur around 128MB.

trincot
  • 317,000
  • 35
  • 244
  • 286
Tony Anecito
  • 347
  • 2
  • 13
  • Um, garbage collection does not wait until you reach the heap limit. After all, there are other apps on the device competing for the same memory. – CommonsWare Feb 11 '17 at 23:55
  • Thanks, I know for the Oracle JVM it had a formula that seemed to indicate 90% of max when it collected. I see a nice sawtooth waveform but it never goes beyond 128MB. My App does use a lot of images that should go beyond the 128MB when I stress if but I never see that happen. I will look closely though. – Tony Anecito Feb 12 '17 at 00:08
  • I ran a stress test and sure enough there seems to be a hard limit at 128MB. I am trying to adjust for memory usage based on available Heap instead of having something from a config file. If the Vendor is setting the limit not based on the two methods mentioned in the link above I would like to know programmatically what that is. – Tony Anecito Feb 12 '17 at 00:18
  • I am wondering if the max heap means the java plus the native? – Tony Anecito Feb 12 '17 at 02:38

2 Answers2

1

Did you try the android:largeHeap in your manifest. This flag indicates to the system that your app might need a bigger memory heap than that is allocated to a regular app. The system however takes the final call based on the overall memory size available on the device. Google doc says -

android:largeHeap

Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results. Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.

To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().

So for your purpose, since you a need a bigger heap, this will definitely help. Now this obviously does not answer the question, why getMemoryClass is returning 256 MB while garbage collection is happening at 128 MB (may be the system is keeping a too wide buffer). What I will suggest is you to print the return value of another method on ActivityManager which is getLargeMemoryClass. This along with turning largeHeap might provide some clues

Dibzmania
  • 1,934
  • 1
  • 15
  • 32
  • I tried android:largeHeap and it did not cause the memory limit to change. I heard it is only a request and does not have to be honored by the device. also did use getLargeMemoryClass and it returned 512MB or double getMemoryClass(). – Tony Anecito Feb 12 '17 at 02:03
0

Okay. I got the android:largeHeap="true" to work which is what I needed. Seems I had to put the following statements in my Main Activity class. ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE); am.getLargeMemoryClass();

Tony Anecito
  • 347
  • 2
  • 13