What is Klass & KlassKlass
Asked Answered
O

2

24

What is Klass & KlassKlass in JVM Hotspot Implementation?

As far as I understood from the article Presenting the Perm Generation, Klass is the internal representation of a Java class (let's say A) & it will hold the basic information about the structure of the class including the bytecode. It will be stored as an object itself. Every object of the class A will have a pointer to the internal representation Klass present in PermGen

KlassKlass is the internal representation of the Klass class itself. Why does KlassKlass is needed? What extra information is it storing?

Also, a KlassKlass's Klass pointer points to itself, I didn't understand it either.

Omnibus answered 23/5, 2013 at 18:11 Comment(0)
S
30

Permanent Generation a.k.a permgen is the referred to the place where all the class related information is stored. It is occasionally referred as method-area.

Lets take an example of the following code:

public class Parent
{
    ...
}

Here:

  • new Parent() is an object of Parent class.
  • (new Parent()).getClass() refers to Klass of Parent. The reference type for this object would be java.lang.Class<Parent>. This would store the information about Parent's annotations, constructors, fields, methods, its inheritance (superclass, interfaces) etc
  • KlassKlass would be (new Parent()).getClass().getClass(). The reference type for this object would be java.lang.Class<java.lang.Class>. This defines the information about java.lang.Class's annotations, constructors, fields, methods, its inheritance (superclass, interfaces), etc.

Theoretically, this chain could go on but KlassKlassKlass would be same as KlassKlass.

Inshort, KlassKlass means that you would have one java.lang.Class object which defines the behavior of java.lang.Class itself.

Hope this helps

Strage answered 11/6, 2013 at 20:22 Comment(0)
P
1

You seem to have a good general understanding of storage management in HotSpot. To get some more specific details it's a good idea to go to the horse's mouth - in this case the developers of HotSpot. This page is their description of the storage management system.

In situations where I want to find out what some code does I try and find the code and read it. In this case we can find an implementation of the KlassKlass class here. This doesn't seem to add an extra information on top of Klass - which is very interesting to look at. Note that KlassKlass extends Klass.

So why do we need it? Well, we can draw out the Klass hierarchy of a particular program as a tree where one Klass describes another. Everything needs a description but we cannot keep going like this forever, we need to find a fixed point where the thing we use to describe something is itself - so we can stop!

So, in summary KlassKlass doesn't do anything more useful than existing. It needs to exist but its existence is reasonably uninteresting.

Pecten answered 11/6, 2013 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.