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

How to use the JVM memory model

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you how to use the JVM memory model, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Whether you are familiar with the JVM memory model, here to share with you, mainly includes stack and heap two parts, Java stack is associated with each thread, JVM in the creation of each thread, will allocate a certain stack space to the thread. It is mainly used to store local variables during thread execution, the return value of the method, and the method call context. A heap in Java is an area of memory shared by all threads. The heap is used to hold various JAVA objects, such as arrays, thread objects, and so on.

JVM memory model

1.1Java stack

The Java stack is associated with each thread, and JVM allocates a certain amount of stack space to each thread when it is created. It is mainly used to store local variables during thread execution, the return value of the method, and the method call context. Stack space is freed as the thread terminates. StackOverflowError: if there is not enough stack space during thread execution, JVM will throw this exception, which is usually caused by dead recursion.

1.2 heap

A heap in Java is an area of memory shared by all threads. The heap is used to hold various JAVA objects, such as arrays, thread objects, and so on.

1.2.1Generation

Generally speaking, the JVM heap in the JVM memory model can be divided into the following three parts:

◆ Perm

The Perm generation mainly saves class,method,filed objects, and the space in this department generally does not overflow, unless a lot of classes are loaded at one time, but when it comes to hot-deployed application servers, java.lang.OutOfMemoryError:PermGenspace errors are sometimes encountered. The big reason for this error may be that it is redeployed every time, but the class of the class is not unloaded after redeployment. This results in a large number of class objects being saved in perm, in which case restarting the application server can generally solve the problem.

◆ Tenured

The Tenured area mainly stores objects with a long life cycle, usually some old objects. When some objects are copied and transferred in Young for a certain number of times, the objects will be transferred to the Tenured area. Generally, if the application-level cache is used in the system, the objects in the cache will often be transferred to this area.

◆ Young

The Young area is divided into three parts, the Eden area and two Survivor zones of exactly the same size. In the Survivor area, only one of them is used at a certain time, and the other one is reserved for garbage collection to copy objects. When the Young interval becomes full, minorGC will move the surviving objects to the free Survivor interval. According to JVM's strategy, after several garbage collection, the objects still alive in Survivor will be moved to the Tenured interval.

1.2.2SizingtheGenerations reference:

Parameters are provided in the JVM memory model to configure the memory size. As described above, the heap in JVM is divided into three large intervals, and JVM also provides some options to control the size of the Young,Tenured.

◆ TotalHeap

-Xms: specifies that memory is initialized after the initial startup of JVM

-Xmx: specifies that the JVM heap * * memory. After JVM starts, the memory specified by the-Xmx parameter will be allocated to JVM, but not all of it will be used. JVM will adjust the memory reference for JVM according to the-Xms parameter:

-the difference between Xmx-Xms is the size of three Virtual spaces.

◆ YoungGeneration

-XX:NewRatio=8 means the ratio of tenured to young is 8:1, so eden+2*survivor=1/9

Heap memory

-XX:SurvivorRatio=32 means that the ratio of eden to a survivor is 32:1, so a Survivor accounts for 1 eden 34 in the Young region.

-Xmn parameter sets the size of the younger generation.

◆ PermGeneration

-XX:PermSize=16M-XX:MaxPermSize=64M

ThreadStack

-XX:Xss=128K

1.3 benefits of stack separation

Aside from the rest, let's talk about object-oriented design. Of course, in addition to the maintainability, reusability and extensibility benefits of object-oriented design, let's see how object-oriented takes advantage of stack separation. If we understand the object-oriented design from the perspective of the JAVA memory model, we will find that the object represents the heap and the stack, and the data of the object is placed in the heap, and the methods we write generally run on the stack, so object-oriented design is a very * design method, which unifies the data storage and operation.

On how to use the JVM memory model to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report