Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to check how much memory is remaining?

Tags:

android

Below is my formula to check how much memory is remaining (not how much memory remains in the current heap, but how much more memory may be utilized before the application crashes). I'm not remotely sure this is correct, is it?

double max = Runtime.getRuntime().maxMemory(); //the maximum memory the app can use double heapSize = Runtime.getRuntime().totalMemory(); //current heap size double heapRemaining = Runtime.getRuntime().freeMemory(); //amount available in heap double nativeUsage = Debug.getNativeHeapAllocatedSize(); //is this right? I only want to account for native memory that my app is being "charged" for.  Is this the proper way to account for that?  //heapSize - heapRemaining = heapUsed + nativeUsage = totalUsage double remaining = max - (heapSize - heapRemaininng + nativeUsage);  
like image 679
ab11 Avatar asked May 20 '11 14:05

ab11


People also ask

How do I see how much Memory I have left on my phone?

By navigating to your Android device's Settings app and clicking on the Storage option, you'll be able to look at an at-a-glance view of your storage. Up top, you'll see how much of your phone's total storage you're using, followed by a breakdown of different categories that use up space on your phone.

How can I tell how much RAM my Android is using?

Tap Developer options and then tap Memory. In the resulting screen (Figure B), you'll see a list of the average memory used by the device in the past three hours (you can adjust the time frame, by tapping the time drop-down at the top). The Memory usage window in Android 12.

How much Memory do I have left?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.


2 Answers

Try the following code. that should give you the results you are after (especially the Pss field). You can read more about it here

Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memoryInfo);  String memMessage = String.format(     "Memory: Pss=%.2f MB, Private=%.2f MB, Shared=%.2f MB",     memoryInfo.getTotalPss() / 1024.0,     memoryInfo.getTotalPrivateDirty() / 1024.0,     memoryInfo.getTotalSharedDirty() / 1024.0); 
like image 167
Muzikant Avatar answered Oct 11 '22 03:10

Muzikant


This is an old thread, but this seems to work:

  Runtime.getRuntime().gc();   long memoryAvailable = (Runtime.getRuntime().maxMemory() -         Runtime.getRuntime().totalMemory() +         Runtime.getRuntime().freeMemory()); 

totalMemory-freeMemory is the current used memory.

So this boils down to:

(maximum possible memory)-(current used memory)

Edit to add: freeMemory does not count memory available for garbage collection, so you can try Runtime.getRuntime().gc(), although realize that can have its own consequences (like visual hiccups if run on the UI thread).

like image 31
Oded Avatar answered Oct 11 '22 04:10

Oded