During execution, how can a java program tell how much memory it is using?

Ron Tuffin picture Ron Tuffin · Oct 27, 2008 · Viewed 19.7k times · Source

During execution, how can a java program tell how much memory it is using?

I don't care how efficient it is!

Answer

Jon Skeet picture Jon Skeet · Oct 27, 2008

VonC's answer is an interactive solution - if you want to know programatically, you can use Runtime.totalMemory() to find out the total amount used by the JVM, and Runtime.freeMemory() to find out how much of that is still available (i.e. it's allocated to the JVM, but not allocated within the JVM - new objects can use this memory).

These are instance methods - use Runtime.getRuntime() to first get the singleton instance.