EmbeddedRelated.com
Forums

free - The Memory is still not returned back to OS

Started by karthikbalaguru February 25, 2008
On Feb 26, 2:11=A0am, CBFalconer <cbfalco...@yahoo.com> wrote:
> karthikbalaguru wrote: > > > 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 ? > > The following is the C standard description of free. =A0Note the > phrase "made available for further allocation". =A0It says nothing > about the OS, so that "further allocation" may be limited to the > process that executed the free function. > > If you want to see the code for an actual malloc/free application, > you can read nmalloc.zip, which is intended for DJGPP, but will > probably function unchanged on most Linux, Dos, or Windows > environments. =A0See: > > =A0 =A0 =A0<http://cbfalconer.home.att.net/download/> > > =A0 7.20.3.2 =A0The free function > =A0 Synopsis > =A0 [#1] > =A0 =A0 =A0 =A0 =A0 #include <stdlib.h> > =A0 =A0 =A0 =A0 =A0 void free(void *ptr); > > =A0 Description > > =A0 [#2] The free function causes the space pointed to by ptr to > =A0 be =A0 deallocated, =A0 that =A0is, =A0made =A0available =A0for =A0fur=
ther
> =A0 allocation. =A0If ptr is a null =A0pointer, =A0no =A0action =A0occurs.=
> =A0 Otherwise, =A0if the argument does not match a pointer earlier > =A0 returned by the calloc, malloc, or realloc function, =A0or =A0if > =A0 the space has been deallocated by a call to free or realloc, > =A0 the behavior is undefined. > > =A0 Returns > > =A0 [#3] The free function returns no value. > > --
Thx for that info. Yes, that clarifies a lot. :):) I got some more interesting discussions regarding 'free' in comp.lang.c and comp.std.c. free :- http://groups.google.com/group/comp.std.c/browse_thread/thread/930b55c2892df= 2f1/8510e07d4d24034d How do I know if free() really free()ed? :- http://groups.google.com/group/comp.lang.c/msg/8c93163a9fa6033d Function: free :- http://groups.google.com/group/comp.lang.c/msg/5f56cb7e08d330f9 "available for further allocation"? :- http://groups.google.co.in/group/comp.std.c/browse_thread/thread/602880aa31f= d7c14?hl=3Den Karthik Balaguru
On Feb 28, 8:13=A0am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
> On Feb 26, 2:11=A0am, CBFalconer <cbfalco...@yahoo.com> wrote: > > > > > > > karthikbalaguru wrote: > > > > 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 ? > > > The following is the C standard description of free. =A0Note the > > phrase "made available for further allocation". =A0It says nothing > > about the OS, so that "further allocation" may be limited to the > > process that executed the free function. > > > If you want to see the code for an actual malloc/free application, > > you can read nmalloc.zip, which is intended for DJGPP, but will > > probably function unchanged on most Linux, Dos, or Windows > > environments. =A0See: > > > =A0 =A0 =A0<http://cbfalconer.home.att.net/download/> > > > =A0 7.20.3.2 =A0The free function > > =A0 Synopsis > > =A0 [#1] > > =A0 =A0 =A0 =A0 =A0 #include <stdlib.h> > > =A0 =A0 =A0 =A0 =A0 void free(void *ptr); > > > =A0 Description > > > =A0 [#2] The free function causes the space pointed to by ptr to > > =A0 be =A0 deallocated, =A0 that =A0is, =A0made =A0available =A0for =A0f=
urther
> > =A0 allocation. =A0If ptr is a null =A0pointer, =A0no =A0action =A0occur=
s.
> > =A0 Otherwise, =A0if the argument does not match a pointer earlier > > =A0 returned by the calloc, malloc, or realloc function, =A0or =A0if > > =A0 the space has been deallocated by a call to free or realloc, > > =A0 the behavior is undefined. > > > =A0 Returns > > > =A0 [#3] The free function returns no value. > > > -- > > Thx for that info. Yes, that clarifies a lot. :):) > > I got some more interesting discussions regarding 'free' in > comp.lang.c and comp.std.c. > > free :-http://groups.google.com/group/comp.std.c/browse_thread/thread/930b=
55...
> > How do I know if free() really free()ed? :-http://groups.google.com/group/=
comp.lang.c/msg/8c93163a9fa6033d
> > Function: free :-http://groups.google.com/group/comp.lang.c/msg/5f56cb7e08=
d330f9
> > "available for further allocation"? :-http://groups.google.co.in/group/com=
p.std.c/browse_thread/thread/6028...
>
Freeing memory - will it be available immediately http://groups.google.com/group/comp.lang.c/browse_thread/thread/962bea6f78be= 0959 Thx, Karthik Balaguru
karthikbalaguru wrote:
>
... snip ...
> > Freeing memory - will it be available immediately
Probably. No guarantee. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com
In article <u_GdnXtda9r0BF_anZ2dnUVZ_hWdnZ2d@giganews.com>,
badal_akr <badal_akr@yahoo.com> 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.
I don't have the original post anymore. So I answer here. Apparent from the reaction of the OP this is not the way the question is to be understood. It is a question asked from all free developers, and not about a particular free implementation. It could be rephrased: What are the criteria for free developers to return the memory to the OS, or not? A typical Unix application will allocate from a pool, and ask memory from the OS only if the pool runs out. On freeing, the memory is returned to the pool. The main reason is that everything involving the OS is more costly than a simple memory allocation that stays within a process. On a virtual memory OS there are hardly disadvantages to having memory allocated that is not used. In the worst case it is swapped out and is no longer a drain on the RAM memory resource. At the end of a process, the memory is returned anyway. Sometimes an OS is lightweight and all allocations and free's directly involve the OS (e.g. in Forth if that is not too lightweigt to be called an OS). Some care has to be taken for processes that run indefinitely. If they keep on allocating memory and don't free it (memory leak), memory runs out, wherever it is allocated from. This is a totally separate issue. Groetjes Albert. -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- like all pyramid schemes -- ultimately falters. albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
On Feb 28, 6:56=A0pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
wrote:
> In article <u_GdnXtda9r0BF_anZ2dnUVZ_hWdn...@giganews.com>, > > badal_akr <badal_...@yahoo.com> 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. > > I don't have the original post anymore. So I answer here. > Apparent from the reaction of the OP this is not the way > the question is to be understood. > It is a question asked from all free developers, > and not about a particular free implementation. > > It could be rephrased: > What are the criteria for free developers to return the memory > to the OS, or not? > > A typical Unix application will allocate from a pool, and ask memory from > the OS only if the pool runs out. On freeing, the memory is returned > to the pool. The main reason is that everything involving the OS is > more costly than a simple memory allocation that stays within a > process. On a virtual memory OS there are hardly disadvantages to > having memory allocated that is not used. In the worst case it is > swapped out and is no longer a drain on the RAM memory resource. At > the end of a process, the memory is returned anyway. > > Sometimes an OS is lightweight and all allocations and free's > directly involve the OS (e.g. in Forth if that is not too lightweigt > to be called an OS). > > Some care has to be taken for processes that run indefinitely. > If they keep on allocating memory and don't free it (memory leak), > memory runs out, wherever it is allocated from. This is a totally > separate issue. >
Is there any open source tool that will tell the amount of " memory freed but not returned back to the OS " ? Thx in advans, Karthik Balaguru
On Mar 3, 10:02=A0am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
> On Feb 28, 6:56=A0pm, Albert van der Horst <alb...@spenarnc.xs4all.nl> > wrote: > > > > > > > In article <u_GdnXtda9r0BF_anZ2dnUVZ_hWdn...@giganews.com>, > > > badal_akr <badal_...@yahoo.com> 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 implementatio=
n
> > >of the free and ask the free,s developper. > > > I don't have the original post anymore. So I answer here. > > Apparent from the reaction of the OP this is not the way > > the question is to be understood. > > It is a question asked from all free developers, > > and not about a particular free implementation. > > > It could be rephrased: > > What are the criteria for free developers to return the memory > > to the OS, or not? > > > A typical Unix application will allocate from a pool, and ask memory fro=
m
> > the OS only if the pool runs out. On freeing, the memory is returned > > to the pool. The main reason is that everything involving the OS is > > more costly than a simple memory allocation that stays within a > > process. On a virtual memory OS there are hardly disadvantages to > > having memory allocated that is not used. In the worst case it is > > swapped out and is no longer a drain on the RAM memory resource. At > > the end of a process, the memory is returned anyway. > > > Sometimes an OS is lightweight and all allocations and free's > > directly involve the OS (e.g. in Forth if that is not too lightweigt > > to be called an OS). > > > Some care has to be taken for processes that run indefinitely. > > If they keep on allocating memory and don't free it (memory leak), > > memory runs out, wherever it is allocated from. This is a totally > > separate issue. > > Is there any open source tool that will tell the amount of > " memory freed but not returned back to the OS " ? >
I understand that the memory freed depends on the type of memory management technique, then, we should be having a corresponding tool to clearly state the amount memory freed but still with the application/process and not yet returned back to the OS. But, Do normal tools take care of this or Do they convey the info based on the amount of memory freed (That is , they do not worry about the amount of memory returned to the OS and display only the memory freed) ? How about this in linux ? Thx in advans, Karthik Balaguru
karthikbalaguru wrote:
> On Mar 3, 10:02 am, karthikbalaguru <karthikbalagur...@gmail.com> > wrote: > >>On Feb 28, 6:56 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl> >>wrote: >> >> >> >> >> >> >>>In article <u_GdnXtda9r0BF_anZ2dnUVZ_hWdn...@giganews.com>, >> >>>badal_akr <badal_...@yahoo.com> 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. >> >>>I don't have the original post anymore. So I answer here. >>>Apparent from the reaction of the OP this is not the way >>>the question is to be understood. >>>It is a question asked from all free developers, >>>and not about a particular free implementation. >> >>>It could be rephrased: >>>What are the criteria for free developers to return the memory >>>to the OS, or not? >> >>>A typical Unix application will allocate from a pool, and ask memory from >>>the OS only if the pool runs out. On freeing, the memory is returned >>>to the pool. The main reason is that everything involving the OS is >>>more costly than a simple memory allocation that stays within a >>>process. On a virtual memory OS there are hardly disadvantages to >>>having memory allocated that is not used. In the worst case it is >>>swapped out and is no longer a drain on the RAM memory resource. At >>>the end of a process, the memory is returned anyway. >> >>>Sometimes an OS is lightweight and all allocations and free's >>>directly involve the OS (e.g. in Forth if that is not too lightweigt >>>to be called an OS). >> >>>Some care has to be taken for processes that run indefinitely. >>>If they keep on allocating memory and don't free it (memory leak), >>>memory runs out, wherever it is allocated from. This is a totally >>>separate issue. >> >>Is there any open source tool that will tell the amount of >>" memory freed but not returned back to the OS " ? >> > > > I understand that the memory freed depends on the type of memory > management technique, then, we should be having a corresponding tool > to clearly state the amount memory freed but still with the > application/process > and not yet returned back to the OS. But, Do normal tools take care of > this or > Do they convey the info based on the amount of memory freed (That is , > they > do not worry about the amount of memory returned to the OS and display > only > the memory freed) ? How about this in linux ? > > Thx in advans, > Karthik Balaguru
In a demand-paged virtual-memory system, malloc() and friends allocate address space, not memory directly. The address space is separate for each process, so that different processes will be using same addresses for different data. The physical memory is connected with the address space when an access is attempted. The connection is performed in units called pages. Common page size is 4 kilobytes. In Linux, you'll find the page maps of each process in the /proc file system. There is a directory for each process with the process ID as the directory name. The memory mapping is in the directory under name maps. For details, get a book on Linux kernel, e.g. Understanding the Linux Kernel, by Daniel P. Bovet and Marco Cesati. -- Tauno Voipio tauno voipio (at) iki fi
karthikbalaguru wrote:

> Is there any open source tool that will tell the amount of > " memory freed but not returned back to the OS " ?
If you're using glibc version of malloc/free, check out the mallinfo() function. It returns a structure containing the current state of memory allocation. http://www.delorie.com/djgpp/doc/libc/libc_550.html On Linux, you may also use sbrk(0) to find out the extend of the program's data space.
Arlet Ottens wrote:
> karthikbalaguru wrote: > >> Is there any open source tool that will tell the amount of >> " memory freed but not returned back to the OS " ? > > If you're using glibc version of malloc/free, check out the > mallinfo() function. It returns a structure containing the > current state of memory allocation. > > http://www.delorie.com/djgpp/doc/libc/libc_550.html > > On Linux, you may also use sbrk(0) to find out the extend of > the program's data space.
All those functions are also available in the malldbg module for nmalloc, for DJGPP (and most Linux systems). nmalloc avoids various problems. See nmalloc.zip, at: <http://cbfalconer.home.att.net/download/> -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com