From where does the program allocate memory?

后端 未结 4 996
迷失自我
迷失自我 2020-12-18 14:33

As a C and C++ programmer I have used malloc and new to allocate memory. I am just wondering: how does the OS allocate memory?

  1. D

4条回答
  •  一向
    一向 (楼主)
    2020-12-18 15:01

    When you address memory allocated by malloc or operator new, it is in RAM and has a unique address. It's the operating system's job to have that block of memory in RAM when addressed, but it might swap it out to disk at its discretion. Since swapping is slow, it's up to the OS to figure out how to minimize the swapping.

    Operating systems that use "virtual memory" allow you to set how much of your disk space is available to the virtual memory manager.

    There is no need for you to try to manage this yourself, although you might consider whether it is better to use a database to manage the data, especially if the lifetime of the data is longer than a single session.

提交回复
热议问题