public final class ObjectSize extends Object
Security. This implementation uses AccessController.doPrivileged() so it could be granted privileges to access non-public class fields separately from the application code. The minimum set of permissions necessary for this class to function correctly follows:
permission java.lang.RuntimePermission "accessDeclaredMembers"; permission java.lang.reflect.ReflectPermission "suppressAccessChecks";We tried to align with all the information from here: https://shipilev.net/jvm/objects-inside-out/
Remember, that after every update of JDK 7+ and higher the size of objects can be changed. If, suddenly, you'll notice unexpected behaviour in object size measurement, use memory-measurer project for check the size in bytes. Keep the basic size of int fields up-to-date.
Modifier and Type | Field and Description |
---|---|
static int |
SIZE_ARRAY_OBJECT
Size of an array object in bytes.
|
static int |
SIZE_BOOLEAN_FIELD
Size of a
boolean field in bytes. |
static int |
SIZE_BYTE_FIELD
Size of a
byte field in bytes. |
static int |
SIZE_CHAR_FIELD
Size of a
char field in bytes. |
static int |
SIZE_DATE_OBJECT
Size of a
Date object in bytes, assuming it does not use a Calender. |
static int |
SIZE_DOUBLE_FIELD
Size of a
double field in bytes. |
static int |
SIZE_FLOAT_FIELD
Size of a
float field in bytes. |
static int |
SIZE_INT_FIELD
Size of a
int field in bytes. |
static int |
SIZE_LONG_FIELD
Size of a
long field in bytes. |
static int |
SIZE_OBJECT_BASE
Basic size of an object in bytes.
|
static int |
SIZE_OBJECT_REF
Size of an object reference field in bytes.
|
static int |
SIZE_SHORT_FIELD
Size of a
short field in bytes. |
Modifier and Type | Method and Description |
---|---|
static int |
getAlignment(int sizeWithoutAlign)
Any java object is placed in memory with alignment to 8 bytes.
|
static int |
sizeof(Object obj)
Estimates the size of the object graph rooted at the specified object.
|
static int |
sizeofString(String s)
Deprecated.
use sizeof
|
public static final int SIZE_OBJECT_BASE
public static final int SIZE_OBJECT_REF
public static final int SIZE_LONG_FIELD
long
field in bytes.public static final int SIZE_INT_FIELD
int
field in bytes.public static final int SIZE_SHORT_FIELD
short
field in bytes.public static final int SIZE_CHAR_FIELD
char
field in bytes.public static final int SIZE_BYTE_FIELD
byte
field in bytes.public static final int SIZE_BOOLEAN_FIELD
boolean
field in bytes.public static final int SIZE_DOUBLE_FIELD
double
field in bytes.public static final int SIZE_FLOAT_FIELD
float
field in bytes.public static final int SIZE_ARRAY_OBJECT
public static final int SIZE_DATE_OBJECT
Date
object in bytes, assuming it does not use a Calender. UPD: In Java 8 java.util.Date object's size is 28 bytes. Please refer to top class javadoc for further details.
@Deprecated public static int sizeofString(String s)
s
- the string to estimate the size of; may be null
.public static int sizeof(Object obj)
obj
- the object to estimate the size of.null
.RuntimeException
- Thrown if the method failed to access fields of an object
in the object graph, probably for security reasons.public static int getAlignment(int sizeWithoutAlign)
sizeWithoutAlign
- size of object without alignCopyright (c) 2014-2021 All Rights Reserved by the RWS Group for and on behalf of its affiliates and subsidiaries