Core Java

How much memory do I need

What is retained heap?

How much memory will I need? This is a question you might have asked yourself (or others) when building a solution, creating a data structure or choosing an algorithm. Will this graph of mine fit in my 3G heap if it contains 1,000,000 edges and I use a HashMap to store it? Can I use the standard Collections API while building my custom caching solution or is the overhead posed by them too much?

Apparently, the answer to the simple question is a bit more complex. In this post we’ll take a first peek at it and see how deep the rabbit hole actually is.

The answer to the question in the headline comes in several parts. At first we need to understand whether you are interested in shallow or retained heap sizes.

The shallow heap is easy – it consists of only the heap occupied by the object itself. There are some nuances to how to calculate it, but for the scope of this article we leave it as is. Stay tuned for future posts on the same topic.

The retained heap is in many ways more interesting. Only rarely are you interested in the shallow heap, in most cases your actual question can be translated to “If I remove this object from the memory, how much memory can now be freed by the garbage collector”.

Now, as we all remember, all Java garbage collection (GC) algorithms follow this logic:

  1. There are some objects which are considered “important” by the GC. These are called GC roots and are (almost) never discarded. They are, for example, currently executing method’s local variables and input parameters, application threads, references from native code and similar “global” objects.
  2. Any objects referenced from those GC roots are assumed to be in use and hence not discarded by the GC. One object can reference another in different ways in Java, in the most common case an object A is stored in a field of an object B. In such case we say “B references A”.
  3. The process is repeated until all objects that can be transitively reached from GC roots are visited and marked as “in use”.
  4. Everything else is unused and can be thrown away.

Now to illustrate how to calculate the retained heap, let’s follow the aforementioned algorithm with the following example objects:

To simplify the sample, let’s estimate that all the objects O1-O4 have the shallow heap of 1024B = 1kB. Lets start calculating the retained sizes of those objects.

  • O4 has no references to other objects, so its retained size is equal to its shallow size of 1kB.
  • O3 has a reference to O4. Garbage collecting O3 would thus mean O4 would also be eligible for garbage collection and so we can say that O3 retained heap is 2kB.
  • O2 has a reference to O3. But it is now important to note that removing the pointer from O2 to O3 does not make O3 eligible for GC, as O1 still has got a pointer to it. So O2 retained heap is only 1kB.
  • O1 on the other hand is the object keeping all the references in this small graph, so if we would remove O1, everything on this graph would be garbage collected. So O1 retained heap is 4kB.

Which implications does this have in practice? In fact, understanding the differences between shallow and retained heap sizes makes it possible to work with tools such as memory profilers and heap dump analyzers – for example digging into Eclipse MAT might prove to be impossible if you don’t know how to distinguish these two types of heap size measurements.

What is shallow heap?

This article is the second post in the series where we try to answer those questions. The last post explained the difference between retained and shallow sizes of an object. In the article we also offered an example of how to calculate retained heap size of a data structure. In today’s article we will expand on what we called “simple” in the previous post. Namely – what is and how to measure shallow heap used by an object.

In the first post we pushed a whole lot of complexity away by stating that calculating shallow heap size is easy – it consists of only the heap occupied by the object itself. But how do you calculate how much memory does the object “itself” require? Apparently there is a formula for it:

Shallow Heap Size = [reference to the class definition] + space for superclass fields + space for instance fields + [alignment]

Does not seem too helpful, eh? Let’s try to apply the formula using the following sample code:

class X {
   int a;
   byte b;
   java.lang.Integer c = new java.lang.Integer();
class Y extends X {
   java.util.List d;
   java.util.Date e;

Now, the question we strive to answer is – how much shallow heap size does an instance of a Y require? Lets start calculating it, assuming that we are on a 32-bit x86 architecture:

As a starting point – Y is a subclass of X, so its size includes “something” from the superclass. Thus, before calculating the size of Y, we look into calculating the shallow size of X.

Jumping into the calculations on X, first 8 bytes are used to refer its class definition. This reference is always present in all Java objects and is used by JVM to define the memory layout of the following state. It also has three instance variables – an int, an Integer and a byte. Those instance variables require heap as follows:

  • a byte is what it is supposed to be. 1 byte in a memory.
  • an int in our 32bit architecture requires 4 bytes.
  • a reference to the Integer requires also 4 bytes. Note that when calculating retained heap, we should also take into account the size of a primitive wrapped into the Integer object, but as we are calculating shallow heap here, we only use the reference size of 4 bytes in our calculations.

So – is that it? Shallow heap of X = 8 bytes from reference to the class definition + 1 byte (the byte) + 4 bytes (the int) + 4 bytes (reference to the Integer) = 17 bytes? In fact – no. What now comes into play is called alignment (also called padding). It means that the JVM allocates the memory in multiples of 8 bytes, so instead of 17 bytes we would allocate 24 bytes if we would create an instance of X.

If you could follow us until here, good, but now we try to get things even more complex. We are NOT creating an instance of X, but an instance of Y. What this means is – we can deduct the 8 bytes from the reference to the class definition and the alignment. It might not be too obvious at first place but – did you note that while calculating the shallow size of X we did not take into account that it also extends java.lang.Object as all classes do even if you do not explicitly state it in your source code? We do not have to take into account the header sizes of superclasses, because JVM is smart enough to check it from the class definitions itself, instead of having to copy it into the object headers all the time.

The same goes for alignment – when creating an object you only align once, not at the boundaries of superclass/subclass definitions. So we are safe to say that when creating a subclass to X you will only inherit 9 bytes from the instance variables.

Finally we can jump to the initial task and start calculating the size of Y. As we saw, we have already lost 9 bytes to the superclass fields. Let’s see what will be added when we actually construct an instance of Y.

