Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of running mechanism and memory mechanism in Java?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the principle of the operating mechanism and memory mechanism in Java. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

I. the operating mechanism of Java

The working principle and characteristics of JVM mainly means that the operating system loads JVM through Java.exe in jdk, and completes the JVM environment through the following four steps.

1. Create a JVM mount environment and configuration

two。 Mount JVM.dll

3. Initialize JVM.dll and hang it to JNIEnv (JNI calling API) instance

4. Call the JNIEnv instance to load and process the class class.

The entire life cycle of a class from the time it is loaded into the virtual machine class memory to the time it is unloaded from memory includes:

Load → verify → prepare → parse → initialize → unload using → (7 parts)

Load:

When 1.JDK executes the program running command, it will find jvm.dll in the JRE directory and initialize JVM, which will generate a Bootstrap Loader (start classloader).

2.Bootstrap Loader automatically loads Extended Loader (standard extension classloader)

3.Bootstrap Loader automatically loads AppClass Loader (system class loader)

4. Finally, AppClass Loader loads the java class that we specify (want to run)

Verify:

File format verification-> metadata verification-> bytecode verification-> symbol reference verification

1. File format verification: mainly to check whether the byte stream of the bytecode conforms to the specification of the Class file format, and to verify whether the file can be processed by the current jvm

If there is no problem, the byte can be entered into the method area to save.

two。 Metadata verification: semantic analysis of the information described by bytecode to ensure that its description conforms to the syntax specification of java language and can be recognized by java virtual machine

3. Bytecode verification: this part is the most complex and verifies the contents of the method to ensure that the code does not make any events that endanger the security of the virtual machine.

4. Symbol reference verification: to verify the authenticity and feasibility of some references, for example, other classes are cited in the code (whether the corresponding class can be found through the fully qualified name described by the string). Here is to check whether those exist; or to access some properties of other classes in the code, and the accessible lines of those attributes are verified here.

Prepare:

The preparation phase is a class variable (static variable, which is what we often say, static variable / method is executed when the class is loaded, through the class name. Static * * to call) allocates memory and sets the initial value of the class; it is worth mentioning that if there is the following statement:

Public static int i = 123

In the preparation phase, the initial value is 0, not 123, because only memory space is allocated and I is not initialized. The real I assignment is in the initialization phase.

Parsing:

1. Parsing of classes or interfaces

two。 Field parsing

3. Class method analysis

4. Analysis of interface method

Initialization: (can also be performed before parsing)

The class initialization phase is the last step in the class loading process, which is the execution of the java program code (bytecode) defined in the class. In the preparation stage, the variable has been assigned the initial value required by the system, and the initialization variable assignment will be given according to the programmer's requirements in the initialization phase.

1. When you encounter the four bytecode instructions of new,get static,put static,invoke static, if the class has not been initialized, initialize it immediately.

two。 When you use java.lang.reflect.* 's method to make a reflection call to a class, if the class has not been initialized, initialize it immediately at the speed of light!

3. When initializing a class, if its parent class has not been initialized, it will initialize its parent class first

Second, the memory structure of java

Memory occupancy in java is mainly divided into four blocks: heap, stack, method area and program counter, of which heap and stack are the most frequently used.

Heap: a heap is a run-time data area that mainly stores objects and arrays from new.

Stack (stack): the stack mainly holds some basic data types and object reference variables.

Method area:

1. Constant pool: store some string constants and basic type constants. 2. Static area: there is an area allocated when the program is compiled, mainly storing some static variables (static); 3. Code area: the binary code of the program method is stored, and multiple objects share a code space area.

Program counter: record the next instruction of the program

The operational tact and advantages and disadvantages of the stack

The memory allocated in the heap is dynamically allocated at run time, so the lifetime does not have to tell the compilation area, and it is reclaimed by the garbage collection mechanism of the java virtual machine. The disadvantage: because it is dynamically allocated memory, so the access speed is slow. The heap can be understood as a complete binary tree.

The advantage of the stack is that when a variable is defined in a block of code, Java allocates memory space for the variable in the stack. When the scope of the variable is exceeded, Java will automatically release the memory space allocated for the variable, which can be immediately used for other purposes. The advantage is that the access speed is fast, second only to the registers in CPU, and the data can be shared. Disadvantages: data size and lifetime must be determined, lack of flexibility

Stack stores large content: object instance size is limited, LIFO, storage: references and basic types are slow, fast all threads share, life cycle and JVM synchronization

Each thread has its own stack, which is destroyed when it is destroyed.

III. Memory management mechanism of java

In the final analysis, the purpose of JVM memory management mechanism is to solve two problems: allocating memory to objects and reclaiming the memory allocated to objects.

Allocation of Java variables in memory

1. Class variables (variables modified by static): when the program is loaded, the system opens up memory for it in the heap, and the memory address in the heap is stored on the stack for high-speed access. The life cycle of static variables-continues until the entire "system" shuts down.

