简体   繁体   English

JVM (HotSpot):所有的方法都去哪儿了? 方法区? 本机方法堆栈?

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

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?我是 JVM (HotSpot) 的新手,并试图了解它的体系结构及其工作原理,所以我的问题是所有方法(静态和非静态)都被翻译成字节码了吗? and when JVM loads the class files, does it load all the methods into somewhere?当 JVM 加载类文件时,它是否将所有方法加载到某个地方? like method area?比如方法区? or native method stacks?或本机方法堆栈?

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).它取决于 JVM 实现——不同的 JVM 可能会选择以不同的方式处理它(只要它们符合 Java 规范)。 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.在 Sun/Oracle JVM 中,方法数据被加载到称为“永久代”的特殊内存区域,这是垃圾收集器为类等长期对象保留的内存区域。

Most other "industrial-strength" JVMs are likely do something similar.大多数其他“工业级”JVM 可能会做类似的事情。

See:看:

PS聚苯乙烯

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.这些都是非常高级的东西——您绝对不需要了解任何关于它的知识就可以很好地使用 Java 和/或 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.您通常应该假设 JVM 代表您进行内存管理,并且会高效地进行管理——它已经由专家进行了多年的调优。

The whole point of the JVM is to allow you to abstract away from the implementation details of the specific platform, after all...... JVM 的全部意义在于允许您从特定平台的实现细节中抽象出来,毕竟……

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.方法调用、局部变量、中间结果和执行行将存储在 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.不管正在执行的方法是什么,方法 Area 都有类信息。 It is similar to cache in a browser, holding the required info for JVM.它类似于浏览器中的缓存,保存 JVM 所需的信息。

1.) Do all the methods (both static and non-static) get translated into byte-code? 1.) 所有方法(静态和非静态)都被翻译成字节码了吗?

  • 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? 2.) 当 JVM 加载类文件时,它是否将所有方法加载到某个地方? 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.类块是方法区内部逻辑分离的内存块,存储各自类的类级数据,如字节码、方法表、静态变量等。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

  • class-level data is instance independent ie 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要了解有关方法区的更多信息,请访问https://www.artima.com/insidejvm/ed2/jvm5.html或参考 SCJP/Oracle 学习指南

Yes, all the methods get translated into byte code.是的,所有方法都被翻译成字节码。 And the byte code files are intermediate files which the jvm will load from.字节码文件是jvm将从中加载的中间文件。

When jvm loads the class file? jvm什么时候加载类文件? 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.创建类的实例:new 运算符、反射、克隆方法或反序列化。
  2. Inoking a static method of the class. Inoking类的静态方法。
  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. 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.换句话说,字节码文件被加载到方法区。

I would generally suggest that you read this great article about the essentials of the JVM.我通常建议您阅读这篇关于 JVM 基础知识的精彩文章。

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

Memory consumed by Java process could be categorize into Java and Native heap. Java 进程消耗的内存可以分为 Java 和 Native 堆。 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. Java 堆是由 jvm 分配的内存部分,大小为 Xmx,用于 java 对象分配,而本机内存部分由 JNI 代码分配,分配由本机语言完成。 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用java编写的代码被翻译成字节码进行访问,而不管访问说明符或修饰符

When JVM loads the class files,does it load all the methods into somewhere?当 JVM 加载类文件时,它是否将所有方法加载到某个地方? 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. Class(Method) Area 存储每个类的结构,例如运行时常量池、字段和方法数据、方法代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM