On May 13, 2:59pm, Malcolm Cohen wrote:
> Subject: Re: memory leak
> Pierre Hugonnet said:
>
> > I don't know if it is memory leak strictly speaking, but the
> > consequences are more or less the same:
> > if you don't ALLOCATE/DEALLOCATE in the LIFO order (last allocated,
> > first deallocated), you are likely to fragment the memory...
> Jack Scheible said:
> > This is not something I would worry about. Most computers now
> > automatically defragment memory, much as Unix systems automatically
> > defragment hard drives.
> This is completely false. The first author is talking about memory
> fragmentation from the point of view of the user's address space. He
> is correct.
>
> I don't know of any Fortran implementations that run a compacting garbage
> collector, which is the only thing which can "defragment memory".
>
> Nonetheless, I would not recommend worrying about it too much either, unless
> your program does a lot of mixed allocations and deallocations. The worst
> case scenario is pretty unlikely to happen. [Anyway, is this not why we
> all run with 100's of MBs of swap space on our desktop machines?]
I agree with Malcolm, but would like to point out the further twist
that most modern memory allocators do not, as Pierre seems to assume,
add each new allocated block onto the end of a growing chain, even
if one allocates successively without deallocation.
Most memory allocators anticipate subsequent allocations by getting
more memory from the OS than the user requests, so that there is
at least a chance that subsequent requests can be satisfied without
going back to the OS.
With this strategy, successive allocations, even if nothing is
deallocated, will not, in general, result in a sequence of increasing
addresses. Memory will be fragmented (or at least "holy") even in
this scenario. Furthermore, modern memory allocators do all kinds
of things to optimize against expected typical use patterns, and
to try to outsmart these strategies by trying to implement some
strategy of one's own -- like LIFO deallocation -- is not guaranteed
to help.
Finally, if the writer of a large program had to worry about LIFO
deallocation, chances are he would never get his program written. :-)
-P.
--
*********** How can we have ethnic music without ethnic hatred? ***********
* Peter S. Shenkin; Chemistry, Columbia U.; 3000 Broadway, Mail Code 3153 *
** NY, NY 10027; [log in to unmask]; (212)854-5143; FAX: 678-9039 ***
*MacroModel WWW page: http://www.columbia.edu/cu/chemistry/mmod/mmod.html *
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|