[GRASS5] Module memory management - G_set_error_routine()

Glynn Clements glynn at gclements.plus.com
Thu Mar 24 15:18:28 EST 2005


Brad Douglas wrote:

> > > Would it be acceptable practice to use the following function to free
> > > memory resources before exit (for fatal errors)?
> > 
> > The OS automatically recovers the process' memory when the process
> > terminates.
> 
> My copy of glibc would beg to differ (_MALLOC_CHECK environ variable).

1. Huh? What does _MALLOC_CHECK have to do with anything?

2. Your copy glibc is irrelevant. One a process has terminated, the
kernel will re-use its memory for other purposes. Nothing which the
program (or its libraries) does can affect this.

> Besides, it's just very bad practice not to explicitly free resources.

Explicitly free()ing memory when you know that the process is about to
terminate is a waste of CPU cycles. free() simply re-arranges
user-space data structures, i.e. adding the memory block to the free
list. Upon termination, the process' memory is going to vanish, so
there is no point in rearranging it.

The only reason for explicitly free()ing memory on shutdown is if you
want to re-use the code in a context where shutdown doesn't imply
process termination, i.e. where you can "close" a library but the
process keeps running. In that situation, the saving in CPU cycles
isn't worth making the code more complex by having separate cleanup
routines for the two cases.

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list