Class RamUsageEstimator


  • public class RamUsageEstimator
    extends java.lang.Object
    Helper class that helps estimate memory usage Mostly forked from Lucene tag releases/lucene-solr/8.5.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean COMPRESSED_REFS_ENABLED
      True, iff compressed references (oops) are enabled by this JVM
      (package private) static java.lang.String HOTSPOT_BEAN_CLASS  
      (package private) static boolean JRE_IS_64BIT  
      (package private) static java.lang.String MANAGEMENT_FACTORY_CLASS  
      static int NUM_BYTES_ARRAY_HEADER
      Number of bytes to represent an array header (no content, but with alignments).
      static int NUM_BYTES_OBJECT_ALIGNMENT
      A constant specifying the object alignment boundary inside the JVM.
      static int NUM_BYTES_OBJECT_HEADER
      Number of bytes to represent an object header (no fields, no alignments).
      static int NUM_BYTES_OBJECT_REF
      Number of bytes this JVM uses to represent an object reference.
      static long ONE_GB
      One gigabyte bytes.
      static long ONE_KB
      One kilobyte bytes.
      static long ONE_MB
      One megabyte bytes.
      (package private) static java.lang.String OS_ARCH  
      static java.util.Map<java.lang.Class<?>,​java.lang.Integer> primitiveSizes
      Sizes of primitive classes.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private RamUsageEstimator()
      No instantiation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static long adjustForField​(long sizeSoFar, java.lang.reflect.Field f)
      This method returns the maximum representation size of an object.
      static long alignObjectSize​(long size)
      Aligns an object size to be the next multiple of NUM_BYTES_OBJECT_ALIGNMENT.
      static long shallowSizeOf​(java.lang.Object obj)
      Estimates a "shallow" memory usage of the given object.
      private static long shallowSizeOfArray​(java.lang.Object array)
      Return shallow size of any array.
      static long shallowSizeOfInstance​(java.lang.Class<?> clazz)
      Returns the shallow instance size in bytes an instance of the given class would occupy.
      static long shallowUsedSizeOfArray​(java.lang.Object array, int usedSize)
      Return used part of shallow size of any array.
      • Methods inherited from class java.lang.Object

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

      • COMPRESSED_REFS_ENABLED

        public static final boolean COMPRESSED_REFS_ENABLED
        True, iff compressed references (oops) are enabled by this JVM
      • 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.
      • primitiveSizes

        public static final java.util.Map<java.lang.Class<?>,​java.lang.Integer> primitiveSizes
        Sizes of primitive classes.
      • JRE_IS_64BIT

        static final boolean JRE_IS_64BIT
      • MANAGEMENT_FACTORY_CLASS

        static final java.lang.String MANAGEMENT_FACTORY_CLASS
        See Also:
        Constant Field Values
      • OS_ARCH

        static final java.lang.String OS_ARCH
    • Constructor Detail

      • RamUsageEstimator

        private RamUsageEstimator()
        No instantiation.
    • Method Detail

      • alignObjectSize

        public static long alignObjectSize​(long size)
        Aligns an object size to be the next multiple of NUM_BYTES_OBJECT_ALIGNMENT.
      • 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)
      • 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.
      • shallowUsedSizeOfArray

        public static long shallowUsedSizeOfArray​(java.lang.Object array,
                                                  int usedSize)
        Return used part of shallow size of any array.
        Parameters:
        usedSize - Size that array is actually used
      • shallowSizeOfArray

        private static long shallowSizeOfArray​(java.lang.Object array)
        Return shallow size of any array.
      • adjustForField

        static long adjustForField​(long sizeSoFar,
                                   java.lang.reflect.Field f)
        This method returns the maximum representation size of an object. sizeSoFar is the object's size measured so far. f is the field being probed.

        The returned offset will be the maximum of whatever was measured so far and f field's offset and representation size (unaligned).