JVM (HotSpot) : Where do all the methods go ? method area ? native method stack?

空扰寡人 提交于 2020-01-23 21:52:27

问题


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 ?


回答1:


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:

  • https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation
  • How is the java memory pool divided?
  • http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

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......




回答2:


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.




回答3:


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.




回答4:


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/




回答5:


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.



来源:https://stackoverflow.com/questions/11821974/jvm-hotspot-where-do-all-the-methods-go-method-area-native-method-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!