4

Is it possible to find memory usage of object in java within application?

I want to have object memory usage to be part of debug output when application runs. I don't want to connect using external application to VM.

I have a problem that few classes eats up huge amount of memory and causes memory problems, my app gets crash. I need to find that memory usage (I am working with limited memory resources).

EDIT: I am using java 1.4:/

asked Oct 19, 2010 at 8:17
2

4 Answers 4

6

See my pet project, MemoryMeasurer. A tiny example:

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

You may also derive more qualitative memory breakdown:

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

For example, I used the latter to derive the per entry cost of various data structures, where the overhead is measured in number of objects created, references, and primitives, instead of just bytes (which is also doable). So, next time you use a (default) HashSet, you can be informed that each element in it costs 1 new object (not your element), 5 references, and an int, which is the exact same cost for an entry in HashMap (not unexpectedly, since any HashSet element ends up in a HashMap), and so on.

You can use it on any object graph. If your object graph contains links other structures you do wish to ignore, you should use a predicate to avoid exploring them.

Edit Instrumentation is not available to Java 1.4 (wow, people still use that?!), so the memoryBytes call above wouldn't work for you. But the second would. Then you can write something like this (if you are on a 32bit machine):

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

That gives you an approximation. A better answer would be to ceil this to the closest multiple of 16:

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

But the answer might still be off, since if you find, say, 16 booleans, it's one thing if they are found in the same object, and quite another if they are spread in multiple objects (and cause excessive space usage due to aligning). This logic could be implemented as another visitor (similar to how MemoryMeasurer and ObjectGraphMeasurer are implemented - quite simply as you may see), but I didn't bother, since that's what Instrumentation does, so it would only make sense of Java versions below 1.5.

answered Oct 19, 2010 at 8:28

1 Comment

Nice tool, but I must verify 1.4 compatibility.
1

Eclipse MAT is a really good tool to analyze memory.

answered Oct 19, 2010 at 8:27

1 Comment

Thanks, this seems nice but it is external tool. What I found out on theirs page wiki.eclipse.org/index.php/MemoryAnalyzer is <i> If you run your application with the VM flag -XX:+HeapDumpOnOutOfMemoryError a heap dump is written on the first Out Of Memory Error.</i>. Though I doubt it will be supported in my java.
0

There are tools that comes with jdk such as jmap and jhat which provides object level details.

answered Oct 19, 2010 at 8:24

1 Comment

Thanks, unfortunately those are external tools. I need to get that information inside as noted.
0

The folowing link provides a piece of Java Code computing the size of objects:

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

answered Oct 19, 2010 at 8:29

1 Comment

It is benchmark, not observation of memory usage within application livetime.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.