Malloc allocates memory more than RAM

后端 未结 4 436
执念已碎
执念已碎 2020-12-29 15:44

I just executed a program that mallocs 13 MB in a 12 MB machine (QEMU Emulated!) . Not just that, i even browsed through the memory and filled junk in it...



        
4条回答
  •  别那么骄傲
    2020-12-29 16:30

    This is called as Lazy Allocation.

    Most OS like Linux have an Lazy Allocation memory model wherein the returned memory address is a virtual address and the actual allocation only happens at access-time. The OS assumes that it will be able to provide this allocation at access-Time.

    The memory allocated by malloc is not backed by real memory until the program actually touches it.

    While, since calloc initializes the memory to 0 you can be assured that the OS has already backed the allocation with actual RAM (or swap).

    Try using callocand most probably it will return you out of memory unless your swap file/partition is big enough to satisfy the request.

提交回复
热议问题