Memory Manager

Hi

Ive been tasked with writing a simple memory manager and Im looking for some general advice and pointers.

Im required to keep track of dynamic memory allocations and deallocations in order to track memory leaks. It also needs to take into account memory fragmentation.

Although I have not yet started coding this my intention from research it to override the global new and delete operators and pass them arguments of a heap class to track any memory allocations. This will also be done with each class new and delete to track which class allocated what memory. Also it may be nessacary to tackle memory fragmentation which is where I come unstuck. Is it possible to track where memory is alloacted on the heap and to search through the heap for free blocks that can be pieced together.

Im not looking for any comprehensive code that tackles this but rather any hints and advice to put me in the right direction

Thanks

+DaNNy+
Is it possible to track where memory is alloacted on the heap and to search through the heap for free blocks that can be pieced together.
Well, sure. A pointer is an offset from the beginning of memory, and you always have the size of object that was allocated.
Although it sounds like you're supposed to be able to measure memory fragmentation, not attempt to mitigate it.
thanks for the response

It is reasonably simple for me to track object size but im not sure how I can track where on the heap objects have been writen (i.e measure memory fragmentation) and what control I can have over writing to specific areas of the heap to reduce fragmentation.

Any pointers on how to simply measure this and output it?

Also on a side note, if I cant actually manipulate where memory is written to decrease fragmentation then why the need to measure it in the first place?
im not sure how I can track where on the heap objects have been writen
Do this:
1
2
std::cout <<new int<<std::endl;
std::cout <<new int<<std::endl;
The address is where the object was put.

if I cant actually manipulate where memory is written to decrease fragmentation
See, it's not that you can't control where objects are created. You can. The problem is that that by itself doesn't do much to fix memory fragmentation. You need to be able to move objects around, which isn't easy to do in C/++.

why the need to measure memory fragmentation in the first place?
It can give insights into how the program behaves, I suppose.
thanks thats extremely helpfull, I dont understand it but it gives me starting point to research.

So I now keep track of memory allocated on the heap but it isnt easy manipulating these allocations to improve fragmentation. Are there any common techniques used to improve fragmentation?
Read up on memory allocation techniques. Some search phrases: "worst fit", "best fit", "first fit".
Counter-intuitively, "worst fit" works better to reduce fragmentation than "best fit".
Last edited on
Topic archived. No new replies allowed.