Package alluxio.util

Class ObjectSizeCalculator


  • public class ObjectSizeCalculator
    extends java.lang.Object
    Contains utility methods for calculating the memory usage of objects. It only works on the HotSpot JVM, and infers the actual memory layout (32 bit vs. 64 bit word size, compressed object pointers vs. uncompressed) from best available indicators. It can reliably detect a 32 bit vs. 64 bit JVM. It can only make an educated guess at whether compressed OOPs are used, though; specifically, it knows what the JVM's default choice of OOP compression would be based on HotSpot version and maximum heap sizes, but if the choice is explicitly overridden with the -XX:{+|-}UseCompressedOops command line switch, it can not detect this fact and will report incorrect sizes, as it will presume the default JVM behavior. Adapted from https://github.com/twitter-archive/commons/blob/master/src/java/com/twitter/common/objectsize/ObjectSizeCalculator.java
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long calculateObjectSize​(java.lang.Object obj)
      Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it.
      static long getObjectSize​(java.lang.Object obj)
      Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it.
      static long getObjectSize​(java.lang.Object obj, java.util.Set<java.lang.Class<?>> constantClasses)
      Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it.
      • Methods inherited from class java.lang.Object

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

      • ObjectSizeCalculator

        public ObjectSizeCalculator​(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification)
        Creates an object size calculator that can calculate object sizes for a given memoryLayoutSpecification.
        Parameters:
        memoryLayoutSpecification - a description of the JVM memory layout
      • ObjectSizeCalculator

        public ObjectSizeCalculator​(ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification,
                                    java.util.Set<java.lang.Class<?>> constSizeClass)
        Creates an object size calculator that can calculate object sizes for a given memoryLayoutSpecification.
        Parameters:
        memoryLayoutSpecification - a description of the JVM memory layout
        constSizeClass - constant sized classes
      • ObjectSizeCalculator

        public ObjectSizeCalculator​(ObjectSizeCalculator calc)
        Creates an object size calculator based on a previous one.
        Parameters:
        calc - previous calculator
    • Method Detail

      • getObjectSize

        public static long getObjectSize​(java.lang.Object obj)
                                  throws java.lang.UnsupportedOperationException
        Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it. Attempts to to detect the current JVM memory layout, but may fail with UnsupportedOperationException;
        Parameters:
        obj - the object; can be null. Passing in a Class object doesn't do anything special, it measures the size of all objects reachable through it (which will include its class loader, and by extension, all other Class objects loaded by the same loader, and all the parent class loaders). It doesn't provide the size of the static fields in the JVM class that the Class object represents.
        Returns:
        the total allocated size of the object and all other objects it retains
        Throws:
        java.lang.UnsupportedOperationException - if the current vm memory layout cannot be detected
      • getObjectSize

        public static long getObjectSize​(java.lang.Object obj,
                                         java.util.Set<java.lang.Class<?>> constantClasses)
                                  throws java.lang.UnsupportedOperationException
        Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it. Attempts to to detect the current JVM memory layout, but may fail with UnsupportedOperationException;
        Parameters:
        obj - the object; can be null
        constantClasses - a set of classes that are considered constant sized
        Returns:
        the total allocated size of the object and all other objects it
        Throws:
        java.lang.UnsupportedOperationException
      • calculateObjectSize

        public long calculateObjectSize​(java.lang.Object obj)
        Given an object, returns the total allocated size, in bytes, of the object and all other objects reachable from it.
        Parameters:
        obj - the object; can be null. Passing in a Class object doesn't do anything special, it measures the size of all objects reachable through it (which will include its class loader, and by extension, all other Class objects loaded by the same loader, and all the parent class loaders). It doesn't provide the size of the static fields in the JVM class that the Class object represents.
        Returns:
        the total allocated size of the object and all other objects it retains