Monday, October 27, 2008

Check memory usage by your java program

It is always helpul for us to know how much memory is our program consuming for its execution. It helps us to improve our program performance and optimize programs.
In Java, we have ways to do this programatically.
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.

No comments: