2

What I mean is this - is allocating small memory chunks faster than allocating big ones? I.e.:

int * dummy = new int;    // A
int * dummy2 = new int[2];    // A
int * dummy3 = new int[1000000];    // B
VeryVeryBigStruct * dummy4 = new VeryVeryBigStruct;    // B

(A) Would be small allocations, (B) would be big ones.

I know I could just write a simple program and test it for myself but I actually tried and even QueryPerformanceCounter() returned 0 time for single small allocation (which is a little surprising since all allocations are supposedly so slow. Besides, 1 test on 1 computer is hardly reliable so I would like to hear what you know on the matter.

Also, I would like to know if 10 allocations of N bytes are faster/slower than 1 allocation of 10 * N bytes (I would say it should be slower but who knows).

NPS
  • 6,003
  • 11
  • 53
  • 90
  • 3
    Be warned, any answer has to make a few significant assumptions about the internals of memory allocation, and that makes them implementation specific even *if* these assumptions are true for almost all implementations of `new`/`new[]`/`malloc`. The /design space for memory allocators is huge, and for any possible answer I can easily conceive a memory allocator to which the answer does not apply -- though most of them will be awful for real code. –  Sep 08 '13 at 12:35
  • I wonder if you've used optimization option in you test. – lulyon Sep 08 '13 at 12:36
  • The underlying memory manager usually assigns large chunks of memory at once (e.g. a *page* on Unix-like systems, which may be something like 4kB in size). A small allocation may fit into an existing page and thus be fast. – Kerrek SB Sep 08 '13 at 12:36
  • 2
    Allocations on Windows are extremely fast, they just allocate virtual memory. It doesn't cost anything. You don't start to pay until you actually *access* the memory. At which point you'll induce a page fault to get the memory mapped to RAM. – Hans Passant Sep 08 '13 at 12:37
  • @HansPassant on the other hand the page tables and vm structures have to be set up, right? They are probably more expensive the more page you reserve. – usr Sep 08 '13 at 13:21

1 Answers1

3

Depends on the operating system. In general though, it shouldn't make a big differance because system's like Linux ( and I assume modern Windows, etc. ) use lazy allocation. Namely, they allocate virtual memory space without actually allocated physical memory until you access it.

You can see more details in this other SO post:

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

Community
  • 1
  • 1
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
  • So the actual physical allocation takes place during first access? If so, I would like to know the answer to my question regarding **that actual allocation**. – NPS Sep 08 '13 at 12:49
  • @NPS Allocation occurs on a per page basis. If you do `dummy3[12345]=OxDEADBEEF`, then only one page, in which that address is located, will be allocated at that time. – Robert S. Barnes Sep 08 '13 at 13:28