biggest reason for using memory manager in large C project.

U

uday.das

hi ,
I am not sure but I think it might be to avoid heap fragmentation due
to several malloc calls. Are there any strong reason ? What are the
other things that should take care when we implement a memory manager ,
like i) garabage collector kind of thing , ii) Safe Free of pointer in
wrapper of Free functions etc .

Thanks
Uday
 
C

Chris McDonald

hi ,
I am not sure but I think it might be to avoid heap fragmentation due
to several malloc calls. Are there any strong reason ? What are the
other things that should take care when we implement a memory manager ,
like i) garabage collector kind of thing , ii) Safe Free of pointer in
wrapper of Free functions etc .

If I correctly understand your use of 'memory manager', then it enables
you to free all memory associated with a single activity/linked-structure,
etc, with a single call to its deallocate function. Some software
systems term these memory pools.

This means that you can first allocate a large chunk of memory, perform
many 'sub'allocations from that large chunk (without having to remember
precisely what has been allocated and freed) and then free all space
in one final call. This can work very well if your program needs to
longjmp() 'back' to some earlier state, and permits you to free all
memory allocated since the earlier setjmp().
 
E

Eric Sosman

hi ,
I am not sure but I think it might be to avoid heap fragmentation due
to several malloc calls. Are there any strong reason ? What are the
other things that should take care when we implement a memory manager ,
like i) garabage collector kind of thing , ii) Safe Free of pointer in
wrapper of Free functions etc .

malloc() itself has a very "narrow" interface: You
tell it the number of bytes you want, but nothing about
what you intend to do with the memory, how long you intend
to retain it, what other chunks of memory it might be
related to, and so on.

A memory manager with more knowledge of the project's
data structures may be able to do better, even if it uses
plain malloc() to obtain the memory that it then doles out
to the higher-level code. Instead of just saying "Please
give me forty bytes," your program can now say "Please give
me memory for a `struct node' that will become part of the
tree whose root is `*rootptr'." Or it might say "Please
give me a hundred bytes for a short-lived buffer that I
may need to expand before discarding." Or it might say
"Please give me a thousand bytes for long-lived storage
whose size will never change." A memory manager armed
with extra information of this sort could do things like
try to keep the nodes of a particular tree close to each
other to minimize page faults, or segregate long-lived and
short-lived data, and so on.

Wholesale operations are also possible. To free all
the memory of an enormous tree, you might just say "Please
release all the memory belonging to pool T" instead of
traversing the whole tree and freeing each node separately.

A project-specific memory manager may be able to offer
additional services beyond pure memory management, too.
For example, let's think of that tree again: Perhaps your
program might need to visit every node of the tree as part
of some operation, but might not care what order they're
visited in. Instead of following the tree's own structure,
possibly hopping from page to page in erratic fashion and
putting stress on various hardware caches, a program that
has allocated all the tree's nodes from a "pool" associated
with the tree might say "Please visit all the allocated
nodes of pool T in an arbitrary memory-efficient order,
calling function f() for each node that's in use."

... and so on, and so on. Different projects have
different needs, and some (particularly those that use
large amounts of memory) can benefit from memory managers
that "know more" about the project's data structures than
malloc()'s simple interface can convey.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top