Class ObjectSizes


  • public class ObjectSizes
    extends java.lang.Object
    A convenience class for wrapping access to MemoryMeter. Should be used instead of using a MemoryMeter directly. MemoryMeter can be used directly for testing as it allow a more fine tuned configuration for comparison.
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectSizes()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long measure​(java.lang.Object pojo)  
      static long measureDeep​(java.lang.Object pojo)  
      static long measureDeepOmitShared​(java.lang.Object pojo)  
      static long sizeOf​(java.lang.String str)
      Memory a String consumes
      static long sizeOfArray​(byte[] bytes)
      Memory a byte array consumes
      static long sizeOfArray​(int[] ints)
      Memory an int array consumes
      static long sizeOfArray​(long[] longs)
      Memory a long array consumes
      static long sizeOfArray​(java.lang.Object[] objects)
      Memory a reference array consumes itself only
      static long sizeOfReferenceArray​(int length)
      Memory a reference array consumes
      static long sizeOnHeapExcludingDataOf​(java.nio.ByteBuffer buffer)
      Measures the heap memory used by the specified byte buffer excluding the data.
      static long sizeOnHeapExcludingDataOf​(java.nio.ByteBuffer[] array)
      Amount of non-data heap memory consumed by the array of byte buffers.
      static long sizeOnHeapOf​(java.nio.ByteBuffer buffer)
      Measures the heap memory used by the specified byte buffer.
      static long sizeOnHeapOf​(java.nio.ByteBuffer[] array)
      Amount of heap memory consumed by the array of byte buffers.
      • Methods inherited from class java.lang.Object

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

      • IPV6_SOCKET_ADDRESS_SIZE

        public static final long IPV6_SOCKET_ADDRESS_SIZE
    • Constructor Detail

      • ObjectSizes

        public ObjectSizes()
    • Method Detail

      • sizeOfArray

        public static long sizeOfArray​(byte[] bytes)
        Memory a byte array consumes
        Parameters:
        bytes - byte array to get memory size
        Returns:
        heap-size of the array
      • sizeOfArray

        public static long sizeOfArray​(long[] longs)
        Memory a long array consumes
        Parameters:
        longs - byte array to get memory size
        Returns:
        heap-size of the array
      • sizeOfArray

        public static long sizeOfArray​(int[] ints)
        Memory an int array consumes
        Parameters:
        ints - byte array to get memory size
        Returns:
        heap-size of the array
      • sizeOfReferenceArray

        public static long sizeOfReferenceArray​(int length)
        Memory a reference array consumes
        Parameters:
        length - the length of the reference array
        Returns:
        heap-size of the array
      • sizeOfArray

        public static long sizeOfArray​(java.lang.Object[] objects)
        Memory a reference array consumes itself only
        Parameters:
        objects - the array to size
        Returns:
        heap-size of the array (excluding memory retained by referenced objects)
      • sizeOnHeapOf

        public static long sizeOnHeapOf​(java.nio.ByteBuffer[] array)
        Amount of heap memory consumed by the array of byte buffers. It sums memory consumed by the array itself and for each included byte buffer using sizeOnHeapOf(ByteBuffer).
      • sizeOnHeapExcludingDataOf

        public static long sizeOnHeapExcludingDataOf​(java.nio.ByteBuffer[] array)
        Amount of non-data heap memory consumed by the array of byte buffers. It sums memory consumed by the array itself and for each included byte buffer using sizeOnHeapExcludingDataOf(ByteBuffer).
      • sizeOnHeapOf

        public static long sizeOnHeapOf​(java.nio.ByteBuffer buffer)
        Measures the heap memory used by the specified byte buffer. If the buffer is a slab only the data size will be counted but not the internal overhead. A SLAB is assumed to be created by: buffer.duplicate().position(start).limit(end) without the use of slice().

        This method makes a certain amount of assumptions:

        • That slabs are always created using: buffer.duplicate().position(start).limit(end) and not through slice
        • That the input buffers are not read-only buffers
        • That the direct buffers that are not slab are not duplicates
        Non-respect of those assumptions can lead to an invalid value being returned.
        Parameters:
        buffer - the buffer to measure
        Returns:
        the heap memory used by the specified byte buffer.
      • sizeOnHeapExcludingDataOf

        public static long sizeOnHeapExcludingDataOf​(java.nio.ByteBuffer buffer)
        Measures the heap memory used by the specified byte buffer excluding the data. If the buffer shallow size will be counted. A SLAB is assumed to be created by: buffer.duplicate().position(start).limit(end) without the use of slice().

        This method makes a certain amount of assumptions:

        • That slabs are always created using: buffer.duplicate().position(start).limit(end) and not through slice
        • That the input buffers are not read-only buffers
        • That the direct buffers that are not slab are not duplicates
        Non-respect of those assumptions can lead to an invalid value being returned. T
        Parameters:
        buffer - the buffer to measure
        Returns:
        the heap memory used by the specified byte buffer excluding the data..
      • sizeOf

        public static long sizeOf​(java.lang.String str)
        Memory a String consumes
        Parameters:
        str - String to calculate memory size of
        Returns:
        Total in-memory size of the String
      • measureDeep

        public static long measureDeep​(java.lang.Object pojo)
        Parameters:
        pojo - the object to measure
        Returns:
        the size on the heap of the instance and all retained heap referenced by it, excluding portions of ByteBuffer that are not directly referenced by it but including any other referenced that may also be retained by other objects.
      • measureDeepOmitShared

        public static long measureDeepOmitShared​(java.lang.Object pojo)
        Parameters:
        pojo - the object to measure
        Returns:
        The size on the heap of the instance and all retained heap referenced by it, excluding portions of ByteBuffer that are not directly referenced by it but including any other referenced that may also be retained by other objects. This also includes bytes referenced in direct byte buffers, and may double-count memory if it is referenced by multiple ByteBuffer copies.
      • measure

        public static long measure​(java.lang.Object pojo)
        Parameters:
        pojo - the object to measure
        Returns:
        the size on the heap of the instance only, excluding any referenced objects