Android Method for obtaining the maximum memory allocated by the current application and the memory currently used

  • 2021-11-02 02:28:41
  • OfStack

In Android, the program memory is divided into two parts: native and dalvik, and dalvik is the memory used by our ordinary Java when analyzing the stack. The objects we create are allocated here, and the memory limit is that native+dalvik cannot exceed the maximum limit. Android native system 1 defaults to 16M, but domestic mobile phones 1 are specially customized, and the memory size of the system is modified. Sometimes, to see the memory size allocated by specific application systems, it still needs to be actually tested.

The test method is as follows:

Mode 1:


ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
  // Maximum allocated memory 
  int memory = activityManager.getMemoryClass();
  System.out.println("memory: "+memory);
  // Acquisition method of maximum allocated memory 2
  float maxMemory = (float) (Runtime.getRuntime().maxMemory() * 1.0/ (1024 * 1024));
  // Total memory currently allocated 
  float totalMemory = (float) (Runtime.getRuntime().totalMemory() * 1.0/ (1024 * 1024));
  // Remaining memory 
  float freeMemory = (float) (Runtime.getRuntime().freeMemory() * 1.0/ (1024 * 1024));
  System.out.println("maxMemory: "+maxMemory);
  System.out.println("totalMemory: "+totalMemory);
  System.out.println("freeMemory: "+freeMemory);

Mode 2:


Runtime rt=Runtime.getRuntime();
long maxMemory=rt.maxMemory();
log.i("maxMemory:",Long.toString(maxMemory/(1024*1024)));
 Can be obtained directly app Maximum available memory size Calculate is MB,  What you get is heapgrowthlimit
1 , maxMemory() Method to get the system for APP Maximum memory allocated, 
2 , totalMemory()  Get APP Memory currently allocated heap Size of space. 
 Look at the memory limit of the machine first, in /system/build.prop In the file: 
heapgrowthlimit Is 1 Memory limits for common applications, using ActivityManager.getLargeMemoryClass() This is the value you get. 
 And heapsize Is in manifest Set in the largeHeap=true  Maximum memory value that can be used after 
 The conclusion is that setting largeHeap It is true that the amount of memory application can be increased. However, it is not that the system can apply for as much memory as it has, but that it is determined by dalvik.vm.heapsize Restrictions. 
 You can use the app manifest.xml Plus  largetHeap=true
 You can apply for more  , But it exploded .
<application
   .....
   android:label="XXXXXXXXXX"
   android:largeHeap="true">
  .......
</application>
cat /system/build.prop  // Read these values 
getprop dalvik.vm.heapsize // If build.prop There is no inside heapsize These values, you can use this to grab the default value 
setprop dalvik.vm.heapsize 256m // Settings 
-----------------------  build.prop  Partial content  ---------------------
dalvik.vm.heapstartsize=8m
dalvik.vm.heapgrowthlimit=96m
dalvik.vm.heapsize=384m
dalvik.vm.heaputilization=0.25
dalvik.vm.heapidealfree=8388608
dalvik.vm.heapconcurrentstart=2097152
ro.setupwizard.mode=OPTIONAL
ro.com.google.gmsversion=4.1_r6
net.bt.name=Android
dalvik.vm.stack-trace-file=/data/anr/traces.txt

Summarize


Related articles: