简体   繁体   中英

Memory Mapped FIle is slow

I am trying to read from a memory mapped file, but access to the file is taking a long time. I am mapping the whole file to my program, and initial access to fast, but then it begins to drastically slow down

The file is ~47gb and I have 16gb of RAM. I am running a 64-bit application on windows 7 using Visual Studios as my IDE. Below is snippet of my code

    hFile = CreateFile( "Valid Path to file",                // name of the write
                        GENERIC_READ , // open for reading
                        0,                      // do not share
                        NULL,                   // default security
                        OPEN_EXISTING,             // existing file only
                        FILE_ATTRIBUTE_NORMAL,  // normal file
                        NULL);                  // no attr. template

    if (hFile == INVALID_HANDLE_VALUE)
    {
        cout << "Unable to open vals" << endl;
        exit(1);
    }

    hMapFile = CreateFileMapping(
                                hFile,              
                                NULL,                        // default security
                                PAGE_READONLY,      // read/write access
                                0,                           // maximum object size (high-order DWORD)
                                0,                           // maximum object size (low-order DWORD)
                                NULL);                       // name of mapping object

    if (hMapFile == NULL)
    {
        cout<< "Error code " << GetLastError() << endl;
        exit(1);
    }



     data = (float*) MapViewOfFile(
                            hMapFile,
                            FILE_MAP_READ,
                            0,
                            0,
                            0);

    if (data == NULL)
    {
        cout << "Error code " << GetLastError() << endl;

        CloseHandle(hFile);

        exit(1);
    }

Is this just because the file is so large that continually swapping chunks of the file takes long, or is it some other parameter I need for faster access?

EDIT: I tried using read only instead of using read, write, execute as seen above, but the speed is still slow. I understand the concepts of memory mapping and switch swap space, but I thought I may have been doing something else wrong with was hindering the speed

This is because of paging. What is happening is that your RAM can only hold 16GB of the file (in reality it is less because of other programs running on your computer, but let's just use this for simplicity).

So if you access a part of the file in your program that is not in RAM (let's say, a part of the file that is in the 20GB segment) your RAM needs to talk to the disk and transfer a whole new segment of the file to RAM. This takes a lot of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM