Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. So can I get how many remaining (in byte) memory that my program can use?

P/s: There's NO way to release the process memory. They need memory, as much as possible, and that is how it works (and, no trash for collector, since old ones still be need).

like image 400
AndBie Avatar asked Dec 22 '22 12:12

AndBie


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

like image 79
Kenny Avatar answered Dec 24 '22 01:12

Kenny


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

like image 28
shaunhusain Avatar answered Dec 24 '22 02:12

shaunhusain