mrblah mrblah - 7 months ago 46
Java Question

How to view the current heap size that an application is using?

so I think I increase my heap size to 1 GB in netbeans, I changed the config to look like:

netbeans_default_options="-J-Xmx1g ......

I closed and started up netbeans again, can I be sure that my app is given 1GB now? or is there a way to verify this?


Use this code:

// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory(); 

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();

 // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory(); 

It was useful to me to know it.