freeing malloced memory back the operating system. 
Author Message
 freeing malloced memory back the operating system.

A program I am writing a program which when freeing memory appears to not
reduce its memory usage (according to ps and top ). I believe this is because
most implementations of malloc/free do not return freed memory to the operating
system (Solaris UNIX and Linux in my case) but make it available for future
malloc calls.
Is there an implementation of malloc/free out there to enable the memory to be
truly freed. I am running an iterative batch process and I need the memory to
be completely available for the operating system. Any ideas - pointers to pointers?
Dave becalick


Mon, 24 Nov 1997 03:00:00 GMT  
 freeing malloced memory back the operating system.


Quote:
>A program I am writing a program which when freeing memory appears to not
>reduce its memory usage (according to ps and top ). I believe this is because
>most implementations of malloc/free do not return freed memory to the operating
>system (Solaris UNIX and Linux in my case) but make it available for future
>malloc calls.
>Is there an implementation of malloc/free out there to enable the memory to be
>truly freed. I am running an iterative batch process and I need the memory to
>be completely available for the operating system. Any ideas - pointers to pointers?
>Dave becalick

Warning: I'm posting secondhand info here...

    This problem came up in a system meeting today at our company.
The guru, whose knowledge I respect deeply, stated that gcc's
malloc/free package does correctly return memory to the OS, but
that many implementations (I'm most familiar with MS-DOS) do not.
    He recommended that we study the source to malloc & free; not
only do I plan to do so, I plan to implement my own "smart"
malloc/free package for my MS-DOS implementation.

    However, I see that you are using linux, and I had been under
the impression that linux uses gcc. Hmmm.

[using gcc may not imply using gcc's malloc/free.  also, "correctly" does
 not imply that it's done; no standard requires it.  :)  seriously, tho',
 it's a matter of the c library, more than the compiler, whether memory
 is returned to the OS.  -s]

--
----
Eric

"If anyone finds this offensive, I am prepared not only to retract my
words, but also to deny under oath that I ever said them." -- Tom Lehrer
----



Mon, 24 Nov 1997 03:00:00 GMT  
 freeing malloced memory back the operating system.

Quote:
>A program I am writing a program which when freeing memory appears to not
>reduce its memory usage (according to ps and top ). I believe this is because
>most implementations of malloc/free do not return freed memory to the operating
>system (Solaris UNIX and Linux in my case) but make it available for future
>malloc calls.
>Is there an implementation of malloc/free out there to enable the memory to be
>truly freed. I am running an iterative batch process and I need the memory to
>be completely available for the operating system. Any ideas - pointers to pointers?
>Dave becalick

It's difficult because if there is any allocated memory after the memory you
want to free, UNIX can't release the free memory in between.

Perhaps you could use shared memory instead?  I think that you can
create and attach to some shared memory, use it, and then discard it.

Dave
--

AT&T Bell Laboratories    (908)946-1107
943 Holmdel Road
Holmdel, NJ 07733



Wed, 26 Nov 1997 03:00:00 GMT  
 freeing malloced memory back the operating system.

Quote:
>    This problem came up in a system meeting today at our company.
>The guru, whose knowledge I respect deeply, stated that gcc's
>malloc/free package does correctly return memory to the OS, but
>that many implementations (I'm most familiar with MS-DOS) do not.

gcc is not a complete C implementation.  It is only a compiler, which
still uses the system supplied headers (with a few exceptions) and
libraries.  Your guru must have been talking about glibc, which is a
separate "product".

The big question is whether a free() implementation which returns the
memory to the OS is "correct" on a multitasking system.  I argued, in a
previous discussion on this subject, that such an implementation is
broken, based on this quote from the standard:

   The free function causes the space pointed to by ptr to be
   deallocated, that is, made available for further allocation.

My argument was that if the memory is returned to the OS, the OS might
give it to a different process and it won't be available to the original
one "for further allocation".  Some people disagreed with my interpretation.

Quote:
>    However, I see that you are using linux, and I had been under
>the impression that linux uses gcc. Hmmm.

See my comment about gcc above.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 26 Nov 1997 03:00:00 GMT  
 freeing malloced memory back the operating system.


Quote:

>> A program I am writing a program which when freeing memory appears to not
>> reduce its memory usage (according to ps and top ). I believe this is
>> because most implementations of malloc/free do not return freed memory to
>> the operating system (Solaris UNIX and Linux in my case) but make it
>> available for future malloc calls.  Is there an implementation of
>> malloc/free out there to enable the memory to be truly freed. I am running
>> an iterative batch process and I need the memory to be completely available
>> for the operating system. Any ideas - pointers to pointers?  Dave becalick

Eric> Warning: I'm posting secondhand info here...

Eric>     This problem came up in a system meeting today at our company.  The
Eric> guru, whose knowledge I respect deeply, stated that gcc's malloc/free
Eric> package does correctly return memory to the OS, but that many
Eric> implementations (I'm most familiar with MS-DOS) do not.  He recommended
Eric> that we study the source to malloc & free; not only do I plan to do so, I
Eric> plan to implement my own "smart" malloc/free package for my MS-DOS
Eric> implementation.

This would be very interesting if you succeed, but it is my understanding that
DOS reneges completely when it comes to basic operating system tasks such as
memory management.  I should come out with a disclaimer here; I'm not a
specialist in either DOS or operating systems.  Nevertheless, it is my
understanding that, when DOS executes a program, it gives the entire control of
all the system's resources to that program to do with as it sees fit, and only
takes over control again if and when the program exits normally.  This explains
why the system reboots if you make a programming error.  If I'm right, if you
free memory and give it to DOS, then DOS would not know what to do with it.

Anyway, there is really no problem with DOS if the memory is only freed in the
sense that it is made available for future malloc calls, as DOS also reneges
completely when it comes to such basic operating system tasks as managing
processor time.  Only one process is permitted to execute at any given moment.
When your program is running, there are no other processes competing for
memory, and even DOS has no need of it as it has already consumed all it needs
when it took control of the top 360 Kbytes of the only megabyte it can address,
thereby leaving only 640 Kbytes for your program.  There is therefore nothing
that DOS could do with any freed memory, except give it back to your program
for future malloc calls.  Take care, though, as your memory can get very
fragmented, and DOS does not help out there either.

        Cheers,
                Mike
--

Abteilung fuer Mathematische Optimierung  (oo)
Institut fuer Angewandte Mathematik        \/-------\
TU Braunschweig                             ||     | \
Pockelsstr. 14                              ||---W||  *
38106 Braunschweig, Germany                 ^^    ^^    Ph.: +49 (531) 391-7553



Wed, 26 Nov 1997 03:00:00 GMT  
 freeing malloced memory back the operating system.

Quote:



> >A program I am writing a program which when freeing memory appears to not
> >reduce its memory usage (according to ps and top ). I believe this is because
> >most implementations of malloc/free do not return freed memory to the operating
> >system (Solaris UNIX and Linux in my case) but make it available for future
> >malloc calls.
> >Is there an implementation of malloc/free out there to enable the memory to be
> >truly freed. I am running an iterative batch process and I need the memory to
> >be completely available for the operating system. Any ideas - pointers to pointers?
> >Dave becalick

A problem with returning freed memory is that, (in any architecture in which
sbrk is implemented normally and then used to supply space to malloc) return
to O.S. can only happen if there's absolutely no unfreed heap at higher
addresses.  It's hard to set this up because of libraries, etc., (consider the
standard alloca emulators!).  If you really need this behavior and do the work
needed to set it up, your program becomes unstable against minor changes and its
performance might deteriorate unexpectedly.

If your system includes explicit shared-memory operations (e.g., BSD-descended
systems, AIX, etc.)  you could well get the behavior you want by explicitly
attaching and detaching shared segments.  It's no more work than policing
your code for orphan strings, and it's all in one place.



Fri, 28 Nov 1997 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. freeing malloced memory back the operating system.

2. Freeing memory tothe Operating System

3. is MALLOCed memory freed up when program terminates

4. Q: Freeing malloced memory after strtoking it.

5. freed memory back to OS?

6. free space malloced to auto vars??

7. querying the size of a malloced/freed block

8. system() and freeing memory

9. Malloced memory - ahhhhh!

10. operating system

11. Operating System Vs. Applications? (Abstract Milieu)

12. operating system

 

 
Powered by phpBB® Forum Software