2

I'm allocating 10 GB of RAM for tons of objects that I will need. I want to be able to squeeze every last byte of RAM I can before hitting some problem like null pointer.

I know the allocator returns continuous memory, so if I have scattered memory from other programs, the max continuous size will be quite small (I assume), or smaller than the actual amount of remaining free memory.

Is it better to allocate the entire size of continuous memory I need in one go (10GB) or is it better to allocate smaller non-contiguous chunks and link them together?

Which one is more likely to always return all the memory I need?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
extracrispy
  • 669
  • 5
  • 16
  • 2
    Is this a real problem you're trying to solve? – Mark Garcia Sep 23 '13 at 05:39
  • 5
    Virtual memory means you mostly don't need to worry about fragmentation caused by other programs' use of memory. – user2357112 Sep 23 '13 at 05:40
  • 1
    How and when are your tons of objects allocated ? Are they allocated early at initialization and freed only at end, or do you allocate and free many of them while running? – Basile Starynkevitch Sep 23 '13 at 05:41
  • And what kind of objects do you have? Are they nearly all of the same class, or of the same size? – Basile Starynkevitch Sep 23 '13 at 05:44
  • What the best technique is, heavily depends on what you actually want to do with the data and on which system your are running. Systems have different ways in handling memory, including swapping of memory on process switches. So if you use memory near the limit of your hardware, you should really know what you are doing. Otherwise the swapping on process switches could horribly influence the performance of your application. – t.niese Sep 23 '13 at 06:04
  • http://stackoverflow.com/questions/13074689/maximum-memory-that-can-be-allocated-dynamically-and-at-compile-time-in-c may answer few of you questions. Os does not allow application to allocate ram directly. it usually allocate pages which remain unused until there is some read and write operation on it. – Himanshu Pandey Sep 23 '13 at 06:05

0 Answers0