SoFunction
Updated on 2025-04-10

Android obtains the maximum memory allocated by the current application and the current memory used

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

The test method is as follows:

Method 1:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
  //Maximum allocated memory  int memory = ();
  ("memory: "+memory);
  //Maximum allocated memory acquisition method 2  float maxMemory = (float) (().maxMemory() * 1.0/ (1024 * 1024));
  //The total memory currently allocated  float totalMemory = (float) (().totalMemory() * 1.0/ (1024 * 1024));
  //Remaining memory  float freeMemory = (float) (().freeMemory() * 1.0/ (1024 * 1024));
  ("maxMemory: "+maxMemory);
  ("totalMemory: "+totalMemory);
  ("freeMemory: "+freeMemory);

Method 2:

Runtime rt=();
long maxMemory=();
("maxMemory:",(maxMemory/(1024*1024)));
This can be obtained directlyappThe maximum availablememory sizeCalculated it isMB, What you get isheapgrowthlimit
1,maxMemory()The method acquisition system can beAPPMaximum memory allocated,
2,totalMemory() GetAPPThe currently allocated memoryheapSpace size。
First look at the memory limit of the machine,exist/system/In the file:
heapgrowthlimitIt's the memory limit of an ordinary application,use()This is the value obtained。
andheapsize是existmanifestSet inlargeHeap=true after,可以使use的最大内存值
The conclusion is,set uplargeHeapIt can indeed increase the amount of memory application。But it is not possible to apply for as much memory as the system has,and是由限制。
你可以existapp add largetHeap=true
You can apply for more memory ,But there is still a chance to explode.
<application
   .....
   android:label="XXXXXXXXXX"
   android:largeHeap="true">
  .......
</application>
cat /system/  //Read these valuesgetprop  //If there are no values ​​such as heapsize in it, you can use this to grab the default valuesetprop  256m //set up-----------------------   Some content ---------------------
=8m
=96m
=384m
=0.25
=8388608
=2097152
=OPTIONAL
=4.1_r6
=Android
-trace-file=/data/anr/

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below