hihi,

when I open a file using ofstream or fopen, the memory cannot be released after I call close() function. how can I release the memory?

P.S. the memory can be released after remove the files.

the program code:

int main (int argc, char **argv){

  ofstream file1;

  while (true){
     
     /**
      * intToStr() uses to converts interger to string 
      */
     filename1 = "/mnt/data/"+intToStr(count)+".txt";
     
     file1.open(filename1.c_str());
     
     for (int i = 0; i < 3000; i++){
         file1 << "testing string";
         file1.flush();
     }

     file1.close();    
     count++;

  }


}

when you use fopen you assign a file pointer for a file.

you can use free(FilePointer); , after using fclose(FilePointer);

but as far as i know fclose is reseting pointing address of pointer, so you don't have to use...

commented: This is the C++ forum. -1

I cannot release the memory unless I remove the files. how can I solve this problem? thanks

oopg,
Did you allocate memory elsewhere in the program? Post complete code if you have. I didn't find any memory allocation statement in your program.

Wrong post

Here is the complete program:

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

string intToStr(const int intValue){
    string str;
    stringstream strStream;
    strStream << intValue;

    return strStream.str();
}

int main(int argc, char **argv){

    string filename1;
    string filename2;
    string filename3;
    string filename4;

    int count = 0;

    while (true){

        filename1 = "data/"+intToStr(count)+"_1.txt";
        filename2 = "data/"+intToStr(count)+"_2.txt";
        filename3 = "data/"+intToStr(count)+"_3.txt";
        filename4 = "data/"+intToStr(count)+"_4.txt";

        FILE *file1;
        FILE *file2;
        FILE *file3;
        FILE *file4;

        file1 = fopen(filename1.c_str(),"w");
        file2 = fopen(filename2.c_str(),"w");
        file3 = fopen(filename3.c_str(),"w");
        file4 = fopen(filename4.c_str(),"w");

        for (int i = 0; i < 3000; i++){
              fprintf(file1,"test");
              fprintf(file2,"test");
              fprintf(file3,"test");
              fprintf(file4,"test");
        }

         fclose(file1);
         fclose(file2);
         fclose(file3);
         fclose(file4);

         count++;

    }
}

You are not allocating any memory that needs to be freed. You are using FILE pointers, so you need to call fclose() on each one, but that's all. Can you be more specific about what the problem is?

the problem is the linux "pageCache" cannot be free. how can I free the pageCache? thank you.

Why do you want to release something that belongs to the operating system? Programs don't normally have any control over paging system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.