2, instance variables: when you use the java keyword new, the system opens up in the heap is not necessarily continuous space allocated to variables (such as class instances), and then according to the scattered heap memory address, converted into a long string of numbers through the hash algorithm to represent the variable's "physical location" in the heap. Life cycle of instance variables-when a reference to an instance variable is lost, it is added to the recyclable "list" by the GC (garbage collector), but does not immediately release memory in the heap.

3, local variables: local variables, declared in a method, or a code segment (such as for loop), open up memory in the stack when it is executed. When the local variable is out of scope, memory is immediately released.

4. String str = new String ("abc") and String str = "abc"

When generating a string through new, first go to the constant pool to see if there is a "abc" object, if not, create a "abc" object in the constant pool, and then create a copy object of "abc" in the constant pool in the heap

The second is to first create a reference to the reference variable str of the String object in the stack, and then find out if there is an address in the stack with the value of "abc". If not, create an address with the literal value of "abc", and then create a "abc" object in the constant pool and point to this address.

Different object reference types are recycled by GC in different ways. References to JVM objects are divided into four types:

(1) strong references: by default, objects use strong references (the instance of this object has no other object references, so GC will be recycled)

(2) soft reference: soft reference is an application provided in Java that is suitable for caching scenarios (it will be GC only if there is not enough memory)

(3) weak reference: it must be reclaimed by GC during GC

(4) Virtual reference: because virtual reference is only used to know whether an object is GC or not.

Garbage collection (GC, full name: garbage colletion)

(1) when will it be recycled?

When garbage collection occurs when memory is insufficient or currently idle, the priority of GC threads is not too high.

(2) to judge what is garbage?

Generally speaking, an object that does not have a reference to it is marked as garbage, and no object points to it, so it cannot be manipulated. This object is useless to us.

(3) garbage collection method

Tag removal algorithm, replication algorithm, tag finishing algorithm and generation recovery algorithm

Label removal algorithm

The algorithm is divided into two stages: "marking" and "clearing": the task of the marking phase is to mark all the objects that need to be recycled, and the clearing phase is to recover the space occupied by the marked objects. It is the most basic collection algorithm and is very efficient, but it brings two obvious problems:

Efficiency problem

Space problem (a large number of discontinuous fragments will be produced after the mark is cleared)

Replication algorithm

Divide the memory into two blocks of the same size, one of which is used each time. When the first block of memory is used up, copy the surviving objects to another block, and then clean up the used space at once. This makes it possible to reclaim half of the memory interval for each memory collection.

The advantages and disadvantages of this algorithm are also obvious.

Advantages: to solve the problem of fragmentation, sequential memory allocation is simple and efficient

Disadvantages: it is only suitable for scenes with low survival rate. In extreme cases, if all the objects on the object surface survive, half of the storage space will be wasted.

Tag finishing algorithm

In order to solve the shortcomings of the replication algorithm and make full use of the memory space, a tag finishing algorithm is proposed. The marking phase of the algorithm is the same as mark removal, but after completing the tag, it does not directly clean up the recyclable objects, but moves all the surviving objects to one end, and then cleans up the memory beyond the end boundary. As shown in the following figure:

Generation collection algorithm

In the heap, the generation collection algorithm is used, which is to select the specific garbage collection algorithm according to the specific situation. Generally, the java heap is divided into new generation and old age (Young Generation and Old Gereration), so that we can choose the appropriate garbage collection algorithm according to the characteristics of each age.

The new generation

In most cases, objects are allocated in the new generation Eden zone, but some large objects may go straight into the old age. The virtual machine provides a-XX:PretenureSizeThreshold parameter that allows objects larger than this setting to be allocated directly into the old age, in order to avoid a large number of memory copies between the Eden zone and the two Survivor zones.

When the new generation is full, Garbage Collection (GC) is executed, and the GC at this time is called Minor GC.

JVM executes Minor GC, and the referenced objects will survive, and they will be moved to the Survivor area, that is, S0 or S1 in the figure.

Two Survivor areas at the same time, one is used to save objects, the other is empty; each time Minor GC garbage collection, the reachable objects of Eden,From are copied to the To area, and some of those with a long survival time are copied to the old age, then the Eden,From space is cleared, and finally the original To space is changed into From space, and the original From space is changed into To space.

If the object is still alive after the Eden was born and entered the first Minor GC, and can be accommodated by the Survivor, it will be moved to the Survivor space, and the age of the object will be set to 1. Each time the object survives the Minor GC in the Survivro area, the age will increase by 1 year, and when its age increases to a certain extent (the default is 15), it will be promoted to the old age. The age threshold for the promotion of an object can be set by the parameter-XX:MaxTenuringThreshold.

If the sum of all the objects of the same age in the Survivor space is more than half of the Survivor space, the objects whose age is greater than or equal to that age can directly enter the old age without waiting for the age required in MaxTenuringThreshold.

