org.mmbase.util
Class SizeOf

Package class diagram package SizeOf
java.lang.Object
  extended by org.mmbase.util.SizeOf

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.

Don't forget to dereference or clear the SizeOf after use, otherwise it itself is a memory leak.

Since:
MMBase-1.6
Version:
$Id: SizeOf.java 45316 2011-02-18 20:47:28Z michiel $
Author:
Michiel Meeuwissen
To Do:
We need to know how well this actually works...

Field Summary
static int SZ_REF
           
 
Constructor Summary
SizeOf()
           
 
Method Summary
 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SZ_REF

public static final int SZ_REF
See Also:
Constant Field Values
Constructor Detail

SizeOf

public SizeOf()
Method Detail

sizeof

public static int sizeof(boolean b)

sizeof

public static int sizeof(byte b)

sizeof

public static int sizeof(char c)

sizeof

public static int sizeof(short s)

sizeof

public static int sizeof(int i)

sizeof

public static int sizeof(long l)

sizeof

public static int sizeof(float f)

sizeof

public static int sizeof(double d)

getByteSize

public static int getByteSize(Object obj)

clear

public void clear()
Makes this SizeOf object ready for reuse.

Since:
MMBase-1.8

sizeof

public int sizeof(Object obj)
Returns:
The size in bytes obj structure will take, or 0 if the object was already counted by this SizeOf object.

main

public static void main(String[] argv)
                 throws InterruptedException
Throws:
InterruptedException


MMBase2 Utils 2.0-SNAPSHOT - 2013-03-30T06:24