Skip to content

Persistent Memory

Linux systems increasingly leverage persistent memory (PMEM) and hybrid memory architectures to optimize performance for memory-intensive workloads. PMEM devices, such as Intel Optane DC Persistent Memory, offer byte-addressable storage with persistence across reboots, bridging the gap between volatile RAM and traditional storage. When combined with swap-backed memory and hybrid memory configurations, these technologies enable applications to balance speed, capacity, and persistence for critical tasks like databases, in-memory caches, and real-time analytics.


Persistent Memory (PMEM) as a Memory Layer

Persistent memory devices provide a tier between RAM and SSDs, offering faster access than disk while retaining data after power loss. They can be used as:

  • Swap devices: Replace traditional disk-based swap with PMEM for faster context switching.
  • Memory-mapped files: Allow applications to directly access PMEM for persistent storage.
  • Hybrid memory pools: Combine PMEM with RAM to create tiered memory architectures.

Example: Configuring PMEM as Swap

  1. Verify PMEM device recognition:

    dmesg | grep -i pmem
    lsblk
    
    Look for devices like /dev/pmem0 or /dev/hpmem0.

  2. Create a swap file on PMEM:

    dd if=/dev/zero of=/dev/pmem0 bs=1M count=1024
    mkswap /dev/pmem0
    swapon /dev/pmem0
    
    Adjust count to define swap size (e.g., 1GB = 1024MB).

  3. Persist the swap configuration: Add /dev/pmem0 to /etc/fstab:

    /dev/pmem0 none swap sw 0 0
    


Swap-Backed Memory with PMEM

Traditional swap uses disk, but PMEM-based swap reduces latency by leveraging faster storage. Key considerations:

  • Performance: PMEM swap is significantly faster than SSD-based swap but slower than RAM.
  • Kernel tuning: Adjust vm.swappiness to prioritize PMEM for less critical processes:

    sysctl -w vm.swappiness=10
    
    Add vm.swappiness=10 to /etc/sysctl.conf for persistence.

  • Monitoring: Use free, swapon --show, and iostat to track swap usage and PMEM I/O.


Hybrid Memory Architectures

Hybrid systems combine RAM, PMEM, and SSDs to optimize for speed and capacity. Linux supports this through:

  • NUMA and memory policies: Use numactl to bind processes to specific memory nodes.
  • Transparent Huge Pages (THP): Enable THP for large memory workloads:

    echo 1 > /proc/sys/vm/transparent_hugepage_enabled
    
    (Note: THP may impact PMEM performance; test carefully.)

  • Memory-mapped files: Applications can map PMEM regions directly, enabling persistent storage without disk I/O. Example (C):

    int fd = open("/dev/pmem0", O_RDWR);
    void* ptr = mmap(NULL, 1024*1024, PROT_WRITE, MAP_SHARED, fd, 0);
    // Use ptr as a persistent memory buffer
    munmap(ptr, 1024*1034);
    close(fd);
    


Key Takeaways

  • PMEM as swap reduces latency compared to disk-based swap but requires careful capacity planning.
  • Hybrid memory architectures combine RAM, PMEM, and SSDs to balance speed and persistence for workloads like databases.
  • Kernel tuning (e.g., vm.swappiness, THP) and tools like numactl are critical for optimizing PMEM and swap performance.
  • Applications can leverage PMEM via memory-mapped files or libraries like libpmem for persistent storage.
  • Monitoring and testing are essential to ensure PMEM configurations meet performance and reliability goals.