Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a data buffer which stores large number of 4-5 byte data and fetch them not in the same order they were allocated. The rest of the resources used by the process are large and dynamic so fragmentation will seriously affect performance.

Data is added one by one and removed in bulk but not fully.

So I plan to implement it as a bulk allocated memory chunk with a memory manager. What is the best way to do it?
Posted

In the book Modern C++ design[^] by Andrei Alenxandrescu, there's a chapter about small object allocation. He describes an efficient way of managing a lot of small objects in memory (allocating and deallocating them yourself in an efficient way).
I don't know if this worth to buy the book just for that. However, the rest of the book is interesting too.
 
Share this answer
 
Does your software work alone on the computer or will it just one of many applications that are expected to run with it?

If it is expected to work alone, just allocate a bulk buffer and write yourself an algorithm that take care of the various fragments (you need some ancillary structures to track free blocks, compact them and best-fit).

But beware that's such a "science" is not "today business": these kind of algorithms are well known from decades, and are all embedded into memory allocators of the modern operating systems, that manage fragmentation nerveless with no difference of what you can make yourself. So, even if you allocate through the system one data by one ... may be you get the same efficiency.

If it is expected to run together with all possible desktop apps ... allocating a big bulk is not the way to go: you should ask for memory as you need it and return it as you don't need it anymore, otherwise your app will not be "fair" to other apps.

You can try to optimize the number of allocation if you can predict in wich "bulk" a data will be released and how large that "bulk" will be.

You can -at that point- allocate "plexes" large as the bulks, and suballocate into the plexes, returning the plexes to the system as they become completely free.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900