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 realize memory recovery in Java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to achieve memory recovery in Java, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Java memory region method area, metaspace

The memory area shared by each thread is used to store data such as class information, constants, static variables, code compiled by just-in-time compiler, which have been loaded by the virtual machine. The method area is sometimes called the permanent generation. In JDK1.8 and later, the permanent generation was cancelled and replaced by metaspace. Tuning in the permanent generation is very difficult, and the effect is not obvious. The size of the permanent generation is limited by the memory of the JVM itself, while the metaspace is directly used by the machine's memory and is limited only by the system memory. The default maximum size of metaspace is unlimited. Some parameters:

-XX:PermSize=N / / initial size of method area-maximum size of XX:MaxPermSize=N / / method area-initial size of XX:MetaspaceSize=N / / metaspace-maximum size heap of XX:MaxMetaspaceSize=N / / original space

The heap is the largest piece of memory managed by the Java virtual machine, which is shared by all threads, and the sole purpose of this memory area is to hold object instances. The heap is the main area managed by the garbage collector. In HotSpot, memory is divided into the new generation and the old age in most cases, and the default allocation ratio is 1:2. In the Cenozoic era, it was further divided into one Eden and two Survivor, with a distribution proportion of 8:1:1. The age of the subjects in the new generation will be + 1 after a Minor GC, and will enter the old age when the age reaches 15 (the default). Some parameters:

-XX:InitialHeapSize=N / / initial heap size-Xms1024m / / abbreviated value-maximum of XX:MaxHeapSize=N / / heap-Xmx1024m / / abbreviated value-XX:MaxTenuringThreshold=15 / / the age stack area in which the new generation enters the old age

Threads are private and are used to store local variables, Operand stacks, dynamic links, method exits, and other information. Contains program counters, virtual machine stacks, and local method stacks.

Garbage collection algorithm

Which objects need to be recycled?

The reference counting algorithm gives the object a reference counter, which adds 1 whenever a place references it, and subtracts 1 when the reference expires. Any object with a counter of 0 is an unused object. Cons: it is difficult to solve the problem of circular references.

The reachability analysis algorithm uses a series of "GC Roots" as the starting point to search down from these nodes, and the path reached is called the reference chain. When an object is not in any reference chain, the object is not used.

When will it be recycled? When searching from GC Roots in the reachability analysis algorithm, the consistency of references must be guaranteed so that the reference relationship of objects does not change. This causes GC to stop all execution threads (Stop The World). Use OopMap in HotSpot to record the call information. The place where the OopMap is recorded in the code is called SafePoint. When GC occurs, you need to have all threads run to SafePoint before performing the GC operation.

How to recycle?

The tag-clearing algorithm is the same as the name, and this method is divided into two stages: "tag" and "clear". First, a tag is added to the objects that are not used, and then all the marked objects are uniformly recycled. Disadvantages:

Both marking and clearing phases are inefficient.

After recycling, a large number of discontinuous memory fragments will be generated, making it difficult to store larger objects in the future.

The replication algorithm divides the object into two pieces, and when one object runs out, it copies the object that is still in use to the other. It has a higher efficiency disadvantage than the mark-clear algorithm: only one piece of memory can be used at a time, which makes the utilization of memory lower.

The first half of the mark-demarcation algorithm is the same as the mark-cleanup algorithm, but then all the surviving objects are moved to one end, clearing the memory fragments.

According to the different survival cycle of the object, the generation collection algorithm generally divides the object into the new generation and the old age, and uses different collection algorithms according to the characteristics of each age. For the new generation, a large number of objects die every time, so the replication algorithm is adopted. For the old age, the survival rate of the object is high, and the mark-removal or mark-arrangement algorithm is adopted.

Garbage collector

If the collection algorithm is the methodology of memory collection, then the garbage collector is the concrete implementation of memory collection.

Serial is one of the most basic and oldest collectors. The collection method is single-threaded, and all other worker threads must be paused at the time of collection until the collection is finished. It has a good effect in Client mode.

The ParNew ParNew collector is a multithreaded version of Serial that works with CMS.

Parallel Scavenge throughput priority collector. (throughput = user code run time / (user code run time + garbage collection time))

An old version of Serial Old Serial

In the old version of Parallel Old Parallel Scavenge, Parallel Scavenge + Parallel Old is used as the default Cenozoic and old recycler in JDK1.7 and JDK1.8.

CMS is sensitive to CPU resources in order to minimize the recovery pause time.

The default garbage collector in G1 JDK1.9, the main concern of G1 is to achieve controllable pause time, based on which the throughput can be improved as much as possible. Each block in G1 also acts as Eden, Survivor, and Old, but they are not fixed, which makes memory usage more flexible.

Memory allocation strategy

In HotSpot, memory is divided into the new generation and the old age in most cases, and the default allocation ratio is 1:2. In the Cenozoic era, it was further divided into one Eden and two Survivor, with a distribution proportion of 8:1:1. A new object is generally allocated in the Cenozoic Eden area. When there is not enough space in the Eden area to allocate, a Minor GC will be initiated. For large objects (Java objects with a lot of contiguous memory space), it goes straight into the old age. Long-term surviving objects (who survive 15 Minor GC times by default) will enter the old age. If the objects of the same age in Survivor exceed the average of Survivor, these objects will go straight into the old age.

JDK Command Line

Some commands for monitoring virtual machine status and fault handling

Command function jps displays all virtual machine processes in the system jstat is used to collect virtual machine running data jinfo displays virtual machine configuration information jmap generates virtual machine memory dump snapshot (heapdump file) jhat is used to analyze heapdump files jstack display virtual machine thread snapshots does it help you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report