5

Hmmm. Is there a primer anywhere on memory usage in Java? I would have thought Sun or IBM would have had a good article on the subject but I can't find anything that looks really solid. I'm interested in knowing two things:

  1. at runtime, figuring out how much memory the classes in my package are using at a given time
  2. at design time, estimating general memory overhead requirements for various things like:
    • how much memory overhead is required for an empty object (in addition to the space required by its fields)
    • how much memory overhead is required when creating closures
    • how much memory overhead is required for collections like ArrayList

I may have hundreds of thousands of objects created and I want to be a "good neighbor" to not be overly wasteful of RAM. I mean I don't really care whether I'm using 10% more memory than the "optimal case" (whatever that is), but if I'm implementing something that uses 5x as much memory as I could if I made a simple change, I'd want to use less memory (or be able to create more objects for a fixed amount of memory available).

I found a few articles (Java Specialists' Newsletter and something from Javaworld) and one of the builtin classes java.lang.instrument.getObjectSize() which claims to measure an "approximation" (??) of memory use, but these all seem kind of vague...

(and yes I realize that a JVM running on two different OS's may be likely to use different amounts of memory for different objects)

asked Dec 24, 2008 at 0:01
1

8 Answers 8

4

I used JProfiler a number of years ago and it did a good job, and you could break down memory usage to a fairly granular level.

answered Dec 24, 2008 at 5:57

Comments

4

As of Java 5, on Hotspot and other VMs that support it, you can use the Instrumentation interface to ask the VM the memory usage of a given object. It's fiddly but you can do it. In case you want to try this method, I've added a page to my web site on querying the memory size of a Java object using the Instrumentation framework.

As a rough guide in Hotspot on 32 bit machines:

  • objects use 8 bytes for "housekeeping"
  • fields use what you'd expect them to use given their bit length (though booleans tend to be allocated an entire byte)
  • object references use 4 bytes
  • overall obejct size has a granularity of 8 bytes (i.e. if you have an object with 1 boolean field it will use 16 bytes; if you have an object with 8 booleans it will also use 16 bytes)

There's nothing special about collections in terms of how the VM treats them. Their memory usage is the total of their internal fields plus -- if you're counting this -- the usage of each object they contain. You need to factor in things like the default array size of an ArrayList, and the fact that that size increases by 1.5 whenever the list gets full. But either asking the VM or using the above metrics, looking at the source code to the collections and "working it through" will essentially get you to the answer.

If by "closure" you mean something like a Runnable or Callable, well again it's just a boring old object like any other. (N.B. They aren't really closures!!)

answered Dec 25, 2008 at 18:53

2 Comments

(cleaning up old unacceptied questions: I'll accept this if you can point me towards your source for Hotspot object usage on 32-bit machines, e.g. the 8 bytes for housekeeping + 4 bytes for objrefs + 8 byte granularity)
These effectively come from the structures in the Hotspot source code, though they are borne out by actual measurements. Obviously, these figures only hold for 32-bit version. I confess I haven't whether anything has been altered in Java 7, though I would have thought not.
3

You can use JMP, but it's only caught up to Java 1.5.

answered Dec 24, 2008 at 1:42

Comments

1

I've used the profiler that comes with newer versions of Netbeans a couple of times and it works very well, supplying you with a ton of information about memory usage and runtime of your programs. Definitely a good place to start.

answered Dec 26, 2008 at 4:57

Comments

1

If you are using a pre 1.5 VM - You can get the approx size of objects by using serialization. Be warned though.. this can require double the amount of memory for that object.

answered Dec 27, 2008 at 23:15

Comments

0

See if PerfAnal will give you what you are looking for.

answered Dec 24, 2008 at 0:13

1 Comment

that looks like a great tool for profiling CPU usage, but does it also profile memory usage? I skimmed through the article & it doesn't seem like it.
0

This might be not the exact answer you are looking for, but the bosts of the following link will give you very good pointers. Other Question about Memory

answered Dec 24, 2008 at 4:33

Comments

0

I believe the profiler included in Netbeans can moniter memory usage also, you can try that

answered Dec 24, 2008 at 5:43

Comments

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.