Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : size of inner class

I have this class :

public class Stack {

   private class Node {
       String item;
       Node next;
   }
   // some other methods here

}

In my book, the author says that the size per stack Node is 40 bytes including:

16 bytes (object overhead)
8 bytes (inner class extra overhead)
8 bytes (references to string)
8 bytes (references to node)
----------------------------------------------
40 bytes per stack node

I understand that the last two things refer to the size of the references to the String and Node. But I don't know what the object overhead and inner class extra overhead correspond to. Can you please explain?

like image 477
hqt Avatar asked Aug 30 '12 08:08

hqt


People also ask

How do you find the value of an inner class?

Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.

What are the rules for inner class?

Rules of Local Inner Class:The scope of the local inner class is restricted to the block they are defined in. A local inner class cannot be instantiated from outside the block where it is created in. Till JDK 7, the Local inner class can access only the final local variable of the enclosing block.

How many bytecode files are generated for inner class?

Explanation : The java compiler creates two class files in case of inner class.


2 Answers

object overhead

Every object has a header which is typically 8-12 bytes long. Each object is also 8 byte aligned and a simple estimate is you say its about 16 bytes long.

inner class extra overhead

As your inner class is not static, it has a reference to the outer class.

If this were an anonymous inner class you might have copies of any number of final variables (any used in the anonymous class code)

8 bytes (inner class extra overhead) 8 bytes (references to string) 8 bytes (references to node)

Most JVMs use 32-bit references so the size would be 4 bytes. Even 64-bit JVM with a heap up to 32 GB can use 32-bit references.

like image 53
Peter Lawrey Avatar answered Oct 13 '22 22:10

Peter Lawrey


First of all: all the concrete numbers that you quote are very implementation-dependent and can vary a lot based on what JVM you use.

Most JVMs, however, will have similar values (most JVMs use an object header for example).

The object header contains "bookkeeping" information that the JVM needs to keep track of the object. For example it will usually contain something that indicates the exact type of the object.

What you call "inner class extra overhead" probably refers to the fact that every instance of a non-static inner class is "bound" to an instance of the outer class. You can think of it as an implicit field that looks like this: private final Stack Stack_this. In fact, you can actually use that value by referring to Stack.this inside the code of Node. That implcit field will take the same amount of memory as a normal reference variable (which is usually 4 or 8 bytes, depending on your architecture/JVM).

like image 25
Joachim Sauer Avatar answered Oct 13 '22 22:10

Joachim Sauer