| | |
Malloc Fails
![]() |
•
•
Join Date: Sep 2006
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
Last edited by Ancient Dragon; Sep 18th, 2006 at 5:53 am.
•
•
Join Date: Sep 2006
Posts: 2
Reputation:
Solved Threads: 0
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.
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 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?
C Syntax (Toggle Plain Text)
for ( i = 0 ; i < 1000000 ; i++ ) { char *p = malloc( 100 ); free( p ); }
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
HPUX supports alloca() -
it has advantages and disadvantages
Example mediocre code:
This is HPUX code. Compile it and run it. Pay attention to the integral unshared memory.
it has advantages and disadvantages
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Drink Machines
- Next Thread: File Pointer in C
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






