0

In Linux,

  • when using mmap() for anonymous memory mapping, or using malloc(), do they allocate "space" from only physical memory, or either physical memory or swap or their combination? (I guess the later)

  • When using System V shared memory, does it (e.g. shmget()) create a shared memory segment out of only physical memory, or either physical memory or swap or their combination? (I guess the former, because shared memory is said to be a fast IPC mechanism)

  • When using POSIX shared memory, does it (e.g. shm_open()) create a shared memory object out of only physical memory, or either physical memory or swap or their combination? (I guess the former, because shared memory is said to be a fast IPC mechanism)

Thanks.

1 Answer 1

1

If they allocate memory at all, they only reserve it as space in swap.

mmap, malloc, and shmget allocate space in the calling process’ address space; on Linux, mmap and shmget also reserve space in swap space (unless MAP_NORESERVE or SHM_NORESERVE are specified). They don’t allocate backing physical memory. shm_open doesn’t allocate memory either: it’s used to open an existing object, or create a new 0-length object.

Actual pages of memory are allocated when addresses in the address space are dereferenced. That allocation always happens in physical memory, if it’s possible at all; otherwise the process wouldn’t be able to read or write there. After it’s allocated, and written to, non-locked memory can be swapped out, but that happens later if at all.

(By “reserve”, I mean that the corresponding amount of storage is set aside for the process; by “allocate”, I mean that specific resources are assigned to the process, e.g. addresses in the virtual address space, or pages in memory.)

2
  • Thanks. What is difference between "reserve" and "allocate"?
    – Tim
    Nov 24, 2020 at 17:42
  • Good question, I wasn’t using the terms consistently; I’ve fixed that and added an explanation. Nov 25, 2020 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .