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

Jvm memory Model and allocation parameters

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Jvm memory model

Program counter: is a very small memory space. When the number of threads exceeds the number of cpu, threads snatch cpu resources according to time slice polling. Each thread must use a separate program counter to record the next instruction to run.

Java virtual machine stack (thread stack): it is also the thread's private memory space, which is created at the same time as the java thread. It saves the local variables and partial results of the method, and participates in the call and return of the method. If the requested stack depth of the thread is greater than the maximum available stack depth during the calculation, StackOverflowError; is thrown. If the java stack can be dynamically expanded, and in the process of expansion, the operating system does not have enough memory space to support stack expansion, then throw OutOfMemoryError;-- parameter settings see "setting thread stack"

Local method stack: used to manage calls to local methods, which are implemented in C.

Java heap: the heap space is divided into Cenozoic era and old age. The new generation is used to store new objects, while in the old era, it is used to store older objects (objects that have existed for a long time and have been garbage collected more times). The Cenozoic can be subdivided into eden,s0 and s1Magol Eden. When most objects are first created, they are usually placed here. S0 and S1 are survivor spaces, which means that the objects stored in them have undergone at least one garbage collection and survived. If the object in the survival area is not recycled by the specified age, there is a chance to enter the old age.

Method area: similar to heap space, it is also shared by all threads of jvm. It mainly stores metadata such as class type information, constant pool, domain information, method information, and so on.

Set maximum and minimum heap memory

Use-Xmx to specify the maximum heap memory, which refers to the maximum sum of the sizes of the new generation and the old, which is the stack line of java applications.

Use-Xms to specify the minimum heap memory, that is, the amount of operating system memory occupied when jvm starts.

When the memory size specified by-Xms does not satisfy the application, jvm requests more memory from the operating system until it reaches the memory size specified by-Xmx. If the value of-Xms is small, then in order to ensure that the system runs within the specified memory range as much as possible, jvm will frequently carry out gc operations to release the invalid memory space, thus increasing the number of Minor GC and Full GC, which will have a certain impact on system performance. Therefore, when you set the-XMs value to-Xmx, you can reduce the number of gc and time-consuming at the beginning of the system operation.

Set thread stack

The thread stack is a private space of the thread. The size of the thread stack can be set with-Xss. The size of the stack directly determines the reachable depth of the function call.

Local variable allocation in the thread and function calls all need to open up space in the stack.

If the stack space allocation is too small, then the thread may not have enough space to allocate local variables or function call depth enough to cause the program to exit abnormally; if the stack space is too large, then the memory cost required to set up threads will rise, and the total number of threads that the system can support will decrease.

Both the java heap and the thread stack are like the operating system requesting memory space. If the heap space is too large, it will cause the operating system to reduce the memory available for the thread stack. When the system cannot create a new thread due to insufficient memory, it will throw an OOM exception (note that the OOM is not out of heap memory). So, if the system does need a large number of threads to execute concurrently, setting a smaller heap and a smaller stack helps to increase the maximum number of threads the system can bear.

Set up the new generation

Use-Xmn to specify the size of the Cenozoic generation, which is generally set to 1max 4 to 1max 3 of the entire heap space. Setting a larger or smaller new generation has a great impact on system performance and gc behavior.

Use-XX:NewSize to set the initial size of the new generation, and-XX:MaxNewSize to set the maximum size of the new generation. Typically, just set-Xmn to meet your needs.

Set persistent generation

Persistent generation (method area) is not part of the heap, use-XX:MaxPermSize to set the persistent generation maximum value and use-XX:PermSize to set the persistent generation minimum value.

The size of the persistent generation directly determines how many class definitions and constants the system can support.

The general MaxPermSize is set to 64m or 128m.

Take a heap snapshot (heap dump)

Use the-XX:+HeapDumpOnOutOfMemoryError parameter to export the current heap snapshot of the application when OOM occurs in the program.

The parameter-XX:HeapDumpPath allows you to specify where to save the heap snapshot.

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

Network Security

Wechat

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

12
Report