Package io.github.jbellis.jvector.util
Class RamUsageEstimator
java.lang.Object
io.github.jbellis.jvector.util.RamUsageEstimator
Estimates the size (memory representation) of Java objects.
This class uses assumptions that were discovered for the Hotspot virtual machine. If you use a non-OpenJDK/Oracle-based JVM, the measurements may be slightly wrong.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final boolean
True, iff compressed references (oops) are enabled by this JVMstatic final long
Approximate memory usage that we assign to a Hashtable / HashMap entry.static final int
Recurse only into immediate descendants.static final int
Number of bytes to represent an array header (no content, but with alignments).static final int
A constant specifying the object alignment boundary inside the JVM.static final int
Number of bytes to represent an object header (no fields, no alignments).static final int
Number of bytes this JVM uses to represent an object reference.static final long
One gigabyte bytes.static final long
One kilobyte bytes.static final long
One megabyte bytes.Sizes of primitive classes.static final int
Approximate memory usage that we assign to all unknown objects - this maps roughly to a few primitive fields and a couple short String-s. -
Method Summary
Modifier and TypeMethodDescriptionstatic long
adjustForField
(long sizeSoFar, 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 ofNUM_BYTES_OBJECT_ALIGNMENT
.static String
humanReadableUnits
(long bytes, DecimalFormat df) Returnssize
in human-readable units (GB, MB, KB or bytes).static long
shallowSizeOf
(Object obj) Estimates a "shallow" memory usage of the given object.static long
shallowSizeOf
(Object[] arr) Returns the shallow size in bytes of the Object[] object.static long
shallowSizeOfInstance
(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
static long
Return the shallow size of the providedInteger
object.static long
Return the shallow size of the providedLong
object.static long
Returns the size in bytes of the String object.static long
Returns the size in bytes of the String[] object.
-
Field Details
-
ONE_KB
public static final long ONE_KBOne kilobyte bytes.- See Also:
-
ONE_MB
public static final long ONE_MBOne megabyte bytes.- See Also:
-
ONE_GB
public static final long ONE_GBOne gigabyte bytes.- See Also:
-
COMPRESSED_REFS_ENABLED
public static final boolean COMPRESSED_REFS_ENABLEDTrue, iff compressed references (oops) are enabled by this JVM -
NUM_BYTES_OBJECT_REF
public static final int NUM_BYTES_OBJECT_REFNumber of bytes this JVM uses to represent an object reference. -
NUM_BYTES_OBJECT_HEADER
public static final int NUM_BYTES_OBJECT_HEADERNumber of bytes to represent an object header (no fields, no alignments). -
NUM_BYTES_ARRAY_HEADER
public static final int NUM_BYTES_ARRAY_HEADERNumber of bytes to represent an array header (no content, but with alignments). -
NUM_BYTES_OBJECT_ALIGNMENT
public static final int NUM_BYTES_OBJECT_ALIGNMENTA constant specifying the object alignment boundary inside the JVM. Objects will always take a full multiple of this constant, possibly wasting some space. -
UNKNOWN_DEFAULT_RAM_BYTES_USED
public static final int UNKNOWN_DEFAULT_RAM_BYTES_USEDApproximate memory usage that we assign to all unknown objects - this maps roughly to a few primitive fields and a couple short String-s.- See Also:
-
primitiveSizes
-
HASHTABLE_RAM_BYTES_PER_ENTRY
public static final long HASHTABLE_RAM_BYTES_PER_ENTRYApproximate memory usage that we assign to a Hashtable / HashMap entry. -
MAX_DEPTH
public static final int MAX_DEPTHRecurse only into immediate descendants.- See Also:
-
-
Method Details
-
alignObjectSize
public static long alignObjectSize(long size) Aligns an object size to be the next multiple ofNUM_BYTES_OBJECT_ALIGNMENT
. -
sizeOf
-
sizeOf
-
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
Returns the size in bytes of the String[] object. -
sizeOf
-
sizeOf
Returns the size in bytes of the String object. -
shallowSizeOf
Returns the shallow size in bytes of the Object[] object. -
shallowSizeOf
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
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:
IllegalArgumentException
- ifclazz
is an array class.- See Also:
-
adjustForField
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). -
humanReadableUnits
Returnssize
in human-readable units (GB, MB, KB or bytes).
-