Can I get how much memory is left for my program?

My program has a lot of work requiring a lot of memory, which I canโ€™t know exactly when I need to stop it, but in case there is little memory left, I can make it stop using resources. Can I find out how much remaining (in bytes) memory my program can use?

P / s: There is no way to free up process memory. They need memory as much as possible, and this is how it works (and there is no rubbish for the collector, as the old ones are still needed).

+4
source share
4 answers

Try something like:

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() / 1000, memoryInfo.getTotalPrivateDirty() / 1000, memoryInfo.getTotalSharedDirty() / 1000); 

You can find out more on this blog: http://huenlil.pixnet.net/blog/post/26872625

+9
source

http://www.javaspecialists.eu/archive/Issue029.html http://www.exampledepot.com/egs/java.lang/GetHeapSize.html

+1
source
 public static long getCurrentFreeMemoryBytes() { long heapSize = Runtime.getRuntime().totalMemory(); long heapRemaining = Runtime.getRuntime().freeMemory(); long nativeUsage = Debug.getNativeHeapAllocatedSize(); return Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage; } 

Until he is perfect, he should do the trick for the most part.

+1
source

Check out the tools that Android provides for memory tracking here .

0
source

Source: https://habr.com/ru/post/893197/


All Articles