Help -fwrite consuming lot of memory !!!

Hi ,

I am running a C/C++ program on a solaris 5.8 machine. This parituclar application has a module which saves data to a file. The module uses fwrite() function to save data.

The fwrite function write about 500 MB of data to a file. The problem which I am facing is, the memory consumtion of the process increases during the fwrite but does not decrease once the fwrite is over.

I tried fflush(filepointer) after the fwrite but did not help.

The fwrite function writes all the data in once single fwrite() statement. I initially thought the increase in memory was because of this. So I tried writing data in smaller chunks of sizes 100 MB and 50 MB but still the memory utilization of the process does not decreases once the fwrite is over.

As a result a lot of the main memory is being eaten by this process and thus making the system very slow.

Please help me .

Thanks in advance

ajphaj

That's right processes will not shrink when you're using the malloc library even indirectly. Maybe you can fork(). Let the child write the data and then exit.

You can try using the open() system call with O_SYNC flag, then call write() every 1MB of data.

O_SYNC turns off file buffering, in that every call to write waits until the underlying hardware has completed writing the data.

For fwrite(), setvbuf() and setbuf() do somewhat the same thing.

A word of warning: turning off buffering completely is a very bad idea in terms of performance. Especially on writing really big files.