How Go 1.16 Uses madvise to Release Memory and Reduce RSS

This article explains Go 1.16's madvise integration, detailing the MADV_DONTNEED and MADV_FREE options, their impact on physical memory release, RSS reduction, and the underlying mmap-based heap allocation with code examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How Go 1.16 Uses madvise to Release Memory and Reduce RSS

madvice is a system‑call parameter configuration introduced in Go 1.16; the article explains its principle and purpose.

Go uses the madvise system call to return physical memory to the operating system, offering two return types: MADV_DONTNEED: immediately returns memory; subsequent access triggers a page fault and requires reallocation, reducing the program’s RSS. MADV_FREE: marks memory as unused so the OS can lazily reclaim it; if the OS hasn’t reclaimed it yet, no page fault occurs, and RSS may not decrease.

The current implementation first tries MADV_FREE; if it fails, it falls back to MADV_DONTNEED. MADV_FREE requires Linux kernel 4.5 or newer:

var adviseUnused = uint32(_MADV_FREE)
func sysUnused(v unsafe.Pointer, n uintptr) {
    // ...
    var advise uint32
    // If GODEBUG=madvdontneed=1, force MADV_DONTNEED
    if debug.madvdontneed != 0 {
        advise = _MADV_DONTNEED
    } else {
        advise = atomic.Load(&adviseUnused)
    }
    // First try MADV_FREE
    if errno := madvise(v, n, int32(advise)); advise == _MADV_FREE && errno != 0 {
        // MADV_FREE was added in Linux 4.5. Fall back to MADV_DONTNEED if not supported.
        atomic.Store(&adviseUnused, _MADV_DONTNEED)
        madvise(v, n, _MADV_DONTNEED)
    }
}
MADV_FREE

may give slightly better performance but often does not reduce RSS; you can force MADV_DONTNEED with the environment variable GODEBUG=madvdontneed=1.

Go’s heap primarily uses mmap for allocation. First, an anonymous mapping reserves a memory region with PROT_NONE, which does not allocate physical pages. When actual memory is needed, the reserved region is mapped with read/write permissions:

func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
    p, err := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    if err != 0 {
        return nil
    }
    return p
}
func sysMap(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    mSysStatInc(sysStat, n) // memory statistics
    // Change the reserved area to readable and writable
    p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    if err == _ENOMEM {
        throw("runtime: out of memory")
    }
    if p != v || err != 0 {
        throw("runtime: cannot map pages in arena address space")
    }
}

When inspecting a process’s memory usage, the focus is on RSS. The reserved memory (allocated with PROT_NONE) does not consume physical pages and therefore does not affect the RSS value.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxRSSmemory-managementmadvise
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.