malloc returns memory or virtual address space

前端 未结 15 2223
攒了一身酷
攒了一身酷 2021-01-31 06:36

Does malloc allocate a block of memory on the heap or should it be called Virtual Address Space?

Am I being picky calling it Virtual Address Space or this just the legac

15条回答
  •  忘掉有多难
    2021-01-31 07:14

    There are at least 3 ways of measuring memory consumption:

    • virtual address space - the amount of your process's address space consumed by the allocation. this also affects fragmentation and the maximum contiguous future allocations you can make.
    • commit charge - this is the operating system's accounting of the maximum possible physical storage required to maintain all of the writable, non-file/device-backed memory allocated to your process. if the OS allows it to exceed the total physical memory + swap, very bad things could happen the first time the excess is written to.
    • physical memory - the amount of physical resources (potentially including swap, depending on your interpretation) your process is currently occupying. This could be less than commit charge due to virgin zero pages and virgin private writable maps of files, or more than commit charge due to non-writable or shared mappings the process is using (but these are usually swappable/discardable).

    malloc generally affects them all.

    Edit: So, the best way I can think to answer your question is to say:

    malloc allocates virtual memory.

    And virtual memory consumes:

    • virtual address space,
    • commit charge, and
    • physical resources, if it's been written to.

提交回复
热议问题