1

I am new to JVM (HotSpot), and try to understand the architecture of it and how it works, so my question is that do all the methods (both static and non-static) get translated into byte-code ? and when JVM loads the class files, does it load all the methods into somewhere ? like method area ? or native method stacks ?

peter
  • 8,333
  • 17
  • 71
  • 94

6 Answers6

2

It's dependent on the JVM implementation - different JVMs may choose to handle this in different ways (as long as they conform to the Java spec). So you have no absolute guarantees.

In the Sun/Oracle JVM the method data gets loaded into a special memory area called the "Permanent Generation", which is an area of memory reserved by the garbage collector for long-lived objects such as classes.

Most other "industrial-strength" JVMs are likely do something similar.

See:

P.S.

This is all quite advanced stuff - you definitely don't need to know anything about this to make good use of Java and/or the JVM. You should generally assume that the JVM does memory management on your behalf and will do so efficiently - it's had many years of tuning by experts.

The whole point of the JVM is to allow you to abstract away from the implementation details of the specific platform, after all......

Community
  • 1
  • 1
mikera
  • 105,238
  • 25
  • 256
  • 415
  • both static and non-static get loaded into Permanent Generation ? and what do method area and native method stacks do then ? – peter Aug 06 '12 at 03:22
  • Both static and non-static classes go in permgen in the Sun/Oracle JVM at least. You might want to check out the extra links I'm adding re: the other stuff. – mikera Aug 06 '12 at 03:34
  • Static and non-static methods are pretty much the same. Other than the dynamic resolution issue (which isn't relevant to this discussion), the only difference is that instance methods have an implicit argument, the object they take. So your instance method `MyClass.foo(int i)` is really `MyClass.foo(MyClass this, int i)` to which your instance is passed for that first argument. (It doesn't have to be done exactly like that, but that's the general idea.) So from the standpoint of the structure and execution of the code, static and instance methods work similarly. – yshavit Aug 06 '12 at 04:33
1

To be precise,

  • All the methods(static and Non static) will be loaded in the method Area.

  • Method calls, local variables, intermediate results and the line of the execution would be stored in Stack.

  • If a method is being executed, it would on top of the stack. Once it is done executing all the results would be erased, if there are any local reference variables, they would be nullified.

  • Irrespective of the method being executed, method Area has the class info. It is similar to cache in a browser, holding the required info for JVM.

Jayanth
  • 746
  • 6
  • 17
1

1.) Do all the methods (both static and non-static) get translated into byte-code?

  • Yes, all the members of a class whether static or non-static get translated into byte-code. And the compiler is responsible for all this translation/compilation.

2.) when JVM loads the class files, does it load all the methods into somewhere ? like method area? or native method stacks?

  • The entire bytecode(including method bytecode) of the classes gets loaded into the respective class blocks.
  • class blocks are logically separated memory blocks inside the method area that stores class-level data of their respective classes such as bytecode, method table, and static variables.

enter image description here enter image description here enter image description here

  • class-level data is instance independent i.e. why it is not stored in instances(objects) of a class in heap. A single copy of class-level data is shared among all the objects.
  • So, the simple answer to this question would be "All the class bytecode(including methods' bytecode) is loaded in Method Area".
  • To learn more about Method Area visit https://www.artima.com/insidejvm/ed2/jvm5.html or refer SCJP/Oracle Study Guides
0

Yes, all the methods get translated into byte code. And the byte code files are intermediate files which the jvm will load from.

When jvm loads the class file? It will do it when the class is first used -- containing several situations:

  1. Creating a instance of the class: new operator, reflection, clone method or deserialization.
  2. Inoking a static method of the class.
  3. Using or evaluating a static variable of the class or interface except the final static variables, because they are compile time constants.
  4. Invoking a method by reflection.
  5. Loading a subclass of the class. It works just for class except interface.
  6. The bootstrap class of the jvm. eg. the class containing the main method.
  7. A interface needn't to be intialized when the class which implements the interface is intialized, but must be loaded.

Yes, the methods are loaded into the method area. In other words, the byte code file are loaded into the method area.

kino lucky
  • 1,365
  • 4
  • 15
  • 24
0

I would generally suggest that you read this great article about the essentials of the JVM.

https://anturis.com/blog/java-virtual-machine-the-essential-guide/

Sabry Shawally
  • 603
  • 2
  • 11
  • 24
0

Memory consumed by Java process could be categorize into Java and Native heap. Java heap is the memory section allocated by jvm of size Xmx which is used for java object allocation where as Native memory section allocated by JNI code and allocation done by native languages. Is that do all the methods (both static and non-static) get translated into byte-code ?

Code written in java are translated into byte code for accessing irrespective of access specifier or modifier

When JVM loads the class files,does it load all the methods into somewhere? like method area ?or native method stacks ?

Class(Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods.

Mohan Raj
  • 1,104
  • 9
  • 17