9

How does the defragmentation of dynamically allocated memory (allocated using the new and malloc operator ) work in C++?

3
  • 8
    It doesn't. Once an object is allocated, it's stuck in place until it dies. Commented Jun 9, 2013 at 11:06
  • 2
    what do you mean with defragmentation? a code example would help here. Commented Jun 9, 2013 at 11:07
  • 4
    if you are concerned about memory fragmentation due to allocation/deallocation of a high number of small objects, the canonical way to deal with this is to use a memory pool to allocate your instances - try boost boost.org/doc/libs/1_47_0/libs/pool/doc/index.html Commented Jun 9, 2013 at 11:13

2 Answers 2

11

There is no defragmentation in the C++ heap because the application is free to keep pointers to allocated memory. Thus the heap manager cannot move memory around that is already allocated. The only "defragmentation" possible is if you free two adjacent blocks. Then the heap manager will combine the two blocks to a single larger free block that can be used for allocation again.

Sign up to request clarification or add additional context in comments.

9 Comments

Why can't the heap manager defragment memory just like the garbage collector of java does during garbage collection by moving around objects? If the memory in the C++ heap is fragmented it could be possible that the new requests to allocate memory may fail even though the total free space is available but it is not available as a contiguous chunk.
In Java the garbage collector can move blocks around to defragment the heap because it knows about all the references to all the blocks on the heap. When a block is moved during defragmentation the actual memory address is updated but all the managed references are also updated. This is not possible in C++ where an actual address is returned when a block is allocated on the heap. The C++ program can dereference any pointer that points into the block and these pointers can be computed by pointer arithmetic. If the block moves the pointers stored by the application code becomes invalid.
Can't the pointers be updated in C++ just like the Garbage collector updates the references?
@bourne: But how? These pointers are in other memory locations or registers and the C++ runtime doesn't know about all these pointers. In theory you could perhaps write a virtual machine and a C++ compiler that allowed pointers to be updated in the same way as it is done in the Java virtual machine but then you would also have the overhead of that environment and it would be silly to not switch to a fully garbage collected environment. I.e. use Java as your language.
There is no entity in C++ keeping track of where are the variables containing pointers, nor there is a garbage collector. Memory in C++ must be managed carefully. You could defragmentate memory if you code a memory manager yourself, but that would add CPU cost to your application, and thats exactly what coding in C++ is all about to avoid.
|
4

You might want to look into slab allocators. This won't be your silver bullet but for specific problems you may be able to relief the pressure. In a past project of mine we've had our own allocator written which was quite a complex affair but it certainly managed to get a grip on the issue.

While I agree with the other answers in general, sometimes there is hope in specific use cases. Such as similar objects that may be tackled with pool allocators: http://www.boost.org/doc/libs/1_53_0/libs/pool/doc/index.html

Also an interesting read are the allocators that come with boost interprocess: http://www.boost.org/doc/libs/1_53_0/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.