Class Memory

java.lang.Object
com.complexible.common.base.Memory

public final class Memory extends 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 final int
     
    static final long
    The number of bytes in a gigabyte (2^30).
    static final long
    The number of bytes in a kilobyte (2^10).
    static final long
    The number of bytes in a kilobyte (2^20).
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    Returns the current amount of memory available on the heap.
    static String
    Detailed memory information logged only at TRACE level.
    static long
    Take a representation from readable(long) and turn it back into bytes.
    static String
     
    static long
    max()
    Returns the maximum amount of memory that the JVM will attempt to use.
    static String
    readable(long bytes)
    Returns a human-readable representation of bytes similar to how "ls -h" works in Unix systems.
    static long
    Returns the size of this object (with its references).
    static long
    Return the total memory available to the system, ie how much RAM the computer has.
    static long
    Returns the total memory available to JVM in bytes.
    static long
    Returns the used memory in bytes.
    static long
     
    static long
     
    static float
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • readable

      public static 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(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.
    • usedRatio

      public static float usedRatio()
    • usedHeap

      public static long usedHeap()
    • usedNonHeap

      public static long usedNonHeap()
    • heapUsage

      public static String heapUsage()
    • detailedUsage

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

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