Virtual Memory and resident memory of a Java program

Source: Internet
Author: User

Recently, I suffered from virtual memory and received reports saying that our products will be restarted from time to time, and the report said that "the memory used exceeds 5 GB ".

Restarting is the mechanism of our product. We will monitor the memory, perform GC and restart the application beyond a certain limit, but it is too exaggerated to exceed 5 GB, the reported environment does not have much load. This problem has been regarded as a memory leak and cannot be solved because of its inability to reproduce it. Recently, it suddenly appeared in a customer's environment. Hurry and study it!

We finally found that our monitoring mechanism was faulty:CodeYes. When virtual memory> 5 * xmx (Java option) is restarted, it looks correct, however, when you use the free command to view the memory size before and after the application runs, it is found that there are not many actually used memory, and the remaining memory remains in a relatively stable number, and there is no continuous decline. In this case, the memory leakage does not actually exist. Why is the use of virtual memory so large when the application is started?

However, I began to pay attention to the significance of virtual memory. Wow, the problem is here. Its meaning is not what we take for granted. It is quite different!

The manpage of virtual memory in the top command is explained as follows:

 
O: virt -- virtual image (Kb) the total amount of virtual memory used by the task. it includes des all code, data and shared libraries plus pages that have been swapped out. virt = swap + Res.

That is to say, virtual memory does not include the code data but also the shared library (for Java, It is a jar file). The shared library includes not only the currently used but also the already used swap. Swap is not actually in memory. What does it mean? virtual memory is called virtual memory. In fact, part of this value is not physic memory, but disk space.

Further explanation: virtual memory is the sum of all the data capacities in the virtual memory map. Generally, it makes no sense.

For exampleProgramThe classpath references the 10 m jar package and uses ten 10 m files in the program. What is the virtual memory when the program starts?

Code of the program itself + data of the program itself + 10 m (jars) + 10*10 m (File) + space occupied by JRE, which is already a large number, but in fact, when the program is started, it will not load all the resources. Even if it is loaded, it will soon be pushed out by swap. The value of virtual memory is actually a number, the actual consumption has nothing to do with it.

Therefore, it makes no sense to use virtual memory in the monitoring program. Which value represents the actual memory used?

 
Resident memory: the number of pages that are currently resident in Ram

It is a more reliable memory usage value.

IntroductionArticleThis is too thorough:

What is virtual memory?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.