Class RamUsageEstimator

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long alignObjectSize​(long size)
      Aligns an object size to be the next multiple of NUM_BYTES_OBJECT_ALIGNMENT.
      static java.util.EnumSet<RamUsageEstimator.JvmFeature> getSupportedFeatures()
      Return the set of supported JVM features that improve the estimation.
      static java.util.EnumSet<RamUsageEstimator.JvmFeature> getUnsupportedFeatures()
      Return the set of unsupported JVM features that improve the estimation.
      static java.lang.String humanReadableUnits​(long bytes)
      Returns size in human-readable units (GB, MB, KB or bytes).
      static java.lang.String humanReadableUnits​(long bytes, java.text.DecimalFormat df)
      Returns size in human-readable units (GB, MB, KB or bytes).
      static java.lang.String humanSizeOf​(java.lang.Object object)
      Return a human-readable size of a given object.
      static boolean isSupportedJVM()
      Returns true, if the current JVM is fully supported by RamUsageEstimator.
      static long shallowSizeOf​(java.lang.Object obj)
      Estimates a "shallow" memory usage of the given object.
      static long shallowSizeOfInstance​(java.lang.Class<?> clazz)
      Returns the shallow instance size in bytes an instance of the given class would occupy.
      static long sizeOf​(boolean[] arr)
      Returns the size in bytes of the boolean[] object.
      static long sizeOf​(byte[] arr)
      Returns the size in bytes of the byte[] object.
      static long sizeOf​(char[] arr)
      Returns the size in bytes of the char[] object.
      static long sizeOf​(double[] arr)
      Returns the size in bytes of the double[] object.
      static long sizeOf​(float[] arr)
      Returns the size in bytes of the float[] object.
      static long sizeOf​(int[] arr)
      Returns the size in bytes of the int[] object.
      static long sizeOf​(long[] arr)
      Returns the size in bytes of the long[] object.
      static long sizeOf​(short[] arr)
      Returns the size in bytes of the short[] object.
      static long sizeOf​(java.lang.Object obj)
      Estimates the RAM usage by the given object.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • JVM_INFO_STRING

        public static final java.lang.String JVM_INFO_STRING
        JVM info string for debugging and reports.
      • NUM_BYTES_OBJECT_REF

        public static final int NUM_BYTES_OBJECT_REF
        Number of bytes this jvm uses to represent an object reference.
      • NUM_BYTES_OBJECT_HEADER

        public static final int NUM_BYTES_OBJECT_HEADER
        Number of bytes to represent an object header (no fields, no alignments).
      • NUM_BYTES_ARRAY_HEADER

        public static final int NUM_BYTES_ARRAY_HEADER
        Number of bytes to represent an array header (no content, but with alignments).
      • NUM_BYTES_OBJECT_ALIGNMENT

        public static final int NUM_BYTES_OBJECT_ALIGNMENT
        A constant specifying the object alignment boundary inside the JVM. Objects will always take a full multiple of this constant, possibly wasting some space.
    • Method Detail

      • isSupportedJVM

        public static boolean isSupportedJVM()
        Returns true, if the current JVM is fully supported by RamUsageEstimator. If this method returns false you are maybe using a 3rd party Java VM that is not supporting Oracle/Sun private APIs. The memory estimates can be imprecise then (no way of detecting compressed references, alignments, etc.). Lucene still tries to use sensible defaults.
      • alignObjectSize

        public static long alignObjectSize​(long size)
        Aligns an object size to be the next multiple of NUM_BYTES_OBJECT_ALIGNMENT.
      • sizeOf

        public static long sizeOf​(byte[] arr)
        Returns the size in bytes of the byte[] object.
      • sizeOf

        public static long sizeOf​(boolean[] arr)
        Returns the size in bytes of the boolean[] object.
      • sizeOf

        public static long sizeOf​(char[] arr)
        Returns the size in bytes of the char[] object.
      • sizeOf

        public static long sizeOf​(short[] arr)
        Returns the size in bytes of the short[] object.
      • sizeOf

        public static long sizeOf​(int[] arr)
        Returns the size in bytes of the int[] object.
      • sizeOf

        public static long sizeOf​(float[] arr)
        Returns the size in bytes of the float[] object.
      • sizeOf

        public static long sizeOf​(long[] arr)
        Returns the size in bytes of the long[] object.
      • sizeOf

        public static long sizeOf​(double[] arr)
        Returns the size in bytes of the double[] object.
      • sizeOf

        public static long sizeOf​(java.lang.Object obj)
        Estimates the RAM usage by the given object. It will walk the object tree and sum up all referenced objects.

        Resource Usage: This method internally uses a set of every object seen during traversals so it does allocate memory (it isn't side-effect free). After the method exits, this memory should be GCed.

      • shallowSizeOf

        public static long shallowSizeOf​(java.lang.Object obj)
        Estimates a "shallow" memory usage of the given object. For arrays, this will be the memory taken by array storage (no subreferences will be followed). For objects, this will be the memory taken by the fields. JVM object alignments are also applied.
      • shallowSizeOfInstance

        public static long shallowSizeOfInstance​(java.lang.Class<?> clazz)
        Returns the shallow instance size in bytes an instance of the given class would occupy. This works with all conventional classes and primitive types, but not with arrays (the size then depends on the number of elements and varies from object to object).
        Throws:
        java.lang.IllegalArgumentException - if clazz is an array class.
        See Also:
        shallowSizeOf(Object)
      • getUnsupportedFeatures

        public static java.util.EnumSet<RamUsageEstimator.JvmFeature> getUnsupportedFeatures()
        Return the set of unsupported JVM features that improve the estimation.
      • getSupportedFeatures

        public static java.util.EnumSet<RamUsageEstimator.JvmFeature> getSupportedFeatures()
        Return the set of supported JVM features that improve the estimation.
      • humanReadableUnits

        public static java.lang.String humanReadableUnits​(long bytes)
        Returns size in human-readable units (GB, MB, KB or bytes).
      • humanReadableUnits

        public static java.lang.String humanReadableUnits​(long bytes,
                                                          java.text.DecimalFormat df)
        Returns size in human-readable units (GB, MB, KB or bytes).