本文介绍了JVM中的类加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时将类加载到JVM中?它们是在服务器启动时加载的,还是有该类的引用时加载的?我的假设是,像jboss这样的服务器启动时,所有类都将被加载,但是然后有一种称为延迟加载的东西。

When does the class get loaded in the JVM? Do they get loaded on the server start up or when there is a reference for the class? My assumption is that all the class gets loaded as the server like jboss starts up but then there is something called lazyloading.

那么加载实际上是什么意思?这是否意味着JVM内存中存在该.class以及所有方法,变量(包括实例),静态变量方法,并且可供执行。我知道ClassLoader定位需要加载的Java类的字节码,读取字节码,检查particualr类中使用的其他类的引用,并通过创建java.lang.Class类的实例来加载它们。这使该类可用于JVM进行执行

Also what is actually meant by loading? Does it mean there is this .class in the JVM memory along with all the methods, variables including instance, static variables methods and are available for execution. I Know that ClassLoader locates the bytecodes for a Java class that needs to be loaded, reads the bytecodes, checks the refrencces of other class used in the particualr class and loads them as well by creating an instance of the java.lang.Class class. This makes the class available to the JVM for execution

是否也将与类一起加载到JVM中的方法?我的假设是方法仅在线程的堆栈内存中。那么什么是方法记忆?是堆还是堆栈的一部分?

Are methods also loaded in the JVM along with the class? My assumption is that methods are only in the stack memory of threads. then What is method memory? Is it a part of heap or stack?

是否仅将静态方法与类加载一起加载,而不是实例方法?知道当类被初始化时,静态块会被执行,所有静态变量也会被初始化。

Do only static methods get loaded along with class loading and not the instance method? Iknow that static bock gets executed when the class get laoded and also all the static variables get initialzed.

如果这些疑虑得到了清除,请先感谢。

Thanks in advance if these doubts get cleared.

推荐答案

这些是有关JVM和Google的基本问题,肯定可以帮助您解决问题。

These are pretty much basic questions about JVM and Google could surely help you with answers.

对于您的某些问题(尤其是有关实际加载过程的问题),您可以在此处查看,例如:

For some of your questions (especially for the questions about the actual loading process), you could look here, for example: http://www.securingjava.com/chapter-two/chapter-two-7.html

简而言之,一开始,JVM仅加载了基本(和受信任)类。接下来,根据需要创建其他类加载器(例如,引导类加载器),它们将加载更多类。在成功加载类之前,必须先加载其依赖的所有类。

On short, at the beginning, just the basic (and trusted) classes are loaded by the JVM. Next, other classloaders (for example the bootstrap classloader) are created as required and they will load some more classes. Before a class can be successfully loaded, all the classes it depends on must be loaded.

已加载的类以各种形式(这是JVM特定的)存储在内存中,但是Class对象总是公开的。类中的所有内容(方法,变量等)都将被加载。这并不意味着也要编译该类(稍后会在需要执行方法时进行编译)。

A loaded class is stored in memory in various forms (this is JVM specific), but a Class object is always exposed. Everything inside the class (methods, variables etc.) gets loaded. This doesn't mean that the class is also compiled (compilation happens later, when a method needs to be executed).

方法变量的分配在堆栈上进行(对于

Allocation of method variables happens either on stack (for primitives) or on heap.

静态变量的初始化和静态块的执行发生在类加载之后,创建类的任何实例之前。

Initialization of static variables and execution of static blocks happens right after the class is loaded, before any instances of it are created.

这篇关于JVM中的类加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 06:48