Re: Doubts about free()
- From: David Schwartz <davids@xxxxxxxxxxxxx>
- Date: Fri, 31 Oct 2008 12:55:02 -0700 (PDT)
On Oct 31, 3:12 am, pushpakul...@xxxxxxxxx wrote:
In the above piece of code during execution malloc results in
allocating space physically in the RAM.
Yes, if and only if the system library and operating system think
that's the right thing to do. All that is guaranteed is that address
space local to this process be reserved.
Now after the first while loop
execution is complete free(s1) is called. At this point of time is
memory returned back to the OS or not always. I guess memory is just
marked as freed for future use by the same process.
Your use of the term "memory" is ambiguous. It could mean physical
memory, virtual memory, or some confused combination of the two. Since
all that had to be reserved was address space local to the process,
that's all that has to be recovered, and it is on every implementation
I know of.
I have read that in some implementations of OS like some unix flavors
even after free() is executed memory is not released back to the
system.
If you're talking about physical memory, which is probably the only
thing that's scarce, modern operating systems never give that to a
process without retaining the ability to recover it if needed. The
exception would be specific calls to lock memory. Physical memory
mapped to meet the demands of 'malloc' is never locked into the
process. It can always be recovered if physical memory can better be
used elsewhere.
In the above piece of code is it the case that the memory is
never freed up at all in such implementations and remains tied up till
the process dies.
If you mean system wide virtual memory, probably. If you mean physical
memory, definitely not.
Assuming there is a need for memory by some other process and no other
free memory is available, is it the case malloc for the other process
would fail under such circumstances. Does the OS do some kind of
tracking and free up this memory. Is there any way to force freeing
up the memory to the system back.
It's almost impossible to answer that, because it's not clear what you
mean by "memory". In normal circumstances, only physical memory would
be scarce. Physical memory can easily be reclaimed by the OS.
DS
.
- References:
- Doubts about free()
- From: pushpakulkar
- Doubts about free()
- Prev by Date: Re: srand
- Next by Date: Arrays as function parameters and sizeof()
- Previous by thread: Re: Doubts about free()
- Next by thread: 9 / 11 payback time
- Index(es):
Relevant Pages
|