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

What is the method to deal with the problem of memory recovery in JVM

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What is the method to deal with the problem of memory recovery in JVM? aiming at 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 easy way.

Let's focus on the solution to the problem of JVM memory recovery. Generally speaking, JVM memory recovery always refers to heap memory recovery. Indeed, only the content in the heap is dynamically applied for allocation, so the younger and older generations of the above objects refer to the Heap space of JVM, while the persistent generation refers to the MethodArea mentioned earlier and does not belong to Heap.

JVM memory recovery

To understand the system structure of JVM, let's take a look at the memory recovery problem of JVM--

Sun's principle of JVMGenerationalCollecting (garbage collection) is that objects are divided into Young, Tenured and Perm, and different algorithms are used for objects with different lifecycles. (based on object life cycle analysis)

As shown in the figure above, it is the distribution of generations in the Java heap.

1.Young (younger generation)

The younger generation is divided into three districts. One Eden zone and two Survivor zones. Most objects are generated in the Eden zone. When the Eden area is full, the surviving objects will be copied to the Survivor area (one of the two). When the Survivor area is full, the surviving objects in this area will be copied to another Survivor area. When the Survivor is full, the objects copied from the * * Survivor area and still alive at this time will be copied from the old area (Tenured). It should be noted that the two regions of Survivor are symmetrical, so there may be objects copied from Eden and objects copied from the previous Survivor in the same zone, while objects copied to the old zone only come from * Survivor. Also, one of the Survivor zones is always empty.

2.Tenured (older generation)

The older generation stores objects that survive from the younger generation. Generally speaking, the older generation stores objects with a longer life span.

3.Perm (persistent generation)

Used to store static files, now Java classes, methods, and so on. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some class, such as Hibernate, at this time, you need to set a large persistent generation space to store these new classes. The persistent generation size is set through-XX:MaxPermSize=.

For example: when an object is generated in a program, the normal object will allocate space among the younger generation, and if the object is too large, it may also be generated directly in the older generation (it is observed that when running a program, it will generate a 10 megabyte of space to send and receive messages each time, and this part of the memory will be allocated directly in the older generation). The younger generation will initiate memory collection when the space is allocated, most of the memory will be reclaimed, and some of the surviving memory will be copied to the from area of Survivor. After many times of recycling, if the memory in the from area is also allocated, memory collection will also occur and the remaining objects will be copied to the to area. When the to area is full, memory collection occurs again and the surviving objects are copied to the old zone.

Usually, when we talk about JVM memory recovery, we always refer to heap memory recovery. Indeed, only the content in the heap is dynamically applied for allocation, so the younger and older generations of the above objects refer to the Heap space of JVM, while the persistent generation refers to the MethodArea mentioned earlier, which does not belong to Heap.

Some suggestions on JVM memory Management

1. Manually set the generated useless objects and intermediate objects to null to speed up memory recovery.

2. Object pooling technology if the generated objects are reusable objects, but when the attributes are different, we can consider using object pooling to generate less objects. If there are idle objects, they are taken out of the object pool and are not regenerated into new objects, which greatly improves the reuse rate of objects.

3. JVM tuning improves the speed of garbage collection by configuring JVM parameters. If there is no memory leak and the above two methods cannot guarantee JVM memory collection, you can consider using JVM tuning to solve the problem, but you must go through long-term testing on the physical machine, because different parameters may cause different effects. For example,-Xnoclassgc parameter, etc.

Two recommended JVM memory detection tools

1. JconsoleJDK's built-in memory monitoring tool, jconsole.exe in the path jdkbin directory, double-click to run it. There are two connection methods: local methods, such as processes running during debugging, can be connected directly, and the second is remote, which can connect processes started in the form of services. The remote connection method is to add-Dcom.sun.management.jmxremote.port=1090-Dcom.sun.management.jmxremote.ssl=false-Dcom.sun.management.jmxremote.authenticate=false1090 is the listening port number in the jvm startup parameters of the target process to modify it when you use it, and then use IP plus port number to connect. Through this tool, you can monitor the amount of memory at that time, the amount of CPU used, and the loading of classes. It also provides the function of manual gc. The advantage is high efficiency, high speed, and monitoring the operation of the product without affecting the operation. The disadvantage is that you can't see specific information such as classes or objects. The way to use it is very simple. You can know how the function is with a few clicks. There are really things you don't understand that you can query the documents on the Internet.

2. JProfiler charging tools, but there are ways to crack them everywhere. After installation, you can configure a local session to run in the way of configuration and debugging. Can monitor the memory at that time, CPU, threads, etc., can specifically list the memory usage, but also can analyze a class. There are many advantages and disadvantages that affect the speed, and some classes may not be woven into the method. For example, when I use jprofiler, I have never backed up successfully, and there are always some class errors.

The solution to the problem of JVM memory recovery is shared here. 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.

Share To

Development

Wechat

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

12
Report