Introduction to Golang-getpagesize () memory pages-how memory paging size improves performance

Source: Internet
Author: User
Tags benchmark

Tuning of JVM Optimizations large Memory paging (largepage)

This article will be from the principle of memory paging, how to adjust the paging size two sections, to explain to you how largepage to the performance of the JVM, and at the end of the text pointed out the side effects of the large page. OK, let's get started!

How memory paging size improves performance

First, we need to review a small subset of the principles of computer composition, which is good for understanding the high memory paging as well as the performance improvement of the JVM.

What is memory paging?
We know that the CPU accesses memory by addressing it. The addressing width of the 32-bit CPU is 0~0XFFFFFFFF, and the computed size is 4G, which means that the maximum supported physical memory is 4G.

However, in the course of practice, encountered such a problem, the program needs to use 4G of memory, and the available physical memory is less than 4G, causing the program to reduce memory consumption.
In order to solve such problems, modern CPUs introduced the MMU (Memory Management unit RAM Management Unit).

The core idea of the MMU is to replace the physical address with the virtual address, that is, when the CPU is addressing, and the MMU is responsible for mapping the virtual address to physical addresses.
The introduction of the MMU solves the limitation of physical memory, which is the same as using 4G memory for the program.

Memory Paging (Paging) is a memory management mechanism proposed on the basis of the use of MMU. It splits the virtual address and physical address by a fixed size (4K) into pages and page frames (page frame) and guarantees that the page is the same size as the page frame.

This mechanism, from the data structure, guarantees efficient access to memory and enables the OS to support non-contiguous memory allocations.
When the program memory is not enough, you can also transfer the infrequently used physical memory pages to other storage devices, such as disk, which is familiar with the virtual memory.

As mentioned above, the virtual address and physical address need to be mapped to make the CPU work properly.
Mapping requires the mapping table to be stored. In modern CPU architectures, mapping relationships are typically stored in physical memory where a page table is called.
Such as:

The interaction between physical memory

From this diagram, you can clearly see the interaction between the CPU and the page table, the physical memory.

further optimization, the introduction of TLB (translation lookaside buffer, page table register buffer) is indicated in the previous section that the page table is stored in memory. We know that the CPU accesses the memory through the bus, certainly slower than the direct access register.
To further optimize performance, the Modern CPU architecture introduces the TLB to cache part of the frequently accessed page table content.
Such as:

Added the interaction between the TLB physical memory

In contrast to 9.6, the TLB is added in the middle.

Why should I support large memory paging?
The TLB is limited, no doubt about it. TLB miss occurs when the TLB's storage limit is exceeded, and then the OS commands the CPU to access the page table in memory. If the TLB miss is frequent, the performance of the program will drop quickly.

In order for the TLB to store more page address mappings, our approach is to increase the paging size of the memory.

If a page 4M, compared to a page 4K, the former can allow the TLB to store more than 1000 page address mapping relationship, performance improvement is relatively considerable.

Adjusting OS and JVM memory paging

There are some limitations and setup steps to enable large memory pages under Linux and Windows.

Linux:
Limit: Requires 2.6 kernel or 2.4 kernel has been hit large memory page patch.
To confirm support, please hit the following command at the terminal:

Cat/proc/meminfo | grep Huge

hugepages_total:0
hugepages_free:0
hugepagesize:2048 KB

If you have the output of the Hugepage typeface, your OS is supported for large memory paging. Hugepagesize is the default large memory page size.
Next, in order for the JVM to adjust the size of the large memory page, you need to set the OS shared memory segment maximum and the number of large memory pages.

Shared memory Segment Maximum value
It is recommended that this value is greater than the Java Heap size, which sets 4G of memory in this example.

echo 4294967295 >/proc/sys/kernel/shmmax

Number of large memory pages

echo 154 >/proc/sys/vm/nr_hugepages

This value is typically a Java process that consumes the largest memory/single page size, such as Java Settings 1.5G, a single page 10M, then the number is 1536/10 = 154.
Note: Since proc is a memory FS, it is recommended that you write a script to the init phase (rc.local) in order not to let your settings be flushed out after a reboot.

Windows:
Limitation: Only Windows Server 2003 or more server versions are supported
Operation Steps:

    1. Control Panel, Administrative Tools, Local Security Policy
    2. Local Policies User Rights Assignment
    3. Double-click "Lock pages in memory" to add users and groups
    4. Restart your computer

Note: Administrator action is required.

Single Page size adjustment
Add parameter-xx:largepagesizeinbytes=10m when JVM is enabled
If the JDK was before 1.5 update5, you also need to manually add-xx:+uselargepages, which is to enable large memory page support.

Side effects of large memory paging

Because the size of each page becomes larger, the JVM will have a more than normal partition when calculating the memory footprint of the heap internal partitions (perm, new, old). In the worst case scenario, a zone occupies more than one page size. However, subsequent JVM versions are also adjusting this strategy.

In general, it is not recommended to adjust the page size too large, 4-64m, is acceptable (default is 4M). To set this value reasonably, you should do a benchmark on your system.
To tell the truth, I have seen on the Internet the most powerful, has been tuned to 256M, from the benchmark report, performance is not too bad. If you have a 64-bit large memory machine, you might want to try it.

In addition, there are many GC-tuned articles on the web that mention largepagesizeinbytes, but do not mention any OS restrictions. If the OS is not supported, set this parameter, this parameter will be just a device.

Related Article

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.