Malloc Fails

Reply

Join Date: Sep 2006
Posts: 2
Reputation: bhushanm is an unknown quantity at this point 
Solved Threads: 0
bhushanm bhushanm is offline Offline
Newbie Poster

Malloc Fails

 
1
  #1
Sep 18th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Malloc Fails

 
0
  #2
Sep 18th, 2006
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.
Last edited by Ancient Dragon; Sep 18th, 2006 at 5:53 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 2
Reputation: bhushanm is an unknown quantity at this point 
Solved Threads: 0
bhushanm bhushanm is offline Offline
Newbie Poster

Re: Malloc Fails

 
0
  #3
Sep 18th, 2006
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Malloc Fails

 
0
  #4
Sep 18th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Malloc Fails

 
1
  #5
Sep 18th, 2006
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?
  1. for ( i = 0 ; i < 1000000 ; i++ ) {
  2. char *p = malloc( 100 );
  3. free( p );
  4. }
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Malloc Fails

 
2
  #6
Sep 18th, 2006
HPUX supports alloca() -
it has advantages and disadvantages
  1. good - it automatically frees all objects created by alloca calls when
  2. the function making those calls exits
  3.  
  4. good - it is about 100X faster than malloc
  5.  
  6. bad - it allocates from stack, not heap. stack space size limits
  7. are nowhere near a generous
  8. bad - it is REALLY easy to reference a pointer to garbage
  9. or trash the stack of another function
Example mediocre code:
  1.  
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/resource.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <alloca.h>
  9. #define MAX 100000
  10. void foo(void)
  11. {
  12. int i=0;
  13. char *p[MAX]={NULL};
  14. for ( i = 0 ; i < MAX ; i++ )
  15. p[i] = malloc( rand()%456 );
  16. for ( i = MAX ; i>0 ; i-- )free( p[i] );
  17.  
  18. }
  19. int foo1(void)
  20. {
  21. int i=0;
  22. char *p[MAX]={NULL};
  23. for ( i = 0 ; i < MAX ; i++ )
  24. p[i] = alloca( rand()%456 );
  25. }
  26.  
  27. void badfoo(void)
  28. {
  29. int i=0;
  30. char *p[MAX]={NULL};
  31. for ( i = 0 ; i < MAX ; i++ )
  32. p[i] = malloc( rand()%456 );
  33. }
  34.  
  35. void process(struct rusage *p, char *when)
  36. {
  37. printf("%s\n", when);
  38. printf(" /* user time used */ %8d %8d\n", p->ru_utime.tv_sec,p->ru_utime.tv_usec );
  39. printf(" /* system time used */ %8d %8d\n", p->ru_stime.tv_sec,p->ru_stime.tv_usec );
  40. printf(" /* integral shared memory size */ %8d\n", p->ru_ixrss );
  41. printf(" /* integral unshared data */ %8d\n", p->ru_idrss );
  42. printf(" /* integral unshared stack */ %8d\n", p->ru_isrss );
  43.  
  44. }
  45.  
  46.  
  47. int main()
  48. {
  49. int ret=0;
  50. int i=0;
  51. int who= RUSAGE_SELF;
  52. struct rusage usage;
  53. struct rusage *p=&usage;
  54.  
  55. ret=getrusage(who,p);
  56. process(p, "-------------before we run foo");
  57. foo();
  58. ret=getrusage(who,p);
  59. process(p, "\n\n-------------after we run foo");
  60. for (i=0;i<10;i++)
  61. {
  62. foo1();
  63. ret=getrusage(who,p);
  64. process(p, "\n\n-------------after we run foo1");
  65. }
  66. badfoo();
  67. ret=getrusage(who,p);
  68. process(p, "\n\n-------------after we run badfoo");
  69. return 0;
  70. }
This is HPUX code. Compile it and run it. Pay attention to the integral unshared memory.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC