none
Max number of file handles per vc++ application RRS feed

  • Question

  • Hi ,

    Remarks: Application is 32 bit application. Run on Windows 7 64 bit machine.

    Is there any limit on number of files(handle)  that can be open by a process at a time?

    Is there any limit on number of files(handle)  that can be open by a system  at a time?

    Also Is there any limit on number of all handles( GDI , file etc)  that can open by a process at a time?

    (File is opened using  ifstream )



    • Edited by Uralsib1 Tuesday, June 2, 2020 1:32 PM
    Tuesday, June 2, 2020 1:31 PM

All replies

  • The number of file handles depends on what interfaces you're using. If using the 'C' run-time functions, see https://docs.microsoft.com/en-us/cpp/c-runtime-library/file-handling?view=vs-2019

    If you're using the Windows API CreateFile, I don't think there's any arbitrary limit.

    GDI handles - see https://docs.microsoft.com/en-us/windows/win32/sysinfo/gdi-objects

    Tuesday, June 2, 2020 1:41 PM
  • There is no limit for handles created with CreateFile

    (you can check by doing an infinite loop)

    Tuesday, June 2, 2020 2:24 PM
  • There is a documented maximum limit, a process can open 2^24 (16,777,216) kernel handles at a time with no limit on the system. But this is very different from the effective limit. Each object uses memory, this means that the actual factor is memory.

    Since kernel, user and GDI handles are stored in different places then theoretically they don't affect each other. But again memory is the determining factor.

    *Edit*

    I was also doing a bit of a test and there is also a major practicality issue here too. I had the test application running for around an hour and it was struggling to get to 2 million file handles opened. With that many handles opened my system's memory usage had also increased by around 2GiB too.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Tuesday, June 2, 2020 2:28 PM
  • Does ofstream internally calls createfile api. Or fopen
    Tuesday, June 2, 2020 4:14 PM
  • Does ofstream internally calls createfile api. Or fopen
    All APIs call NtCreateFile
    Tuesday, June 2, 2020 4:58 PM
  • It is a chain.

    The std::basic_ifstream and std::basic_ofstream calls open on std::basic_filebuf. Then std::basic_filebuf calls fopen.

    The CRT implementation then has fopen calling _open and _open calls CreateFile.

    So yes, all C++ iostream and fstream file operations use the Windows kernel handles internally.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Tuesday, June 2, 2020 7:33 PM
  • Hi,

    In my local system (windows 10 64 bit os), file creation fails after creating 509 text files. 

    Application is 32 bit application( dialog based mfc application). Code is shown below.

    No other files are creating from this application( from other functions) 

    The error message is " Too many opened files"

    Any idea why file creation fails after 509 files 

    std::string     getMessage()
    {
       const  auto   BUFFER_LENGTH = size_t(256);
       auto  buffer = std::vector<char>(BUFFER_LENGTH, '\0');
       strerror_s(&buffer.front(), BUFFER_LENGTH, errno);
       return  std::string(&buffer.front());
    }
     
    void  CMFCApplication2Dlg::OnBnClickedOk()
    {
      
      std::ofstream    myvect[1000];
       for(int  i=1; i < 1000; i++){
     
          std::stringstream    filename;
          filename << "D:\\"<< i << ".txt";
          myvect[i].open(filename.str().c_str(), std::ofstream::out | std::ofstream::app);
          myvect[i] << "dummy data";
          myvect[i].flush();
     
          if(myvect[i].fail())
          {
             auto  errorMessage = getMessage();
          }
       }
     
    }







    • Edited by Uralsib1 Wednesday, June 3, 2020 4:18 AM
    Wednesday, June 3, 2020 4:13 AM
  • Well, I suggest you look at _setmaxstdio.

    Basically, this is a limit imposed by a higher level subsystem. Just in case you are wondering, 509 + stdin + stdout + stderr = 512.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Edited by Darran Rowe Wednesday, June 3, 2020 8:19 AM
    Wednesday, June 3, 2020 7:05 AM
  • Hi,

    Thank you for posting here.

    >>The error message is " Too many opened files"

    CreatFile/OpenFile api is not meant only for files (Files,Directories,Communication Ports,pipes,mail slots,Drive volumes etc.,). So in a real application depending on the number these resources your max open file may vary. 

    Perhaps Windows has a limitation of 512 descriptors per process (minus 2 for stdin & stdout). And perhaps using threads consumes some descriptors as well. 

    As far as I'm concerned, it may be a limitation of your operating system. It can depend on many things: the way the file descriptors are represented, the memory they consume, and so on.

    Best Regards,

    Jeanine Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 3, 2020 7:36 AM
    Moderator
  • Just to make it absolutely clear following is the documented open file limit guidance contained in the link that was posted by David Lowndes in the first response to this thread --

    "The C run-time libraries have a 512 limit for the number of files that can be open at any one time. Attempting to open more than the maximum number of file descriptors or file streams causes program failure. Use _setmaxstdio to change this number."

    Wednesday, June 3, 2020 9:53 AM