Reallocate memory to avoid memory fragmentation

654 Views Asked by At

Here is a simple example to explain the issue (with c++) :

A* a1 = new A;
A* a2 = new A;
A* a3 = new A;
delete(a2);
B* b = new B;
.
.
.

let's say the size of an A object is 3, and the size of a B object is 4, and the size of my heap is 12, after deleting a2, the memory will be like this :

XXX---XXX---

I can't create a the object B* b even if there is enough memory, since it's not contiguous.

Just a simple example of memory fragmentation.

Can I avoid this dynamically by creating some kind of reallocate() function, a function that would "move" the memory of object a3 and put it right after a :

XXXXXX------

The function should obviously be called after deleting a2, so maybe reimplemeting deallocate() or delete() can do this, how can I do this please ?

This is just a very simple example to show the kind of problem i am dealing with

1

There are 1 best solutions below

1
On

Memory allocation is indeed often a bottleneck. But writing your own allocator is not easy. There are multiple ways of doing it wrong.

In your case it looks like some kind of slab allocator would fit your need.

But instead of writing your own, you may choose to rely on a battle-hardened implementation like jemalloc. Facebook uses it with C and C++ and even contributed patches. See this facebook engineering blog post.

The question how to integrate jemalloc with c++ is handled here

PS: I don't quote facebook because it is hype, but because it is a well known company with real performance issues. Google also uses a custom allocator: tcmalloc