Why malloc doesn't allocate memory until I hit a certain threshold?

前端 未结 2 706
死守一世寂寞
死守一世寂寞 2021-01-15 12:20
#include 
#include 
#include 

int main(int argc, char *argv[])
{
        size_t sz = atol(argv[1]);
        char *arr         


        
相关标签:
2条回答
  • 2021-01-15 12:43

    1 - Why it doesn't allocate when the memory size is relatively small?

    The task of the function malloc is to provide the application with memory, whenever it asks for it. Theoretically, malloc could, as you suggest, just forward all requests for memory allocations to the operating system's kernel, so that it only acts as a wrapper for the kernel's memory allocator. However, this has the following disadvantages:

    1. The kernel only provides large amounts of memory at once, at least one page of memory, which is, depending on the configuration of the operating system, normally at least 4096 bytes. Therefore, if an application asked for only 10 bytes of memory, a lot of memory would be wasted.
    2. System calls are expensive in terms of CPU performance.

    For these reasons, it is more efficient for malloc to not forward memory allocation requests directly to the kernel, but to rather act as an intermediary between the application's memory allocation requests and the kernel. It requests memory in larger amounts from the kernel, so that it can satisfy many smaller memory allocation requests from the application.

    Therefore, only when asking for a large amount of memory at once, will malloc forward that memory allocation request to the kernel.


    2 - Why the allocated memory size is not exactly the same? In the first run, it shows that the size is 1004KB while I've only allocated 1000KB.

    The malloc allocator must keep track of all the memory allocations it granted to the application and also keep track of all the memory allocations that it has been granted by the kernel. To store this information, it requires a bit of addititional memory space. This additional space is called "overhead".

    0 讨论(0)
  • 2021-01-15 13:01

    What you're seeing in the pmap output is almost certainly the addition needed to the malloc arena to satisfy larger requests, not any single request.

    The arena is the pool of memory from which allocations are handed out and there's a good chance this starts at a certain size and is only expanded on demand.

    For example, if the initial arena is 1000K, any allocation that does not exhaust that will have no need to get extra arena space. If you do exhaust that space, the process will try to request more arena from the underlying environment so it can satisfy the extra demand.


    As to why the size isn't what you requested, there are (at least) two possible reasons. First, the arena isn't just the memory allocated for your purposes, it also holds control information so that the memory can be properly managed (sizes, checksums, pointers, free list and so on).

    Secondly, malloc may over-allocate on the expectation that this won't be the last request that exhausts the current arena. Some memory allocation strategies go so far as to double the current arena size when requesting more, in order to amortise the cost of doing so.

    0 讨论(0)
提交回复
热议问题