Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I think I increased my heap size to 1 GB in NetBeans since I changed the config to look like this:

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

After I restarted NetBeans, can I be sure that my app is given 1 GB now?

Is there a way to verify this?

like image 603
mrblah Avatar asked Jan 06 '10 19:01

mrblah


People also ask

What is application heap size?

The heap size value is determined by the amount of memory available in the computer. Initial heap size is 1/64th of the computer's physical memory or reasonable minimum based on platform (whichever is larger) by default. The initial heap size can be overridden using -Xms.


2 Answers

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.

like image 140
Drewen Avatar answered Oct 21 '22 22:10

Drewen


public class CheckHeapSize {      public static void main(String[] args) {         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();                   System.out.println("heap size: " + formatSize(heapSize));         System.out.println("heap max size: " + formatSize(heapMaxSize));         System.out.println("heap free size: " + formatSize(heapFreeSize));              }     public static String formatSize(long v) {         if (v < 1024) return v + " B";         int z = (63 - Long.numberOfLeadingZeros(v)) / 10;         return String.format("%.1f %sB", (double)v / (1L << (z*10)), " KMGTPE".charAt(z));     } } 
like image 44
ram kumar Avatar answered Oct 21 '22 20:10

ram kumar