Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. // Get current size of heap in bytes
  2. long heapSize = Runtime.getRuntime().totalMemory();
  3.  
  4. // Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
  5. long heapMaxSize = Runtime.getRuntime().maxMemory();
  6.  
  7. // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
  8. long heapFreeSize = Runtime.getRuntime().freeMemory();
  9.  
  10. System.out.println("heapsize"+formatSize(heapSize));
  11. System.out.println("heapmaxsize"+formatSize(heapMaxSize));
  12. System.out.println("heapFreesize"+formatSize(heapFreeSize));
  13.  
  14. public static String formatSize(long v) {
  15. if (v < 1024) return v + " B";
  16. int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
  17. return String.format("%.1f %sB", (double)v / (1L << (z*10)), " KMGTPE".charAt(z));
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement