当前位置:网站首页>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.
边栏推荐
猜你喜欢

全网没有之一的JMeter 接口测试流程详解
The general SQL injection flow (sample attached)

STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目

esp8266-01s刷固件步骤

用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓

There are too many systems, how to realize multi-account interworking?

sqoop ETL工具

数组相关 内容 解析

2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch

Sfdp 超级表单开发平台 V6.0.5 正式发布
随机推荐
Why use Selenium for automated testing
There are too many systems, how to realize multi-account interworking?
Shell 函数
TOML配置文件格式,YAML最有力的竞争者
sqoop ETL tool
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
汇编语言之栈
安装postgis时报找不到“POSTGIS_VERSION”这个函数
基于Qt的目录统计QDirStat
sqoop ETL工具
机器学习模型的“可解释性”
Architecture of the actual combat camp module three operations
力扣(LeetCode)215. 数组中的第K个最大元素(2022.08.03)
Mini program + new retail, play the new way of playing in the industry!
The general SQL injection flow (sample attached)
系统太多,多账号互通如何实现?
FFmpeg —— 录制麦克风声音(附源码)
Asynchronous programming solution Generator generator function, iterator iterator, async/await, Promise
查看mysql死锁语法
[Study Notes Dish Dog Learning C] Dynamic Memory Management