Question regarding memory allocation/deallocation 
Author Message
 Question regarding memory allocation/deallocation

Hello,

I have a basic question about memory usage. I am writing a
program in C on a linux (2.0.31 kernel, gcc ver 2.7.2.3-8)
machine. The program calls functions that allocate (and then
deallocates) large amounts of memory. My question is, as I
track the progress of the program and the memory it accumulates
as it runs (according to the "top" Linux/UNIX command), the
program memory usage appears to be cumulative, rather than
returning memory once it has been deallocated in the function.
The reason I ask is that as the program runs and I get further
into it, I am getting alot of swapping to disk for virtual
memory, and I am get dangerously low on virtual memory.  

BTW I am using calloc() and free() to allocate/deallocate memory.
I used the -O3 optimization option during compilation/building.

Is this a basic fact of life in C programs, or is there something
else that I am missing?

Many thanks to anyone who can help.

Chris

(remove the mapson in the address to respond by email)



Mon, 25 Sep 2000 03:00:00 GMT  
 Question regarding memory allocation/deallocation

purify or the like will find your leak.
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Tue, 26 Sep 2000 03:00:00 GMT  
 Question regarding memory allocation/deallocation

Quote:

> I have a basic question about memory usage. I am writing a
> program in C on a linux (2.0.31 kernel, gcc ver 2.7.2.3-8)
> machine. The program calls functions that allocate (and then
> deallocates) large amounts of memory. My question is, as I
> track the progress of the program and the memory it accumulates
> as it runs (according to the "top" Linux/UNIX command), the
> program memory usage appears to be cumulative, rather than
> returning memory once it has been deallocated in the function.

Well, considering that you're getting a lot of swappage, you probably have
a memory leak; there are a lot of packages created to help find these, and
I believe some are suggested in the C FAQ. (But try to give your code a good
once-over before relying on these. :)  However, it is important to note that
the 'cumulative' memory report from 'top' is normal for many implimentations;
on these, 'free()' does not actually return the memory to the operating
system, but, instead, adds it to an internal linked-list of free memory, to
avoid the overhead of having to request more from the OS for each allocation.

Again, the C FAQ has more detail on this issue.

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.



Tue, 26 Sep 2000 03:00:00 GMT  
 Question regarding memory allocation/deallocation

On Thu, 09 Apr 1998 22:20:04 -0700, Christopher Quale

Quote:

>Hello,

>I have a basic question about memory usage. I am writing a
>program in C on a linux (2.0.31 kernel, gcc ver 2.7.2.3-8)
>machine. The program calls functions that allocate (and then
>deallocates) large amounts of memory. My question is, as I
>track the progress of the program and the memory it accumulates
>as it runs (according to the "top" Linux/UNIX command), the
>program memory usage appears to be cumulative, rather than
>returning memory once it has been deallocated in the function.
>The reason I ask is that as the program runs and I get further
>into it, I am getting alot of swapping to disk for virtual
>memory, and I am get dangerously low on virtual memory.  

>BTW I am using calloc() and free() to allocate/deallocate memory.
>I used the -O3 optimization option during compilation/building.

>Is this a basic fact of life in C programs, or is there something
>else that I am missing?

There are two things at work here:
  1) free() does not return memory to the operating system, in Unix.
     The C standard merely says that the space is "made available for
     further allocation."   This means that it gets kept by your
     process, and allocated by further calls to calloc().  So you'll
     never see an ANSI C program under Unix shrink in the amount of
     memory usage -- it only grows.  In order to shrink the size of
     your process, you'll need to use Unix system calls like brk() and
     sbrk(), but I DON'T recommend this -- it's incredibly risky unless
     you can guarantee that nothing (including library functions) is
     using malloc()/calloc().
  2) If the process memory usage grows cumulatively, then most likely
     you have a "memory leak" somewhere, and you're not freeing
     everything that you think you are.  Look for error returns from
     functions that may be allocating memory and then not freeing it
     if there's an error, and if nothing obvious appears, I suggest
     putting in fprintf() statements everywhere you call calloc() and
     free(), and reporting what the pointer values are, and where
     they're being allocated and freed.  I predict that you'll find some
     memory that's never getting freed.

--

Kenan Systems Corporation



Tue, 26 Sep 2000 03:00:00 GMT  
 Question regarding memory allocation/deallocation

On Thu, 09 Apr 1998 22:20:04 -0700, Christopher Quale

Quote:

>Hello,

>I have a basic question about memory usage. I am writing a
>program in C on a linux (2.0.31 kernel, gcc ver 2.7.2.3-8)
>machine. The program calls functions that allocate (and then
>deallocates) large amounts of memory. My question is, as I
>track the progress of the program and the memory it accumulates
>as it runs (according to the "top" Linux/UNIX command), the
>program memory usage appears to be cumulative, rather than
>returning memory once it has been deallocated in the function.
>The reason I ask is that as the program runs and I get further
>into it, I am getting alot of swapping to disk for virtual
>memory, and I am get dangerously low on virtual memory.  

>BTW I am using calloc() and free() to allocate/deallocate memory.
>I used the -O3 optimization option during compilation/building.

>Is this a basic fact of life in C programs, or is there something
>else that I am missing?

   This is somewhat implementation specific.  However, on a lot of
platforms malloc() and calloc() will cause the system to give you more
memory, but free() merely puts it into an unused list, and does not
return it to the system.  When your program ends then all the memory
is returned to the system.  There are usualy performance reasons why
this is so.

   There *are* ways of returning memory to the system, but these are
*highly* machine/OS specific, and cannot be thought of as 'portable'.
Often they entail having a special memory allocator, rather than using
the standard malloc() calls.

   Regards,
           Harry.



Wed, 27 Sep 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. COM question in regards to memory allocation

2. memory allocation/deallocation for tree (a prelimary summary)

3. Strange memory allocation and deallocation errors:

4. Memory Allocation and DeAllocation (new and free) !?

5. DLL Memory Allocation and Deallocation

6. Question about Memory Deallocation in Explorer App

7. Tree memory allocation, pool memory allocation

8. Allocation and deallocation of exceptions...

9. repeated allocation and deallocation

10. allocation/deallocation warnings in splint

11. CObjArray allocation/deallocation

12. Heap Allocation/Deallocation in a DLL

 

 
Powered by phpBB® Forum Software