hi,
in my code i am creating a tree, which have a 20 levels and and each level we have 10 nodes. i.e every node can have a 10 children. for them i am allocating memory for each node.
each node is stracuture which occupies the memmory around 40 bytes.
i have 456 files, each file have more then 18000 data, i.e. for each file 18000+ nodes are created. when coressponding output is written to ourfile for every file, memeory allocated is made free.

but after o the same for 450 files....my c code fails...
nor i am looking that how can i reuse the memory which had been free by my progrm again.

can anybody help.

what operating system and compiler? Might be that your program is fragmenting memory very badly or more likely your program is not freeing memory correctly. You might try allocating just one huge block of memory then allocating it among all those nodes.

what operating system and compiler? Might be that your program is fragmenting memory very badly or more likely your program is not freeing memory correctly. You might try allocating just one huge block of memory then allocating it among all those nodes.

i am working on a HP - UX....machine. and using c compiler. we had monitor the memeory use by program it crosess above 1GB, after free statmets also.

free() does not necessarily give the memory back to the os. C programs normally have a pretty efficient memory allocation algorithm, it does not release memory back to the os when deallocated by free(), but instead keeps it in a free list so that it is readily available the next time malloc() is called. So an external program monitoring the memory usage of another program may only be able to detect when new memory is grabbed from the os and not when free() is called.

I'm not a unix or HP expert, but you might check around to see if you can find a program that can detect memory leaks. For example, this article might be useful to you. And you might read some of the articles in these google links. Some of them apply to other operating systems which you can just ignore.

Use the debugger and put counting breakpoints on malloc() and free().

For a given file, you're saying that they should be equal.


BTW, if you're thinking that free(tree) frees the whole tree, then you would be wrong. From the symptoms you describe, I don't think you're freeing the whole tree at all.

Does this erode memory like your application does?

for ( i = 0 ; i < 1000000 ; i++ ) {
  char *p = malloc( 100 );
  free( p );
}

HPUX supports alloca() -
it has advantages and disadvantages

good - it automatically frees all objects created by alloca calls when
         the function making those calls exits

good - it is about 100X faster than malloc

bad - it allocates from stack, not heap.  stack space size limits
       are nowhere near a generous
bad - it is REALLY easy to reference a pointer to garbage 
       or trash the  stack of another function

Example mediocre code:

#include <stdlib.h>
#include <time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>     
#include <alloca.h>
#define MAX 100000
void foo(void)
{
	int i=0;
	char *p[MAX]={NULL};
	for ( i = 0 ; i < MAX ; i++ )
		p[i] = malloc( rand()%456 );
	for ( i = MAX ; i>0 ; i-- )free( p[i] );

}
int foo1(void)
{
	int i=0;
	char *p[MAX]={NULL};
	for ( i = 0 ; i < MAX ; i++ )
		p[i] = alloca( rand()%456 );	
}

void badfoo(void)
{
	int i=0;
	char *p[MAX]={NULL};
	for ( i = 0 ; i < MAX ; i++ )
		p[i] = malloc( rand()%456 );
}

void process(struct rusage *p, char *when)
{
	printf("%s\n", when);
	printf(" /* user time used */                   %8d  %8d\n",  p->ru_utime.tv_sec,p->ru_utime.tv_usec   );
	printf(" /* system time used */                 %8d  %8d\n",  p->ru_stime.tv_sec,p->ru_stime.tv_usec   );
	printf(" /* integral shared memory size */      %8d\n",  p->ru_ixrss           );
	printf(" /* integral unshared data  */          %8d\n",  p->ru_idrss           );
	printf(" /* integral unshared stack  */         %8d\n",  p->ru_isrss           );

}


int main()
{
	  int ret=0;
	  int i=0;
	  int who= RUSAGE_SELF;
	  struct rusage usage;
	  struct rusage *p=&usage;

	  ret=getrusage(who,p);
	  process(p, "-------------before we run foo");
	  foo();
	  ret=getrusage(who,p);
	  process(p, "\n\n-------------after we run foo");
	  for (i=0;i<10;i++)
	  {
	      foo1();
	      ret=getrusage(who,p);
	      process(p, "\n\n-------------after we run foo1");
      }	      
      badfoo();
      ret=getrusage(who,p);
	  process(p, "\n\n-------------after we run badfoo");
	return 0;
}

This is HPUX code. Compile it and run it. Pay attention to the integral unshared memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.