-1

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

Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
  • Let your OS take care of memory fragmentation. Don't try and second guess it. If you really want to do something, create a std::vector and use that. You can use another vector of int to store indices of "deleted" objects. – Robinson Mar 23 '15 at 10:31
  • This is just a simple example, I'm actually working on a very big project – Othman Benchekroun Mar 23 '15 at 10:33
  • First you need to prove that fragmentation is a problem for your software. Once you've done that if you don't like the allocation/reallocation strategy of the OS, you can make your own. What I wouldn't do is try to hack the OS memory manager around for your project. – Robinson Mar 23 '15 at 10:36
  • Can you give me a simple example of how I could change the allocation/reallocation strategy ?? and how can someone hack the OS memory manager ?? ( I may have to do this, I am just a trainee in this project, there is many experts that would help me) – Othman Benchekroun Mar 23 '15 at 10:41
  • In this case create your objects in an array or vector so they are placed in one memory block. – Neil Kirk Mar 23 '15 at 10:42
  • As I said, this was just a simple example to show the issue, I can't just place all the objects in one vector, the project is like 10M lines ... – Othman Benchekroun Mar 23 '15 at 10:44
  • I agree with the above comments but I imagine that copying the contents of a3 to a local store, deleting and making a new a3 _might_ make a difference. Remember that the decisions of where to allocate memory isn't simple - read about flash memory wear levelling for example. – Ant Mar 23 '15 at 10:48

1 Answers1

1

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

Community
  • 1
  • 1
fjardon
  • 7,921
  • 22
  • 31
  • Thank you for your answer, do you know any tutorial for writing my own allocator ? It is a research project, so I may have to write my own allocator – Othman Benchekroun Mar 23 '15 at 12:40