Re: Doubts about free()



If I have a piece of code something like this

int main()
{

char *s1;

s1= (char *) malloc(sizeof(char) * 200)
while(somecondition)
{
................... //do something

}

free(s1);

while(true)
{
;
} //Infinite loop
}



This is just a sample code, please ignore the syntax errors.

In the above piece of code during execution malloc results in
allocating space physically in the RAM.

In a virtual memory system, it may or may not actually be allocated
physical RAM on allocation, and since you don't use the memory,
it might never be allocated physical RAM.

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.

It may not be possible to return a chunk of memory as small as 200
bytes to the OS. Memory is likely to be allocated and freed in
larger chunks. If you allocated 200 megabytes, it is more likely
that most of it could be returned.

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. 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.

Correct. This is called optimization. It may not be optimal in
all situations.

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.

In a virtual memory system, it is possible that the OS will take
the entire program out of memory (temporarily) to make room for
other programs.

Is there any way to force freeing
up the memory to the system back.

In a UNIX system this operation is often called 'kill'.

.



Relevant Pages

  • Re: [Announce]: Target_Core_Mod/ConfigFS and LIO-Target v3.0 work
    ... allocations would help the performance of a storage target. ... A single codepath memory allocating *AND* mapping for: ... Allocating multiple contigious struct page from the memory allocator ... I never claimed that RDMA is only possible from user space -- that was ...
    (Linux-Kernel)
  • How can I get memory allocated on PHYSICAL RAM and garanteed not on virtual ram.
    ... I'm finding the garanteed method of allocating memory on PHYSICAL RAM. ... I'm testing file copying with large memory cache. ...
    (microsoft.public.dotnet.framework)
  • Re: Forcing a Large Object Heap allocation.
    ... you should be passing the recommended 60% memory limit. ... that point and you should be experiencing an unstable app IMO. ... > compacts the heap. ... > objects in the regular heap causes poorer performance than allocating many ...
    (microsoft.public.dotnet.framework.performance)
  • Re: [00/41] Large Blocksize Support V7 (adds memmap support)
    ... some memory will be wasted. ... blocks with slab pages can be targetted and cleared if necessary. ... Allocating userpages from slab in 4k chunks with a 64k PAGE_SIZE is ... it remains a possibility to make the kernel more generic. ...
    (Linux-Kernel)
  • Re: ORACLE 9i - memory usage
    ... Oracle allocates 13GB (which is the total size of your SGA) at startup ... within physical memory. ... Can I guess it is actually allocating 13Gb of memory? ...
    (comp.databases.oracle.server)