Java Object memory usage - ibm jvm 1.4.2

Is it possible to find memory usage in a Java object in an application?

I want the memory usage of the object to be part of the debug output when the application starts. I do not want to connect using an external application to the VM.

I have a problem that several classes feed on a huge amount of memory and cause memory problems, my application crashes. I need to find this memory usage (I work with limited memory resources).

EDIT: I am using java 1.4: /

+4
source share
4 answers

See my favorite project, MemoryMeasurer . A tiny example:

long memory = MemoryMeasurer.measureBytes(new HashMap());

:

Footprint footprint = ObjectGraphMeasurer.measure(new HashMap());

, , , , ( ). , , ( ) HashSet, , 1 ( ), 5 int, HashMap ( , HashSet HashMap) ..

. , , , .

Instrumentation Java 1.4 ( , ?!), memoryBytes . . - ( 32- ):

long memory = footprint.getObjects() * 8 + footprint.getReferences() * 4 +
              footprint.getPrimitives().count(int.class) * 4 + 
              footprint.getPrimitives().count(long.class) * 8 + ...;

. 16:

long alignedMemory = (x + 15) & (~0xF); //the last part zeros the lowest 4 bits

, , , 16 , , , , ( - ). visitor ( MemoryMeasurer ObjectGraphMeasurer - , ), , , Instrumentation, Java 1.5.

+6
+1

, jdk, jmap jhat, .

0
source

The link below contains a piece of Java code that calculates the size of objects:

http://www.javaworld.com/javaworld/javatips/jw-javatip130.html

0
source

All Articles