"Java Advanced" JVM memory area model and loading process

Source: Internet
Author: User

JVM Memory Area Model


1. Method Area

Also known as " Permanent Generation", "non-heap", It is used to store virtual machine loaded class information, constants, static variables, is the memory area shared by each thread. The default minimum value is 16MBand themaximum value is 64MB, which can be -xx:permsize and -xx:maxpermsize parameter limits the size of the method area.

Run a constant pool: is a part of the method area, In addition to the class file contains the version of the classes, fields, methods, interfaces, and other descriptive information, there is another information is a const pool, to hold the compiler generated a variety of symbol references, This section is placed in the run-time pool of the method area after the class is loaded.

2. Virtual Machine Stack

Describes the memory model that is executed by the Java method: Each method is executed with a "stack frame" for storing the local variable table ( including parameters ), the Operation Stack, Method exports and other information. Each method is called to the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack. The declaration period is the same as the thread and is thread-private.

The local variable table holds the various basic data types known to the compiler(Boolean,byte,Char, Short,int,float,Long,Double), object reference(reference pointer, not the object itself), where -bit length ofLongand theDoubletype of data will occupy2local variables, the rest of the data types only account for1A. The memory space required for a local variable table is allocated during compilation, and when entering a method, this method requires that the local variable allocated in the stack frame is fully deterministic, and the stack frame does not change the size space of the local variable table during the run time.

3. Local method Stack

is basically similar to a virtual machine stack, except that the virtual machine stack is performed for a virtual machine Java method Service, and the local method stack is the Native method Service.

4. Heap

Also calledJavaHeap,GCHeap isJavaThe largest chunk of memory in memory managed by a virtual machine is also an area of memory shared by each thread,JVMcreated at startup. This memory area holds object instances and arrays.(AllNewthe Object). Its size by-xms (Minimum Value)and the-xmx (Maximum Value)parameter settings,-xmsto beJVMminimum memory requested at startup, default is the operating system physical memory1/64but less than1G,-xmxto beJVMmaximum memory that can be requested, default to physical memory Quarterbut less than1G, default when free heap memory is less than40%when theJVMwill increaseHeapto the-xmxthe specified size, available through the-xx:minheapfreeration=to specify this than the column, when the free heap memory is greater than70%when theJVMwill decreaseHeapthe size to-xmsthe specified size, available through thexx:maxheapfreeration=to specify this comparison column, for running the system, to avoid frequent adjustments at run timeHeapthe size, usually-xmswith the-xmxis set to the same value.

Since collectors are now using generational collection algorithms, the heap is divided into the new generation and the old age. The new generation primarily stores newly created objects and objects that have not yet entered the old age. The old age stores the objects that survived by several new generation GC (Minor GC) .

Cenozoic:

the newly created object of the program is to allocate memory from the Cenozoic,Eden Spaceand two pieces of the same sizeSurvivor Space (usually also known asS0and theS1or fromand theto )composition, available through-xmnparameter to specify the size of the Cenozoic, or you can-xx:survivorrationto adjustEden SpaceandSurvivor Spacethe size.

Old Age:

It is used to store objects that survived by multiple generations of GC , such as cached objects, and new objects may go directly into the old age, mainly in two cases: ①. large objects, which can be set by startup parameters -xx:pretenuresizethreshold=1024 ( in bytes, default = 0) to represent more than the age of the allocation of the new generation, but directly in the old age distribution. ②. a large Array object that has no reference to the outer object in the tangent array.

Older generations accounted for the memory size of -xmx corresponding to the value minus -xmn corresponding value.

5. Program counter

is the smallest piece of memory area, its role is the current thread executes the byte code of the line number indicator, in the virtual machine model, the bytecode interpreter works by changing the value of this counter to select the next need to execute the byte code instruction, branch, loop, exception handling, Basic functions such as thread recovery need to rely on counters to complete.


JVM Loading process

In the Java language, classes are loaded into the JVM only to run, and when the specified java programis run, theJVM will compile the generated the . Class file is loaded into memory according to certain rules and organized into a complete application. The load process for a class is done by the ClassLoader (that is, by ClassLoader and its subclasses), and the ClassLoader itself is a class that essentially loads the class file from the hard disk into memory.

There are two ways to load a class:

(1) Explicit loading

by calling Class.forName () method to load the required classes into the JVM in

(2) Implicit loading

when a program creates a new object, it implicitly invokes the class loader to load the corresponding class into the JVM in

in the Java language, class loading is dynamic and flexible, often a large project contains many classes, and each class or interface corresponds to a . Class file, you only need to load the required class (the underlying class that guarantees the program to run, such as the base class) when the program is running to JVM , temporarily unwanted classes can not be loaded first, which can improve the speed of operation, on the other hand can also reduce the cost of memory when the program runs. Furthermore, each class file can be viewed as a dynamic loading unit, and when a project needs to be modified for a class, it needs to be recompiled to load the modified class instead of all of the classes being recompiled.

classes can be divided into three types: system classes, extension classes, custom classes, and Java different class loaders are available depending on the class

Bootstrap Loader <== Loading System class (jre/lib/rt.jar Class)

Extclassloader <== Load Extension class (jar/lib/etc/*.jar Class)

Appclassloader <== Loading the Application class (classpath The specified directory or The class in the jar)

Specific steps:

(1) First Java.exe will find the JREandfind the Jvm.dll, this is the real Java virtual machine, which is then loaded into the dynamic library to activate the Java virtual machine.

(2) Initialize the operation, after the end of the production Bootstrap Loader startup class loader

(3 bootstrap loader In addition to some basic initialization actions, the most important thing is to load extclassloader extension class loader, and set its parent null bootstrap loader

(4) then Bootstrap Loader again requires loading Launcher.java in the Appclassloader ( custom class loader )and set its Parent to Extclassloader entities, both of which exist in the form of static classes.

※ It is important to note that the parent is not directly related to who is being loaded.

We can test it:

Package test; public class ClassLoader {public static void main (string[] args) throws exception{classloader App = Classlo Ader.class.getClassLoader ();//classLoading DeviceSystem.out.println (APP); ClassLoader Ext = App.getparent ();//Previous Layer LoaderSystem.out.println (EXT); ClassLoader Boot = Ext.getparent ();//Root LoaderSystem.out.println (Boot); } }

Operation Result:

[Email protected]
[Email protected]
Null

Bootstrap Loader output null because it is implemented by the C + + language, so it is not visible in the Java language

Program Description ClassLoader This class is loaded by appclassloader

There are three main steps to loading a class:

(1) Load: Find the appropriate class file and import it according to the search path

(2) Link: check that the class file is correct --Assign storage space to static variables in the class-- convert a symbol reference to a direct reference

(3) Initialization: Initialization of static variables and static code blocks

Parental delegation Mechanism:

the parental delegate mode is the process by which a ClassLoader requests another classloader to load a type.

In addition to starting the ClassLoader for each classloader, there is a "parent" class loader, which, before a particular class loader attempts to load a class in a common way, will "delegate" the task to its parents by default, requesting its parents to load the class. The parents then asked their parents in turn to load the type. This delegated process continues up until the startup ClassLoader is reached, typically starting the ClassLoader is the last class loader in the delegation chain. If the parent class loader of a classloader has the ability to load this type. This class loader returns this type. Otherwise, the ClassLoader tries to load the class on its own.
When a program is running, the virtual machine instantiates two user-defined class loaders at startup : an "extension class loader" , One "custom class loader" . these class loaders and the Startup class loader join together in a Parent-child in the chain of Delegates , start the class loader at the top.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Java Advanced" JVM memory area model and loading process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.