Does malloc lazily create the backing pages for an allocation on Linux (and other platforms)?

前端 未结 6 909
無奈伤痛
無奈伤痛 2020-11-27 10:20

On Linux if I were to malloc(1024 * 1024 * 1024), what does malloc actually do?

I\'m sure it assigns a virtual address to the allocation (by walking the

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 10:47

    Linux does deferred page allocation, aka. 'optimistic memory allocation'. The memory you get back from malloc is not backed by anything and when you touch it you may actually get an OOM condition (if there is no swap space for the page you request), in which case a process is unceremoniously terminated.

    See for example http://www.linuxdevcenter.com/pub/a/linux/2006/11/30/linux-out-of-memory.html

提交回复
热议问题