Skip to content Skip to sidebar Skip to footer

Can I Get How Much Memory For My Program Remains?

My program has many works that need a lot of memory that I can't exactly know when I need to stop it, but in case there's very few memory left, I can force it stop using resources.

Solution 1:

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 read more at this blog: http://huenlil.pixnet.net/blog/post/26872625

Solution 2:

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

Solution 3:

publicstaticlonggetCurrentFreeMemoryBytes() {
     long heapSize =  Runtime.getRuntime().totalMemory();
     long heapRemaining = Runtime.getRuntime().freeMemory();   
     long nativeUsage  =  Debug.getNativeHeapAllocatedSize();

     return Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;    
}

While not perfect it should do the trick for the most part.

Solution 4:

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

Post a Comment for "Can I Get How Much Memory For My Program Remains?"