Hello. I'm looking for a way in java to create a buffer with a fixed memory size. For example I want to create a buffer which can hold 10MB of objects in memory, then when the buffer is full, it will deny any new objects being added to it (and write them to the disk instead). So far I haven't found anything in the Java API that suits my needs, so I decided to write a class myself.

However I quickly realised there seems to be no standard way of measuring the memory usage of an object in java (like c++ sizeof). I found the java.lang.instrument.Instrumentation class and it seems to be able to do what I want. However it doesn't seem very accurate, as it will round everything up to 8 bytes, so if I have an int and a long in a class, it will just show 8 bytes.

My current OS is fedora 14 and I'm using the sun JDK implementation (not the OpenJDK one that comes with fedora). I don't know if this matters, but I know that implementation of JVM differs for different systems.

Is there any way to do what I want, with the accuracy I want?