# Memory
+ Transparent Huge Page (THP) 
  + It is not recommended to use `THP` for database applications because databases tend to have sparse rather than contiguous memory access patterns, when upper memory(UMB) with high fragmentation, there will be big delay  in allocating THP pages. If you turn on direct memory trimming function for `THP`, then there will be more likely to have CPU spikes, so it is recommended to turn off `THP`
  + Virtual Memory Parameters 
    + `dirty_ratio`, `dirty_background_ratio`: Normally no adjustment is required. For high performance SSD, such as NVMe devices, setting a small value can improve the efficiency of memory reclamation
![center](./student-guide/diagram/06-memory-01-01.png)
-------------------------------------------------------------
There are two types of large pages in Linux: huge pages (standard large pages) and transparent huge pages (transparent large pages).
Memory is managed in blocks as pages, and the default page size for most systems today is 4096 bytes or 4K. 1MB of memory equals 256 pages; 1GB of memory equals 256000 pages.
The CPU has a built-in memory snap-in that contains a list of these pages, each referenced by a page table entry. As memory gets larger, the more expensive the CPU needs to manage these memory pages, which can have an impact on the performance of the operating system.
- 1. Huge pages
  - Huge pages were introduced after Linux Kernel 2.6 to allow the operating system to support the large page capacity features of modern hardware architectures by replacing traditional 4kb memory pages by using large pages of memory to accommodate the increasing amount of system memory.
  Huge pages are available in two format sizes: 2MB and 1GB, with a 2MB page size suitable for GB-sized memory, and a 1GB page size suitable for terabyte-level memory; 2MB is the default page size.
- 2. Huge Transparent Pages
  - Transparent Huge Pages, short for THP, is a feature introduced in RHEL 6. Transparent pages are enabled by default on Linux6. Because huge pages are difficult to manage manually and often require significant code changes to be effective, RHEL 6 began introducing Transparent Huge Pages (THP), an abstraction layer that automates the creation, management, and use of traditional large pages. THP reduces the complexity of using traditional large pages for system administrators and developers because the goal of THP is to improve performance, so other developers (from the community and Red Hat) have test and optimized THP across a variety of systems, configurations, applications, and workloads. This allows the default settings of THP to improve the performance of most system configurations. However, using THP for database workloads is not recommended. The biggest difference between the two is that standard large page management is pre-allocated, while Transparent Huge Pages management is dynamic.
  - THP is not recommended for database applications because databases tend to have sparse rather than contiguous memory access patterns, and high latency in allocating THP pages when high-level memory fragmentation is severe. Turning on the direct memory throttling for THP can also cause a surge in system CPU usage, so it is recommended to turn off THP.
 
- Virtual Memory Parameters
  - dirty_ratio percentage value. When the total number of dirty page caches reaches this percentage of the total system memory, the system begins writing the dirty page cache to disk using the pdflush operation. The default value is 20%, which usually does not need to be adjusted. For high-performance SSDs, such as NVMe devices, lowering their value can improve the efficiency of memory reclamation.
  - dirty_background_ratio percentage value. When the total number of dirty page caches reaches this percentage of the total system memory, the system starts writing dirty page caches to disk in the background. The default value is 10% and usually does not need to be adjusted. For high-performance SSDs, such as NVMe devices, setting a lower value can improve the efficiency of memory reclamation.
