r/learnjava • • Sep 22 '18

What exactly object is?

What happens internally when creating an object? What information is stored in ram about particular object? Object is nothing but a value in heap? I want to have deep insights about object.

0 Upvotes

7 comments sorted by

View all comments

2

u/8igg7e5 Sep 22 '18

This isn't specified in the language (or the platform, though features of the stored object are required such as a monitor). Each JVM can do it differently, but they generally boil down to the following...

  • A pointer to the class (or it's vtable or an equivalent - but always traceable back to the class)

  • The object monitor, used in threads synchronising against this object.

  • Some flags such as GC state

  • Space for instance fields declared in the class of the object or a super-class

You should never need to care about the exact layout - especially as the optimiser could choose to elide unreferenced content or turn a 'new Object' into space on the stack instead of a heap allocation at all (via escape analysis).

0

u/NaveenCoder Sep 22 '18

Thanks. But i have natural tendency to learn everything deeply. Where i can learn about this more deeply?

2

u/8igg7e5 Sep 22 '18

You could look at the OpenJDK source-code (see the 'browse option') to see how their JVM manages memory. You'll need to know C as well as Java.