Large page memory principle

What is paging?
We know, CPU memory is accessed by addressing. CPU address width of 32 bits is 0 ~ 0xFFFFFFFF, after 16 ^ 8 calculated size 4G, that is to support the maximum physical memory is 4G.

But in practice, encountered such a problem, the program requires the use of 4G memory, available physical memory less than 4G, causing the program had to reduce the memory footprint.
To solve such problems, the introduction of modern CPU MMU (Memory Management Unit memory management unit).

MMU's core idea is to use alternate virtual address physical address, namely the use of virtual addresses when the CPU is not addressed by the MMU is responsible for mapping virtual addresses to physical addresses.
MMU introduced to address the limitations of physical memory, the program is, as they're using the same 4G memory.

 

Paging (Paging) is a memory management mechanism based on the use of the MMU, raised. It virtual addresses and physical addresses is divided by a fixed size (4K) to a page (page) and a page frame (page frame), and to ensure that the same page size of the page frame.

This mechanism, from the data structure, to ensure efficient memory access, and non-continuous support OS memory allocation.
When the program memory is not enough, you can also less commonly used physical memory pages transferred to other storage devices such as disk, which is familiar to everyone virtual memory.

It mentioned above, the virtual addresses and physical addresses by mapping needs to make the CPU work.
The mapping is required to store the mapping table. In modern CPU architectures, the mapping relationship is commonly called a local page table (page table) stored in physical memory.
As shown below:

 

 

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

Further optimization, introduction TLB (Translation lookaside buffer, the page table register buffer)
From the above one, the page table is stored in memory. We know that through the CPU bus access memory, certainly slower than direct access to the register.
To further optimize performance, modern CPU architecture introduces TLB, the cache part of the page table of contents for frequently accessed.
As shown below:

 

 

Comparative preceding picture is in the middle of the addition of TLB.

Why should we support large memory pages?
TLB is limited, no doubt about that. When the TLB exceeded the storage limit, occurs TLB miss, then, OS will command the CPU to access the page table memory. If you frequently appears TLB miss, the performance of the program will drop to quickly.

In order for TLB can store more pages, address mapping, our approach is to transfer large page size.

If a page 4M, a comparison page 4K, former allows 1000 pages, address mapping TLB multiple storage, performance improvement is quite substantial.

OS paging adjustment

Under Linux and windows to enable large pages, there are some restrictions and setup procedures.

Linux:
restrictions: the need for kernel 2.6 or 2.4 kernel has been played more than huge pages patch.
Confirm support, please knock following command in a terminal:

# cat /proc/meminfo | grep Huge
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB

If there HugePage output word content, that your OS is to support large memory paging. Hugepagesize large memory is the default page size.
Next, in order to let the JVM can adjust large memory pages size, you need to set the next OS shared memory segments and the maximum number of large memory pages.

Shared memory segment maximum
recommended that this value is greater than the Java Heap size, this example sets 4G memory.

# echo 4294967295 > /proc/sys/kernel/shmmax

The number of large pages

# echo 154 > /proc/sys/vm/nr_hugepages

This value is generally Java process takes maximum memory / single page size, such as java settings 1.5G, single page 10M, then the number is 1536/10 = 154.
Note: Because the proc is a memory FS, in order to keep your settings after reboot the flush, it is recommended to write a script in init phase (rc.local).

Windows:
restrictions: Only above 2003 server edition windows server
steps:

  1. Control Panel -> Administrative Tools -> Local Security Policy

  2. Local Policies -> User Rights Assignment

  3. Double-click the "Lock pages in memory", adding users and groups

  4. Restart the computer

Note: You need administrator action.

Guess you like

Origin www.cnblogs.com/QiangWum/p/12517469.html