In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to implement memory distribution in JAVA? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
The memory distribution of java is as follows:
1, local method stack
2, program counter
3, virtual machine stack (stack frame 1 (method A), stack frame 2 (method B))
4, stacking area (Cenozoic (Eden area, S0Mague S1...), old age)
5, metadata area (constant pool, method meta information, class meta information)
As shown in the following figure:
The corresponding details of each region are as follows:
1, stacking area:
Almost all the instance objects are stored, the heap is automatically collected by the garbage collector, and the heap area is shared by each child thread.
The memory space of the heap can be either fixed or dynamically adjusted at run time. The initial and maximum values are set by the following parameters, such as-Xms256M-Xmx1024M, which represent the minimum heap capacity and the maximum heap capacity, respectively. In the online environment, the Xms and Xmx of JVM are set to the same size to avoid the extra pressure caused by adjusting the heap size after GC.
The heap is divided into two parts: the Cenozoic era and the old age, the object was born in the Cenozoic era and entered the old age, but the old era also accepted the super-large objects which could not be accommodated in the Cenozoic era, the Cenozoic era = 1 Eden region + 2 Survivor regions. Most of the objects are generated in the Eden area. When the Eden area is full, the Young Garbage Collection will be triggered, and each object will have a counter. Each YGC will add 1. When the value of the counter reaches a certain threshold, the object will be promoted from the new generation to the old age.
Set the parameter-XX:+HeapDumpOnOutOfMemoryError to JVM so that JVM can output in-heap information when it encounters an OOM exception.
2, virtual machine stack:
Stack is an advanced data structure.
JVM is a running environment based on stack structure. The virtual machine stack in JVM is the memory area that describes the execution of java methods, which is private to threads. The process of each method from call to execution is the process of stack frame from stack to unstack.
Stack frame is the basic structure of method running. when the execution engine is running, all instructions can only operate on the current stack frame. StackOverFlowError represents the stack overflow of the request, resulting in memory exhaustion, which usually occurs in recursive methods.
3. Local variable scale:
The local variable table is the area where the method parameters and local variables are stored.
4. Operation stack:
The operation stack is a bucket structure stack with an empty initial state.
5. Local method stack:
The local method stack serves Native methods, and when you start calling local methods, you enter a world that is no longer constrained by JVM.
6, program count register:
Due to the time wheel limitation of CPU, many threads will only execute one instruction in a thread at any given time, a processor or a core in a multi-core processor. Thread execution and recovery depend on program counters.
Garbage collection algorithm:
1Mark Mark sweep (Mark-erase) algorithm
First mark all the objects that need to be recycled, and then uniformly recycle the marked objects
Disadvantages: the marking and removal process is inefficient
Marking and clearing will result in a large number of discontiguous memory fragments, and when the program needs to allocate larger objects, it will trigger a garbage collection action in advance because it cannot find enough continuous memory.
2) copy (copy) algorithm
Divide the memory into two equal blocks and use only one of them at a time. When one piece of memory is used up, the surviving objects are copied to the other, and then the last piece of space is cleaned up directly.
Because 98% of the objects in the new generation die overnight, the memory is divided into three parts, Eden,S1, S2, according to the proportion of 8:1:1. First, the Eden,S1 is used, then the surviving objects are copied to another S2, and then when Eden and S2 are exhausted, they are copied to S1, and so on.
3Marktel Compact (marking-finishing) algorithm
First mark the objects that need to be cleared, then move all the surviving objects to one end, and then clear the memory outside the boundary.
4Gen generational Collection algorithm
The java heap is divided into the new generation and the old era, and the most appropriate algorithm is adopted according to the characteristics of each era.
Garbage collector:
1,Serial/Serial Old
The Serial/Serial Old collector is the most basic and oldest collector, it is a single-threaded collector, and all user threads must be paused when it does garbage collection.
Serial collector is aimed at the new generation of collectors, using Copying algorithm, Serial Old collector is aimed at the old collectors, using Mark-Compact algorithm.
It has the advantage of simple and efficient implementation, but the disadvantage is that it will bring pause to the user.
2,ParNew
The ParNew collector is a multithreaded version of the Serial collector that uses multiple threads for garbage collection.
3,Parallel Scavenge
Parallel Scavenge collector is a new generation of multithreaded collector (parallel collector), it does not need to pause other user threads during collection, it uses the Copying algorithm, this collector is different from the previous two collectors, it is mainly to achieve a controllable throughput.
4,Parallel Old
Parallel Old is an older version of the Parallel Scavenge collector (parallel collector) that uses multithreading and Mark-Compact algorithms.
5,CMS
CMS (Current Mark Sweep) collector is a kind of collector whose goal is to obtain the shortest recovery pause time. It is a concurrent collector using Mark-Sweep algorithm.
6,G1
G1 collector is the most cutting-edge achievement in the development of collector technology. It is a server-oriented collector, which can make full use of multi-CPU and multi-core environment. Therefore, it is a parallel and concurrent collector, and it can build a predictable pause time model.
The details of each collector are as follows:
This is the answer to the question about how to achieve memory distribution in JAVA. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.