Class Memory


  • public final class Memory
    extends java.lang.Object

    Utility class for memory related information.

    Since:
    2.0
    Version:
    3.1.1
    Author:
    Evren Sirin, Michael Grove
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_PAGE_SIZE  
      static long GB
      The number of bytes in a gigabyte (2^30).
      static long KB
      The number of bytes in a kilobyte (2^10).
      static long MB
      The number of bytes in a kilobyte (2^20).
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long available()
      Returns the current amount of memory available on the heap.
      static java.lang.String detailedUsage()
      Detailed memory information logged only at TRACE level.
      static long fromReadable​(java.lang.String theStr)
      Take a representation from readable(long) and turn it back into bytes.
      static void gc()
      Intended for Debugging Only Makes a best effort to run the Garbage Collector by calling the Runtime.gc() repeatedly and checking until the used memory reported decreases.
      static java.lang.String heapUsage()  
      static long max()
      Returns the maximum amount of memory that the JVM will attempt to use.
      static java.lang.String readable​(long bytes)
      Returns a human-readable representation of bytes similar to how "ls -h" works in Unix systems.
      static long sizeOf​(java.lang.Object obj)
      Returns the size of this object (with its references).
      static long system()
      Return the total memory available to the system, ie how much RAM the computer has.
      static long total()
      Returns the total memory available to JVM in bytes.
      static long used()
      Returns the used memory in bytes.
      static long usedHeap()  
      static long usedNonHeap()  
      static float usedRatio()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • readable

        public static java.lang.String readable​(long bytes)
        Returns a human-readable representation of bytes similar to how "ls -h" works in Unix systems. The resulting is guaranteed to be 4 characters or less unless the given value is greater than 999TB. The last character in the returned string is one of 'B', 'K', 'M', 'G', or 'T' representing bytes, kilobytes, megabytes, gigabytes or terabytes. This function uses the binary unit system where 1K = 1024B.

        Examples:

                 482 = 482B
                1245 = 1.2K
              126976 = 124K
             4089471 = 3.9M
            43316209 =  41M
          1987357695 = 1.9G
         
      • fromReadable

        public static long fromReadable​(java.lang.String theStr)
        Take a representation from readable(long) and turn it back into bytes. For example, if the input is "2K" the output would be 2048.
        Parameters:
        theStr - the byte string
        Returns:
        the number of bytes
      • used

        public static long used()
        Returns the used memory in bytes. The result is an estimate and based on when the GC runs this value might change significantly.
      • total

        public static long total()
        Returns the total memory available to JVM in bytes. This value may change over time.
      • max

        public static long max()
        Returns the maximum amount of memory that the JVM will attempt to use. It is possible that
      • available

        public static long available()
        Returns the current amount of memory available on the heap. Shorthand for: Memory.max() - Memory.used()
        Returns:
        available heap memory
      • system

        public static long system()
        Return the total memory available to the system, ie how much RAM the computer has.
        Returns:
        the system memory.
      • gc

        public static void gc()
        Intended for Debugging Only Makes a best effort to run the Garbage Collector by calling the Runtime.gc() repeatedly and checking until the used memory reported decreases.
      • usedRatio

        public static float usedRatio()
      • usedHeap

        public static long usedHeap()
      • usedNonHeap

        public static long usedNonHeap()
      • heapUsage

        public static java.lang.String heapUsage()
      • detailedUsage

        public static java.lang.String detailedUsage()
        Detailed memory information logged only at TRACE level.
      • sizeOf

        public static long sizeOf​(java.lang.Object obj)
        Returns the size of this object (with its references).