  • Y’s headers referring to its class definition consume 8 bytes. The same as with previous ones.
  • The Date is a reference to an object. 4 bytes. Easy.
  • The List is a reference to a collection. Again 4 bytes. Trivial.

So in addition to the 9 bytes from the superclass we have 8 bytes from the header, 2×4 bytes from the two references (the List and the Date). The total shallow size for the instance of Y would be 25 bytes, which get aligned to 32.

To make the calculations somewhat easier to follow, we have aggregated it on the following diagram:


What can you do with this knowledge? Together with the skills to calculate the size of retained heap (covered in my recent post), you now possess the ultimate power to calculate how much memory your data structures actually require.

To make things even more interesting, we have created an utility that measures the sizes of both shallow and retained heap for your objects. In the very near future we will release the tool for free use. Stay tuned by subscribing to our Twitter feed!

Measure, don’t guess

What looks like as an easy task can in reality become somewhat complicated. There is a whole lot of different aspects you have to bear in mind when calculating the memory footprint of your objects:

  • Do I need to measure shallow or retained heap size?
  • Do I make the calculations for 32 or 64bit architecture?
  • Am I running on x86, SPARC, POWER or on something even beyond imagination?
  • Do I use compressed or uncompressed ordinary object pointers?
  • [enter something else you are afraid or do not completely understand here]

Bearing all those aspects in mind when trying to estimate the size of your data structures is simply unreasonable when trying to meet yet another deadline. So we went ahead and packaged the code published by Java Champion Heinz Kabutz as a java agent and provided an easy way to add it to your application.

Adding the agent gives you an easy way to trace how much memory your data structures on your actual environment take. And does it without the complexity introduced by the alternatives. In the following four easy steps you are up&running and finally understanding how much memory do your precious caches actually consume:

Step 1: Download the agent. Don’t worry, its just few kilobytes.

Step 2: Unzip the downloaded agent. You see it is packaged along with it’s source code and a sample on how to use it. Feel free to play around with the code.

nikita-mb:sizeof nikita$ ls -l
total 16
-rw-r--r-- 1 nikita  staff  1696 Aug 28 22:12 build.xml
-rw-r--r--  1 nikita  staff  3938 Aug 28 22:33 sizeofagent.jar
drwxr-xr-x 5 nikita  staff    170 Aug 28 10:44 src

Step 3: Experiment with the bundled testcase. The bundled testcase measures the same data structure we described in our blog post about shallow heap size measurement. For those who do not bother clicking back and forth, here is the code again:

class X {
   int a;
   byte b;
   java.lang.Integer c = new java.lang.Integer();
class Y extends X {
   java.util.List d;
   java.util.Date e;

The testcase is shipped with Ant tests to compile and run the samples. Run ant test or ant test-32 if you are on a 32-bit architecture. You should see the following output when running all the tests with ant test:

nikita-mb:sizeof nikita$ ant test

Buildfile: /Users/nikita/workspace/sizeof/build.xml




      [java] java.lang.Object: shallow size=8 bytes, retained=8 bytes
      [java] eu.plumbr.sizeof.test.X: shallow size=24 bytes, retained=40 bytes
      [java] eu.plumbr.sizeof.test.Y: shallow size=32 bytes, retained=48 bytes


      [java] java.lang.Object: shallow size=16 bytes, retained=16 bytes
      [java] eu.plumbr.sizeof.test.X: shallow size=24 bytes, retained=40 bytes
      [java] eu.plumbr.sizeof.test.Y: shallow size=32 bytes, retained=48 bytes


      [java] java.lang.Object: shallow size=16 bytes, retained=16 bytes
      [java] eu.plumbr.sizeof.test.X: shallow size=32 bytes, retained=56 bytes
      [java] eu.plumbr.sizeof.test.Y: shallow size=48 bytes, retained=72 bytes


Total time: 2 seconds

From the test above you can see for example that on 32bit architecture, the shallow heap of Y consumes 32 bytes and retained heap 48 bytes. On 64bit architecture with -XX:-UseCompressedOops the shallow size increases to 48 bytes and retained heap size to 72 bytes. If it bedazzles you how do we calculate those numbers, then check out what is and how to calculate shallow and retained heap sizes from our previous posts in the series.

Step 4: Attach the agent to your very own Java application. To do this, add -javaagent:path-to/sizeofagent.jar to your JVM startup scripts. Now you can measure shallow heap consumption by invoking MemoryCounterAgent.sizeOf(yourObject) or measure retained heap consumption by invoking MemoryCounterAgent.deepSizeOf(yourObject) directly in your code. See the bundled ant scripts and eu.plumbr.sizeof.test.SizeOfSample class also in case you get confused while doing it.

Of course you have got numerous alternatives, especially in forms of memory profilers and APM solutions. But this small agent will do its task quickly and requires next to no set-up nor learning. Well, at minimum we had fun playing with it. Instead of crunching through our product backlog.

PS. While writing this article, the following online resources were used for inspiration:

And – do not forget to send your congratulations for this code to Heinz Kabutz, who published it originally in its Java Specialists’ Newsletter in March 2007.

Reference: How much memory do I need (part 1) – What is retained heap?, How much memory do I need (part 2) – What is shallow heap?, How much memory do I need (part 3) – measure, don’t guess from our JCG partner Nikita Salnikov Tarnovski at the Plumbr Blog blog.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button