Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Freeing memory - will it be available immediately (http://www.velocityreviews.com/forums/t593985-freeing-memory-will-it-be-available-immediately.html)

karthikbalaguru 02-25-2008 09:10 AM

Freeing memory - will it be available immediately
 
Hi,

Will 'free' return the memory Immediately to the OS ?

Thx in advans,
Karthik Balaguru


Mark Bluemel 02-25-2008 09:18 AM

Re: Freeing memory - will it be available immediately
 
karthikbalaguru wrote:
> Hi,
>
> Will 'free' return the memory Immediately to the OS ?


I believe Harold Pinter, when directing plays he wrote, would answer
questions about the characters, their motivation and so on by saying
"the text doesn't tell us"...

That's the answer here...

Nick Keighley 02-25-2008 09:23 AM

Re: Freeing memory - will it be available immediately
 
On 25 Feb, 09:10, karthikbalaguru <karthikbalagur...@gmail.com> wrote:

> Will 'free' return the memory Immediately to the OS ?


usually no. In fact usually the memory is only returned to the OS
when the program terminates. But this is all implementation
dependent.

--
Nick Keighley
"but that's the worse thing that could possibly happen!
but it's worse than that!"

an engineer on hearing of a fault

karthikbalaguru 02-25-2008 09:49 AM

Re: Freeing memory - will it be available immediately
 
On Feb 25, 4:00 pm, WANG Cong <xiyou.wangc...@gmail.com> wrote:
> On Mon, 25 Feb 2008 01:10:49 -0800,karthikbalaguru wrote:
>
> > Will 'free' return the memory Immediately to the OS ?

>
> It depends on how your free() is implemented.
>


Thx for your replies.
I agree on your view.

> Most libc's use a complex algorithm to do memory manangement,
> in order to get the speed, maybe they won't give the memory
> to OS immediately after free().


Interesting design followed by various OS .
I was thinking over this and have some queries based on this.

Generally,
If it does not return the memory immediately, then ,
- Will it not drain the memory ?
- Will it not add more load to the OS in managing it by still being
in the memory(hidden) ?
- Will it not cause problems if i allocate some memory immediately
after calling free (as it is not freed immediately).
- Why do some memory management design return the memory immediately
to the OS and some at the later stage. Is there any specific
advantage in either of these designs ?

Thx in advans,
Karthik Balaguru

Mark McIntyre 02-25-2008 10:11 AM

Re: Freeing memory - will it be available immediately
 
karthikbalaguru wrote:
> Generally,
> If it does not return the memory immediately, then ,
> - Will it not drain the memory ?
> - Will it not add more load to the OS in managing it by still being
> in the memory(hidden) ?
> - Will it not cause problems if i allocate some memory immediately
> after calling free (as it is not freed immediately).


Yes to all these.

> - Why do some memory management design return the memory immediately
> to the OS and some at the later stage. Is there any specific
> advantage in either of these designs ?


The answer is implementation-specific and offtopic in CLC.
comp.programming might possibly be a better place.

Vijay Kumar R. Zanvar 02-25-2008 10:18 AM

Re: Freeing memory - will it be available immediately
 
On Feb 25, 2:10 pm, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
> Hi,
>
> Will 'free' return the memory Immediately to the OS ?
>
> Thx in advans,
> Karthik Balaguru


No and yes, but usually no. The standard library implementation may
not release the acquired memory to the OS. However, the program can
not use it anymore once freed.

Thanks,
Vijay Zanvar
http://faq.zanvar.in

Richard Tobin 02-25-2008 10:39 AM

Re: Freeing memory - will it be available immediately
 
In article <cae95485-18b4-4545-ac9c-c27d18bc12f5@e10g2000prf.googlegroups.com>,
karthikbalaguru <karthikbalaguru79@gmail.com> wrote:

>If it does not return the memory immediately, then ,

....
>- Will it not cause problems if i allocate some memory immediately
> after calling free (as it is not freed immediately).


You seem to be misunderstanding. If the program doesn't return
the memory to the OS, it's almost certainly because it's keeping
it to use for future allocations in the same program.

It's very likely - though not of course guaranteed - that if you do

p = malloc(sizeof(*p));
free(p);
p = malloc(sizeof(*p));

then malloc() will return the same value both times.

Always returning memory to the OS is likely to be a very poor
strategy, because the overhead of calling the OS is usually very high
compared with having the program manage memory itself. The down side
is that the memory isn't available to other programs, but (a) in
practice, few programs allocate a large amount of memory, free it, and
don't allocate similar quantities later, and (b) on a system with
virtual memory it won't affect the availability of real memory, at
least for chunks of memory large compared with a page.

-- Richard
--
:wq

WANG Cong 02-25-2008 11:00 AM

Re: Freeing memory - will it be available immediately
 
On Mon, 25 Feb 2008 01:10:49 -0800karthikbalaguru wrote

> Will 'free' return the memory Immediately to the OS ?


It depends on how your free() is implemented.

Most libc's use a complex algorithm to do memory manangement,
in order to get the speed, maybe they won't give the memory
to OS immediately after free().

The C std just can guarantee that after your free(), that
memory block can be used for further malloc().

santosh 02-25-2008 11:13 AM

Re: Freeing memory - will it be available immediately
 
karthikbalaguru wrote:

> On Feb 25, 4:00 pm, WANG Cong <xiyou.wangc...@gmail.com> wrote:
>> On Mon, 25 Feb 2008 01:10:49 -0800?karthikbalaguru wrote?
>>
>> > Will 'free' return the memory Immediately to the OS ?

>>
>> It depends on how your free() is implemented.
>>

>
> Thx for your replies.
> I agree on your view.


Please see a recent thread we had here and over in comp.std.c on whether
the memory given to free is kept around by the malloc system for future
allocations or whether it is (or can be) given back to the OS.

>> Most libc's use a complex algorithm to do memory manangement,
>> in order to get the speed, maybe they won't give the memory
>> to OS immediately after free().

>
> Interesting design followed by various OS .
> I was thinking over this and have some queries based on this.
>
> Generally,
> If it does not return the memory immediately, then ,
> - Will it not drain the memory ?


Virtual memory yes.

> - Will it not add more load to the OS in managing it by still being
> in the memory(hidden) ?


No.

> - Will it not cause problems if i allocate some memory immediately
> after calling free (as it is not freed immediately).


I think you are misunderstanding what freeing immediately actually
means. In fact the standard has nothing to say about when it is freed
and it could mean many things in many systems. I would guess that free
would remove the memory from the allocated list and add it to the free
list, at a minimum. This has to be done. Whether it also returns the
memory to the OS, i.e., shrinks the data segment of the process, is
totally implementation specific. Some systems do not even have an OS,
some systems have OSes that are too primitive for such flexibility and
so on.

Your best bet is to examine a few malloc/free implementations for
systems of your interest.

> - Why do some memory management design return the memory immediately
> to the OS and some at the later stage. Is there any specific
> advantage in either of these designs ?


There is no consensus (nor a requirement in the standard) that memory
given to free need be returned to the OS *at* *all* .

The presence of virtual memory and paging will complicate these
decisions. As I said this is upto each malloc implementation to decide
and the only way to say for sure is to examine the sources for your
implementation and keep in mind that they apply only to that
implementation.

<snip>


WANG Cong 02-25-2008 11:33 AM

Re: Freeing memory - will it be available immediately
 
On Mon, 25 Feb 2008 01:49:05 -0800karthikbalaguru wrote

{snip}

> Interesting design followed by various OS . I was thinking over this and
> have some queries based on this.
>
> Generally,
> If it does not return the memory immediately, then , - Will it not drain
> the memory ?


Yes, of course. Your system has constant memory.

> - Will it not add more load to the OS in managing it by still being
> in the memory(hidden) ?


No, it can apply more memory first, then manage them by *itself* and
finally, when your program exits, gives all back to OS together.

> - Will it not cause problems if i allocate some memory immediately
> after calling free (as it is not freed immediately).


No. malloc() or something like that is also controlled by libc,
it can alloc from its own memory instead of OS's.

> - Why do some memory management design return the memory immediately
> to the OS and some at the later stage. Is there any specific advantage
> in either of these designs ?


The advantage of this is that it consumes less memory.
Maybe it works in a system has limited memory. ;)



All times are GMT. The time now is 07:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.