Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Klass & KlassKlass

Tags:

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.

like image 999
Vivek Avatar asked May 23 '13 18:05

Vivek


People also ask

What's the meaning of klass?

Noun. klass c. class. a group, collection, category or set sharing characteristics or attributes. a group of students or pupils regularly taking classes together, with a teacher.

What is klass in Python?

"klass" is a common convention in OOP code when referring to classes, because "class" and "Class" are often reserved already. mikepurvis on April 18, 2019 | next [–] Python typically goes with `cls`, particularly in conjunction with the `@classmethod` decorator. qwsxyh on April 18, 2019 | parent | next [–]

What is klass in rails?

klass is also a Rails (or rather ActiveRecord) method. It is used for getting the class of an association. As stated in the linked content: class Author < ActiveRecord::Base has_many :books end Author.reflect_on_association(:books).klass # => Book. Follow this answer to receive notifications.


2 Answers

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

like image 68
Chris Avatar answered Oct 07 '22 13:10

Chris


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.

like image 44
selig Avatar answered Oct 07 '22 13:10

selig