简体   繁体   English

内存映射文件很慢

[英]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. 该文件约为47gb,我有16gb的RAM。 I am running a 64-bit application on windows 7 using Visual Studios as my IDE. 我正在Windows 7上使用Visual Studios作为IDE运行64位应用程序。 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). 发生的事情是您的RAM只能容纳16GB的文件(实际上,由于计算机上正在运行其他程序,因此内存减少了,但为简单起见,我们只使用它)。

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. 因此,如果您访问程序中不在RAM中的文件的一部分(例如,文件中20GB段中的一部分),则RAM需要与磁盘进行对话并传输文件的整个新段到RAM。 This takes a lot of time. 这需要很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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