public class SizeOf extends Object
Implementation of 'sizeof'. It is very hard to get reasonable estimations of how much memory your structures take, this class it trying it any way. Often it's quite a heavy operation, so don't use it too much.
A count of the byte size of an object is done recursively and for every count a SizeOf instance
must be instantiated. The static getByteSize(Object) does that for you.
The core of the SizeOf object is then sizeof(Object) plus a (private) 'countedObjects'
collection. The sizeof method returns the size the given Object would increase the size of this
countedObjects administration. This means that it returns 0 if the Object was already measured by the SizeOf
instance.
This means that it can be tricky to interpret the results of sizeof. The basic rule is that you should take the size of a lot of similar objects with the same SizeOf instance, and take the average.
A good example is MMObjectNode. The first one counted will also
count a MMObjectBuilder object - because that is linked to it, so
its memory is also taken (indirectly) - and will therefor give an unexpectedly large value like 20
kb or so. The second MMObjectNode, of the same type, that you'd count would however give a much
better estimation of the memory used by one Node in MMBase. The MMObjectBuilder is not counted
any more in this second Object, because it was already counted because of the first one.
For every individual entity there are several strategies for guessing the size.
SizeMeasurable it uses SizeMeasurable.getByteSize(SizeOf)Collection it will simply sum the results of sizeof of its elements, and for a String
it will return getBytes().length().Don't forget to dereference or clear the SizeOf after use, otherwise it itself is a memory leak.
| Modifier and Type | Field and Description |
|---|---|
static int |
SZ_REF |
| Constructor and Description |
|---|
SizeOf() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Makes this SizeOf object ready for reuse.
|
static int |
getByteSize(Object obj) |
static void |
main(String[] argv) |
static int |
sizeof(boolean b) |
static int |
sizeof(byte b) |
static int |
sizeof(char c) |
static int |
sizeof(double d) |
static int |
sizeof(float f) |
static int |
sizeof(int i) |
static int |
sizeof(long l) |
int |
sizeof(Object obj) |
static int |
sizeof(short s) |
public static final int SZ_REF
public static int sizeof(boolean b)
public static int sizeof(byte b)
public static int sizeof(char c)
public static int sizeof(short s)
public static int sizeof(int i)
public static int sizeof(long l)
public static int sizeof(float f)
public static int sizeof(double d)
public static int getByteSize(Object obj)
public void clear()
public int sizeof(Object obj)
0 if the object was
already counted by this SizeOf object.public static void main(String[] argv) throws InterruptedException
InterruptedExceptionMMBase 1.9-SNAPSHOT - ${javadoctimestamp}