当前位置:网站首页>Introduction to the memory model of the JVM
Introduction to the memory model of the JVM
2022-08-04 03:32:00 【xmh - sxh - 1314】
JVM memory model can be divided into two parts, as shown in the figure below, heap and methodAreas are common to all threads, while virtual machine stacks, native method stacks, and program counters are thread-private.
1. Heap
Heap memory is shared by all threads and can be divided into two parts: young generation and old generation.Perm in the figure below represents the permanent generation, but note that the permanent generation is not part of the heap memory, and the permanent generation will also be removed after jdk1.8.
The heap is the largest memory area in the memory managed by the Java virtual machine, and it is also a memory area shared by various threads. This memory area stores object instances and arrays (but not all object instances are in the heap).Its size is set by the -Xms (minimum value) and -Xmx (maximum value) parameters (the maximum and minimum values are both less than 1G). The former is the minimum memory requested at startup, and the default is 1/64 of the physical memory of the operating system.The maximum memory that the JVM can apply for. The default is 1/4 of the physical memory. By default, when the free heap memory is less than 40%, the JVM will increase the heap memory to the size specified by -Xmx, which can be specified by -XX:MinHeapFreeRation=Ratio; when the free heap memory is greater than 70%, the JVM will reduce the size of the heap memory to the size specified by -Xms. This ratio can be specified by XX:MaxHeapFreeRation=. Of course, in order to avoid frequent adjustment of the Heap size at runtime, usually -Xms and -Xmx are set to the same value.Heap memory = young generation + old generation + persistent generation.When we garbage collect, we often divide the heap memory into the new generation and the old generation (the size ratio is 1:2). The new generation consists of Eden, Survivor0, and Survivor1. The ratio of the three is 8:1:1, and the new generationThe recovery mechanism adopts the replication algorithm. During the Minor GC, we all reserve a survival area to store the surviving objects. The real area is Eden + one of the survival areas. When the duration of our objects exceeds a certain age (default 15, you canBy parameter setting), the object will be put into the old generation, of course, large objects will directly enter the old generation.The recycling algorithm used in the old generation is the mark sorting algorithm.
2. Method Area
The method area is also called "permanent generation", which is used to store class information, constants, and static variables loaded by the virtual machine, and is a memory area shared by each thread.The default minimum value is 16MB, and the maximum value is 64MB (64-bit JVM defaults to 85M due to pointer expansion). The size of the method area can be limited by the -XX:PermSize and -XX:MaxPermSize parameters.It is a continuous heap space, and the permanent generation garbage collection is tied to the old generation (old generation), so no matter who is full, it will trigger the permanent generation and old generation garbage collection.However, an obvious problem is that when the class information capacity loaded by the JVM exceeds the value set by the parameter -XX:MaxPermSize, the application will report an OOM error.Parameters are set via -XX:PermSize and -XX:MaxPermSize.
3. JVM Stack
Describes the memory model of java method execution: when each method is executed, a "stack frame" is created to store the local variable table (including parameters), operation stack, method exit and other information.Each method is called to the process of execution, which corresponds to the process of a stack frame from being pushed to the stack in the virtual machine stack.The declaration period is the same as the thread and is thread-private.The stack frame consists of three parts: the local variable area, the operand stack, and the frame data area.The local variable area is organized as a word-length array that starts counting from 0. Like the local variable area, the operand stack is also organized as a word-length array.But the difference from the former is that it is not accessed by index, but accessed by pushing and popping the stack, which can be regarded as a storage area for temporary data.In addition to the local variable area and operand stack, the java stack frame also needs some data to support constant pool parsing, normal method return and exception dispatch mechanism.These data are stored in the frame data area of the java stack frame.
Local variable table: Stores various basic data types and object references known to the compiler (reference pointers, not objects themselves), of which 64-bit long and double type data will occupy the space of 2 local variables,The rest of the data types only account for 1.The memory space required for the local variable table is allocated during compilation. When entering a method, how much local variables the method needs to allocate in the stack frame is completely determined, and the stack frame does not change the size of the local variable table during runtime.space.
4. Native Stack
It is basically similar to the virtual machine stack, the difference is that the virtual machine stack serves the java method executed by the virtual machine, while the local method stack serves the native method.(The size of the stack is much smaller than the heap)
5. Program Counter (PC Register)
is the smallest memory area, and its function is the line number indicator of the bytecode executed by the current thread. In the virtual machine model, the bytecode interpreter works by changing the value of this counter toTo select the next bytecode instruction to be executed, basic functions such as branching, looping, exception handling, and thread recovery all need to rely on the counter to complete.
6. Direct memory
Direct memory is not part of virtual machine memory, nor is it a memory area defined in the Java Virtual Machine specification.The newly added NIO in jdk1.4 introduces the IO method of channels and buffers. It can call the Native method to directly allocate the off-heap memory. This off-heap memory is the native memory and will not affect the size of the heap memory.
边栏推荐
猜你喜欢
随机推荐
机器学习模型的“可解释性”
Brush esp8266-01 s firmware steps
2022杭电多校联赛第五场 题解
从图文展示到以云为核,第五代验证码独有的策略情报能力
6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
Functions, recursion and simple dom operations
马尔可夫链
keytool命令
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
XSS相关知识点
基本表单验证流程
力扣(LeetCode)215. 数组中的第K个最大元素(2022.08.03)
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
多线程间的通信方式你知道几种?
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
Hey, I had another fight with HR in the small group!
数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
如何在MySQL中的数据库下删除所有的表

![[Study Notes Dish Dog Learning C] Dynamic Memory Management](/img/57/a1837e5eec2af0e684e5895a8afed0.png)






![出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]](/img/e0/32d78fac04dc2deb1cb1f847a7bab5.png)
