{
  "slug": "mlock",
  "term": "Memory locking (mlock)",
  "aliases": [
    "mlock(2)",
    "memory locking"
  ],
  "definition": "mlock keeps the pages intersecting a process address range resident in RAM while the memory lock applies.",
  "note": "Successful mlock populates the range; Linux mlock2 with MLOCK_ONFAULT instead locks currently resident pages and locks others when faults populate them. Resource limits and permissions apply. [locking-man]\n\nLocks are not inherited across fork, and later writes can incur copy-on-write faults. Unmapping, exec and process exit remove the relevant locks. Locking does not exclude RAM from suspend-to-disk images. [locking-man]\n\nMAP_LOCKED is weaker: mmap may succeed despite incomplete prefaulting. [locking-mmap]\n\nResidency is not synchronization: msync propagates shared file-mapping changes to the filesystem. MS_INVALIDATE can fail with EBUSY for a locked range; this is not a media-error diagnosis. [locking-msync]",
  "advantages": [
    "Can keep a selected working range resident without locking the entire address space."
  ],
  "limitations": [
    "Does not make file updates durable or transactional.",
    "On-fault locking is not proof that every page has already been populated."
  ],
  "related": [
    "memory-mapped-file",
    "msync",
    "cachestat"
  ],
  "citations": [
    {
      "id": "locking-man",
      "title": "Linux man-pages: mlock(2), residency, on-fault locking and lifetime",
      "url": "https://man7.org/linux/man-pages/man2/mlock.2.html",
      "accessed": "2026-11-14"
    },
    {
      "id": "locking-mmap",
      "title": "Linux man-pages: mmap(2), MAP_LOCKED prefault limitations",
      "url": "https://man7.org/linux/man-pages/man2/mmap.2.html",
      "accessed": "2026-11-14"
    },
    {
      "id": "locking-msync",
      "title": "Linux man-pages: msync(2), synchronization and locked-range invalidation",
      "url": "https://man7.org/linux/man-pages/man2/msync.2.html",
      "accessed": "2026-11-14"
    }
  ],
  "status": "shipping",
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-11-14",
  "provenance": [
    "Primary API documentation reviewed 2026-11-14; Linux-specific mlock2 behavior is distinguished from mlock. No latency, swap or power-loss experiment performed."
  ]
}
