JVM class loader is how is it

Cross-platform java principle: .java source files to generate .class file by compiling the byte code file, then loaded by the java virtual machine and run.


What class loader is: to load class byte code into memory
class loading mechanism is divided into two types: Implicit loading, the display load
loaded implicitly: by the new keyword
display load: Class.forname reflected by ()
class loading mechanism: class loading mechanism is divided into five stages, loading, connect, initialize, use and uninstall.

Note that a class can only be loaded once.

Load: The loaded into memory byte code file fully qualified class name of the class, and all of the information classes Class object inside the package, placed in a Class object methods zone, and then create the heap java.lang.Class association class object (reflective object is to direct operation), the process is completed by the class loader.
Connected: three stages
1. Validation: this byte code verification stage for compliance java no security risk.
2. Prepare: official allocate memory space (only the allocated memory space, no assignment this time is also the default value) for class variables.
3. Analysis: The symbolic references to variables replace direct reference
Initialization: formal class initialization, initialize static variables and assignment, perform a static code block, set a default value for the common attributes open space.
When loading a class if the class has a parent class loader will give priority to its parent. Object class, except that it has no parent.

When the class is loaded into memory, we can use the new keyword target of the operation.
When using the new keyword when copied an object in memory out, JVM allocates memory for the object.

JVM allocates memory space is divided into two types: pointer collision, the free list.

指针碰撞:当java的内存空间是规整的,将使用指针碰撞的分配方式。
当使用new关键字创建对象的时候,首先在堆中存在一个指针,他将移动一块和当前的创建对象相等的空间(这也是为什么对象默认值是null的原因),然后将对象存入到这个空的内存空间中。

空闲列表:当java的内存空间不是规整的,也就是说有很多琐碎的空白的内存,将使用空闲列表的分配方式。
在JVM中维护了一张保存空闲内存空间的列表,当创建出来一个新的对象会在这张列表里面找是否有空着的符合当前新对象的内存大小,然后将对象保存在符合的空间中。

内存地址是否规整取决于gc是否使用了压缩算法。

Guess you like

Origin blog.csdn.net/weixin_45118251/article/details/90578968