Most of the objects in the area die day and night, so the Minor GC recycling frequency is high and the recovery speed is very fast. Each garbage collection can be completed by paying only a small amount of object replication cost. So the replication algorithm is adopted.

Old age

Older objects have a high chance of survival, and there is no extra space to allocate guarantees for it, so we must choose the "mark-clear" or "mark-clean" algorithm for garbage collection. And the recovery frequency is not high.

Object source:

The big object went straight into the old age.

Reachable objects that live for a long time in the Young generation.

Permanent generation

Heap = Cenozoic + old age, excluding permanent generation (method zone).

Jvm permanent generation (method area), constant pool and variable storage area, garbage collection will not occur in the permanent generation, when the permanent generation storage is full or exceeds the critical value, complete garbage collection (full gc) will be triggered; after the permanent generation has been removed, the memory meta-space area (local memory area) has been removed; the maximum available space has become the available space of the entire system memory.

In JDK 1.8, HotSpot no longer has the "PermGen space" interval and is replaced by something called Metaspace (metaspace). Let's look at the difference between Metaspace and PermGen space.

The reason for removing the permanent generation

Removing permanent generations is an effort to integrate HotSpot JVM and JRockit VM, because JRockit does not have permanent generations and does not need to configure permanent generations.

The size of the permanent generation is uncertain, and the small size specified by PermSize can easily lead to permanent OOM, because the size of the PermSize depends on many factors, such as the total number of class loaded by JVM, the size of the constant pool, the size of the method, and so on.

IV. Memory tuning

1. New and old generation overflow: java.lang.OutOfMemoryError:java heep space; will throw this exception message when 98% of the time is spent on garbage collection and the available Heap size is less than 2%.

Solution: manually set the size of the JVM Heap (heap)

2. Persistent generation overflow: java.lang.OutOfMemoryError: PermGen space

Solution: set the permanent generation size through-XX:PermSize and-XX:MaxPermSize.

3. Stack overflow: java.lang.StackOverFlowError:Thread stack space

The stack area is much smaller than the stack area, and the memory required by the stack area is about 1-2m. The stack overflow indicates that the memory required by a single thread to run the program is too large.

Solution: 1: modify the program. 2: use-Xss: to set the Stack size of each thread.

4. Java has its own analysis tools:

Jstack (View Thread), jmap (View memory), and jstat (performance Analysis) commands

Parameter description

-Xmx3550m: set the maximum heap memory of JVM to 3550m.

-Xms3550m: sets the initial heap memory of JVM to 3550m. This value can be set to the same as-Xmx to prevent JVM from reallocating memory after each garbage collection.

-Xss128k: sets the stack size for each thread. The stack size of each thread after JDK5.0 is 1m, and the stack size of each thread before is 256K. It should be adjusted according to the amount of memory required by the applied thread. Decreasing this value generates more threads under the same physical memory. However, the operating system still has a limit on the number of threads in a process, which cannot be generated indefinitely, and the experience value is about 3000 to 5000. It is important to note that when this value is set to a large value (for example, > 2MB), it will greatly degrade the performance of the system.

-Xmn2g: set the size of the younger generation to 2G. When the entire heap memory size is determined, increasing the younger generation will reduce the older generation, and vice versa. This value is related to JVM garbage collection and has a great impact on system performance. It is officially recommended that it be configured as 3max 8 of the entire heap size.

-XX:NewSize=1024m: set the initial value of the younger generation to 1024m.

-XX:MaxNewSize=1024m: set the maximum value of the younger generation to 1024m.

-XX:PermSize=256m: sets the initial value of persistent generation to 256m.

-XX:MaxPermSize=256m: set the maximum persistent generation value to 256m.

-XX:NewRatio=4: sets the ratio of the younger generation (including 1 Eden and 2 Survivor zones) to the older generation. It means that the younger generation is 1:4 than the older generation.

-XX:SurvivorRatio=4: sets the ratio of Eden region to Survivor region in the younger generation. Indicates that the ratio of 2 Survivor zones (there are 2 equal-sized Survivor regions by default in the younger generation of JVM heap memory) to 1 Eden zone is 2:4, that is, 1 Survivor zone accounts for 1 Survivor 6 of the entire young generation size.

-XX:MaxTenuringThreshold=7: indicates that an object enters the old generation after moving in the Survivor area (salvage space) for 7 times without being garbage collected. If set to 0, the younger generation will enter the older generation without going through the Survivor area, which can improve efficiency for applications that require a lot of resident memory. If you set this value to a large value, the younger generation objects will be replicated multiple times in the Survivor area, which can increase the survival time of the object in the younger generation, increase the probability that the object will be garbage collected in the younger generation, and reduce the frequency of Full GC, which can improve the stability of the service to some extent.

The above is what is the principle of operating mechanism and memory mechanism in Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report