The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
writing file, memory not released
Discuss writing file, memory not released in the C Programming forum on Dev Shed. writing file, memory not released C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 20th, 2005, 08:51 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 8
Time spent in forums: 2 h 12 m 26 sec
Reputation Power: 0
|
|
|
writing file, memory not released
i use redhat linux, gcc compiler. in my code, i write out data at certain time interval with different file names, and i use fopen and fclose after each write-out. but when i check the memory usage, it keeps increasing after each call to output the data. ??? any help is appreciated... thanks.
|

June 20th, 2005, 08:53 AM
|
|
Contributing User
|
|
Join Date: Oct 2004
Posts: 235

Time spent in forums: 3 Days 14 h 27 m 20 sec
Reputation Power: 9
|
|
|
show code. Are u allocating memory dynamically?
|

June 20th, 2005, 10:06 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 8
Time spent in forums: 2 h 12 m 26 sec
Reputation Power: 0
|
|
|
writing file, memory not released
at the beginning, i allocate variable q dynamically. and q is kept throughout the running process (no free and re-allocate is done). basically the code:
PHP Code:
main()
{
allocate q[isize][jsize]
time loop
do something with the data
if plot_time is reached
plotdata() //write out q
end time loop
}
void plotdata()
{
FILE*fplot
fplot = fopen(filename,"w") //filename is different for different time
writeout data
fclose(fplot)
}
|

June 20th, 2005, 10:11 AM
|
|
Contributing User
|
|
Join Date: Oct 2004
Posts: 235

Time spent in forums: 3 Days 14 h 27 m 20 sec
Reputation Power: 9
|
|
|
This pseudo-code does not help at all... what does plotdata do? allocating by q[n][p] is NOT called dynamic allocation. Dynamic allocation uses malloc() or new...
|

June 20th, 2005, 12:35 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 8
Time spent in forums: 2 h 12 m 26 sec
Reputation Power: 0
|
|
|
oh...i just put allocate q[n][m]... but in fact, i use malloc to allocate memory for q and this is done correctly, i've checked.
in plotdata()... just writing out q to a data file using
fprintf(fplot,"...."); and nothing else. then the file is closed after it's done.
could this have something to do with the compiler?
|

June 20th, 2005, 12:57 PM
|
 |
Contributing User
|
|
|
|
Quote: | Originally Posted by onebug could this have something to do with the compiler? | Generally not. I'd post your actual code first.
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
|

June 20th, 2005, 01:46 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 8
Time spent in forums: 2 h 12 m 26 sec
Reputation Power: 0
|
|
i cut out some unnecessary details (ex: initializing some vars, etc.). okay here goes:
PHP Code:
int main(void)
{
int iter;
double tcount;
iter = 0;
mem_alloc();
while (time_current < time_end) {
iter++;
time_current += dtmin;
tcount += dtmin;
if (tcount >= tplot) {
tcount = tcount - tplot;
plotdata(iter);
}
}
return 0;
}
PHP Code:
void plotdata(const int iter)
{
short i,j;
FILE *fplot;
char filename[20];
sprintf(filename,"data/%05d.dat",iter);
fplot = fopen(filename,"w");
for (j = 0; j < NYMAX; j++)
for(i = 0; i < NXMAX; i++) {
fprintf(fplot,"%15.10e %15.10 %10.3e \n",x[i],y[j],q[i][j][0]);
}
fclose(fplot);
}
PHP Code:
void mem_alloc()
{
short i,j,nvars;
nvars = 2;
q = (double ***) malloc(NXMAX*sizeof(double **));
for (i = 0; i < NXMAX; i++) {
q[i] = (double **) malloc(NYMAX*sizeof(double *));
for (j = 0; j < NYMAX; j++) {
q[i][j] = (double *) malloc(nvars*sizeof(double));
}
}
}
|

June 20th, 2005, 02:04 PM
|
|
Contributing User
|
|
Join Date: Oct 2004
Posts: 235

Time spent in forums: 3 Days 14 h 27 m 20 sec
Reputation Power: 9
|
|
|
I honestly don't see any memory leaks except in ur mem_alloc() function which is called only once. Perhaps they're in the "unnecessary details"....
|

June 21st, 2005, 06:57 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 8
Time spent in forums: 2 h 12 m 26 sec
Reputation Power: 0
|
|
thanks anyway.
i am almost 100% sure it's not in the "unnecessary details". anyone else has any suggestion? 
|

June 21st, 2005, 07:03 AM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
|
How much does the memory increase by? Does the amount of increase match up w/ any amount of data that you know might be written/created by the program?
|

June 21st, 2005, 11:16 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 8
Time spent in forums: 2 h 12 m 26 sec
Reputation Power: 0
|
|
|
i don't have to code in front of me to find out exactly. but from memory, it seems to increase by about 100MB and i think this is related to the size of variables being written out (q). (i make a quick calculation and it's about the same order of magnitude)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|