Basic knowledge about Java Memory Management

Source: Internet
Author: User

In normal work, I found that a lot of daily details are related to memory management. I always wanted to stop and summarize the results. During communication with a friend over the past two days, the mapping mode between virtual addresses and physical addresses makes the top, MMAP, ringbuffer, directbytebuffer and other details frequently considered in mind, it turned out to be a real-time language. So I wrote some test code at home today to straighten out and sort out my ideas. I hope these basic knowledge will be useful to everyone.

1. Hardware and physical memory Overview

As we all know, physical memory is Ram. The processor connects to the physical memory through the memory bus. The number of bus bits (such as 32-bit or 48-bit) determines the size of the addressable physical memory. The value of 48 bits is a reminder not to be confused with the CPU register bandwidth. The x86_64 register bandwidth is 64, but the physical address bit may be 48. (After the physical address is expanded, the number of physical addresses may be larger than the register bandwidth ).

Currently, when the number of bus bits is large, only physical addresses are limited (physical addresses are mainly limited by the number and cost of slots ). Physical memory paging addressing, 4 K per page. For 32-bit addresses, page 0th ranges from 0x00000000 to 0x00001000. You can see that only the first 20 digits are used to address the physical page, and the last 12 digits are used to indicate the addresses in the page.

Think 1: What are the advantages of memory paging? This issue will be analyzed at the end of this article when talking about ringbuffer.

Reciprocal transactions between physical memory and disks

Under Linux coordination, there is a "MFN treaty" between physical memory and disks ":

1) when the physical memory is abundant:

In Linux, some physical memory is used for the buffer and cache of Io to improve the system running efficiency.

In the SAR-R command result in Linux, the total available physical memory should be kbmemfree + kbbuffers + kbcached.

Think 2: What process does the physical memory used here belong? It will be discussed later.

2) When the physical memory is insufficient:

In Linux, some data in the physical memory is stored in the SWAp area of the disk to free up the memory for the program.

In the vmstat command result, the SI in SWAp is the amount of data read from the disk per second, and so is the amount of data written from the memory to the disk.

When creating a SWAp (mkswap command), you can use a swap partition or a common file. I tried this experiment and used the swap partition method. After Swapon/dev/hdc7, used, free, buffer, and cache memory Increased. Instead, I used the file method, the four quantities remain unchanged.

Think 3: The ing of files to physical memory is different from the mappedbytebuffer ing to virtual memory scenario mentioned below.

 

 

2. The process and virtual memory are now 64-bit OS, so there is basically no limit on the virtual addressing space of the process. However, the physical memory it maps to is still limited. One-to-multiple ing of virtual memory

Areas in the virtual address space of a process can be mapped to physical memory, files, or any other addressable storage. The area here also uses the paging mechanism. When a program attempts to access the memory using a virtual address, the operating system and hardware will map the virtual address of the page to the physical location, this location can be a physical Ram, a file, or a page file (SWAP partition ).

The following is an overview of the IBM website. pagefile is a Windows statement, which corresponds to swap in Linux.

Figure 1: Virtual Memory ing Overview

Processes in can be user processes or kernel processes (such as init fork all processes ). The virtual address space of a user process includes user space and kernel space (OS space ). Each process has its own virtual address space (Logical Address area), which is determined by the address size on the system. For example, for 32-bit windows, a single process can have 4 GB addressable space, but the physical address may have 64 GB space after expansion.

Thought 4: The mappedbytebuffer we use in our development is simply a map that maps the file content to a region in the computer's virtual memory, so that we can directly operate on the data in the memory, instead of reading files through the physical hard disk through I/O each time, the efficiency is greatly improved. Mappedbytebuffer is mainly used in the following scenarios: file sharing is required for inter-process communication (using the same file inode), and the memory needs to be written and automatically persisted to the file at the same time.


Multi-to-one ing of virtual memory

Think 5: Can a virtual memory address point to the same physical memory address in Figure 1? In some embedded OS, the program does use all the physical memory directly. However, Windows and Linux are operating systems with virtual memory, the virtual memory allows multiple processes to share the physical memory (for example, the OS only occupies a portion of the physical memory, but each process has a kernel address space mapped to the same physical memory ).

Although each process has its own address space, the program usually cannot use all of the space. The address space is divided into kernel space and user space. Most operating systems map part of each process address space to a common kernel memory area. The address space mapped to the kernel is called the kernel space, and the rest is called the user space, which can be used by your applications.

In thinking 2, physical memory is used for buffer and cache, and most of them are kernel space. By default, 32-bit windows has 2 GB user space and 2 GB kernel space, while Linux has 3 GB and 1 GB respectively.

The kernel is the main operating system program, including the logic used to connect computer hardware, scheduling programs, and provide services such as networking and virtual memory. As part of the computer startup sequence, the operating system kernel runs and initializes hardware.

If a user program needs services from an operating system, it can execute an operation called a system call to interact with the kernel program. System calls are usually required to read and write files, connect to the Internet, and start new processes. MMAP System Call implementation

3. Memory Used by the Java Process

On Linux and Windows, a process is a resource (such as file and socket information) controlled by the operating system, a typical virtual address space (more than one in some architectures) and at least one execution thread.

Java is a single-process application and is essentially different from a common process.

With the above analysis, we can easily understand why the memory consumed by the Java Process we see with top is sometimes greater than the sum of-xmx and-XX: maxpermsize. The memory consumed by Java processes includes the physical memory corresponding to the JVM and physical memory outside the JVM consumed by Java applications, as well as the kernel space address, in general, we cannot use top to determine the amount of memory consumed by JVM and the amount of memory outside JVM.

Java processes generally use the following memory:

1) Java heap and permanent generation.

2) thread stack. -XSS can be adjusted. stackoverflowexception is thrown when the stack depth is insufficient, and outofmemoryerror is thrown when a new thread is created without memory.

3) JIT compilation, JNI code, and GC.

4) socket buffer. The receive cache area connected by each socket is about 37kb, And the send cache area is about 25kb. In the case of many connections, it is also very impressive. This corresponds to the kernel address space.

5) directmemory. Some frameworks (such as cometd) used in normal development will use a large number of NiO operations to directbytebuffer. It directly allocates off-heap memory through the native library, the space used here is also within the virtual memory address range, and may also cause outofmemoryerror due to the limited space accessible by the process.

I wrote a simple program to test the impact of directbytebuffer on top and jstat results:

1) After directbytebuffer references are cleared in the heap, GC can also release off-heap physical memory.

2) When the program starts, the-XMS space is only the allocated address space, and the top is not included in the memory used.

3) after the memory is used, the memory is counted in even if the heap memory is GC. This may be related to the use of a new generation of "replication algorithm" instead of "tag-sorting algorithm" for GC.

4 paging mechanism and ringbuffer

Ringbuffer also uses the MMAP technology, but here we want to discuss the question of the First One: What can it be used for using the memory paging technology for reference?

The previous 4 K paging technology can be used to address physical pages with the first 20 digits, and the last 12 digits are used to indicate addresses in pages. In fact, it can be further evolved, for example, use the first 10 digits to indicate the number of 4 m files, and the last 22 digits to indicate the number of 4 m files. In this way, you can set different slot sizes in ringbuff to solve problems such as memory fragmentation, cache, and file-to-memory conversion.

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.