Re: free - The Memory is still not returned back to OS



karthikbalaguru wrote:
On Feb 25, 4:30 pm, David Brown <da...@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
badal_akr wrote:
Hi,
If the 'free' does not return the memory back to the OS, then what is
the advantage of it. Will it not drain the memory in certain
scenarios ?
Why does free' of certain OS return the memory back to OS while others
do not ?
Thx in advans,
Karthik Balaguru
If free does not return the memory to OS then look at the implementation
of the free and ask the free,s developper.
"free" can be implemented in many ways, depending on the OS (if any) and
the library. It is not uncommon for there to be a "cache" of heap
between the application level and the operating system. Thus when the
application asks for more memory (i.e., calls malloc() or a related
library function), it may get memory from this cache without needing an
OS call. If this cache does not have enough spare memory, the library
function will ask the OS for more. When freeing memory, the application
calls free() which returns the memory to the cache - the library
functions will only return the memory to the OS when an entire block is
unused (and it may not return it even then). Such a strategy can reduce
fragmentation in the main memory, it can improve data locality, and it
can allow the main OS memory manager and the library (or
application-level memory manager) to have different allocation schemes.

It is also quite possible to have free() do nothing - that's a valid
strategy for embedded systems which might use malloc() for convenience
during initialisation, but do not allocate and free memory during the
application itself.

Once we know what OS the O/P is talking about, and some more information
about the particular usage, it might be possible to help.

I came across an info that claimed that free does not return the
memory to the OS immediately and it returns at the end of the prorcess/
application.

That depends on the OS, and the library used by the application - again, you'll get little useful information until you tell us what OS and tools you are using.

In the internet i found that some Memory Management techniques allow
the immediate return of the freed memory to the OS and others allow
only at the end of the application / process .

Both are perfectly valid techniques, but are appropriate for different sorts of systems. It is the library used by the application that decides which method is used.

So, i started to look for the reasons in the internet . I got some
answers , but
they were not very clear / conclusive, so i posted my query here.


Your posts are not clear either - we don't know what tools you are using, or where you have found this information on the web. We also don't know what you have tried for yourself. Have you looked at the documentation and the source for your application, your library, and your OS?
.



Relevant Pages

  • Re: Next patches for the 2.6.25 queue
    ... It might be some workload with markers using Immediate Values or ... locality and won't trigger many cache misses after the first loop. ... I instrumented getppidwith 40 markers, so the impact of memory reads ...
    (Linux-Kernel)
  • Re: Next patches for the 2.6.25 queue
    ... It might be some workload with markers using Immediate Values or ... locality and won't trigger many cache misses after the first loop. ... I ran, in userspace, a program that does random memory access ... Since each markers is using ...
    (Linux-Kernel)
  • Re: [patch 2/8] Immediate Values - Architecture Independent Code
    ... which use a load immediate to remove a data cache hit. ... Immediate values refer to the memory address of a previously declared integer. ... Random walk L1 and L2 trashing surrounding a getppid() call: ...
    (Linux-Kernel)
  • Re: Cached memory never gets released
    ... Stock linux 2.4.26 kernel. ... Due to flash bug 3M of memory gets lost due to font memory getting lost ... The output of "free" cache number steadily grows. ... longer to exhaust all of system memory with the cache. ...
    (Linux-Kernel)
  • Re: Problem: Creating a raw binary string
    ... > While its true that a 64-bit cpu will move twice the data per instruction it ... > Memory bus width plays an important role here and unless it too is widened / ... You are forgetting the two levels of cache in the processor. ... The memory chips are addressed in Row col fashion. ...
    (alt.comp.lang.borland-delphi)