943,929 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1872
  • C RSS
Sep 18th, 2006
1

Malloc Fails

Expand Post »
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.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
bhushanm is offline Offline
2 posts
since Sep 2006
Sep 18th, 2006
0

Re: Malloc Fails

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 18th, 2006
0

Re: Malloc Fails

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.
Reputation Points: 14
Solved Threads: 0
Newbie Poster
bhushanm is offline Offline
2 posts
since Sep 2006
Sep 18th, 2006
0

Re: Malloc Fails

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 18th, 2006
1

Re: Malloc Fails

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. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 18th, 2006
2

Re: Malloc Fails

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.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Drink Machines
Next Thread in C Forum Timeline: File Pointer in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC