0

我有一个适用于 Windows Mobile 6 ARMV4I 的 Visual Studio 2008 C++ 项目,我正在使用内存映射文件。不幸的是,它会导致设备锁定。我可以用这段代码演示这个问题:

#include <list>
#include <algorithm>

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD alloc_size = 256;
    DWORD alloc_max = 16 * 1024 * 1024;
    DWORD alloc_count = alloc_max / alloc_size;

    HANDLE f = ::CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, alloc_max, NULL );

    std::list< void* > l;
    for( DWORD i = 0; i < alloc_count; ++i )
    {
        // device freezes after 65529 iterations
        l.push_back( ::MapViewOfFile( f, FILE_MAP_READ | FILE_MAP_WRITE, 0, i * alloc_size, alloc_size ) );
    }

    std::for_each( l.rbegin(), l.rend(), ::UnmapViewOfFile );
    ::CloseHandle( f );
    return 0;
}

在我的测试中,Windows Mobile 6 Classic Emulator 将在 65529 次迭代后冻结。这是我做错了什么还是我应该注意的平台问题?

谢谢,保罗

编辑:增加到 /STACK:1048576,4096 允许我在设备冻结之前达到 65535 次迭代。

Edit2:根据GlobalMemoryStatus故障前的数据,该设备有 70.5MB / 94.1MB 可用物理内存。

Edit3:我可以创建两个 MMF 并将它们都加载到 65500 * 256 字节。但是,它们都不能单独超过 65535 个分配。实际上,分配大小无关紧要。我可以将它切成两半,每个 128 字节,但我仍然在 >65535 次迭代中失败。

Edit4:用实际文件支持 MMF 似乎没有什么区别。> 65535 次迭代失败。

4

3 回答 3

0

According to this (see Figure 4) there is only 256MB of address space allocated for use with memory mapped files. 64K allocations * 4KB = 256MB, so you are hitting the limit.

于 2011-05-12T16:58:33.953 回答
0

在 Windows 中,内存是在pages中管理的。此外,在分配这些页面时有一个最小粒度。在桌面 Windows 上,一个页面通常为 4KiB,最小粒度通常为 64KiB。如果您尝试使用VirtualAllocMapViewOfFile使用小于该大小的大小,它将被四舍五入,并且您将浪费一些 RAM。

我很确定 Windows Mobile 上的页面大小也将是 4KiB - 因此对于每个 256 字节MapViewOfFile,它实际上必须保留至少4KiB。您可以致电GetSystemInfo自己获取这些号码。

这意味着您的代码实际上至少保留了256MiB,如果分配粒度更高,则可能更多。您的应用程序正在耗尽其地址空间。

于 2011-05-12T16:19:23.313 回答
0

I spoke with somebody that has access to the sources. As it turns out, MapViewOfFile uses an internal reference counter that is a USHORT. So, on the 65535th iteration, it overflowed and caused hate and discontent all over the place eventually halting the system. So, there is an undocumented limit of 65535 open views in to a Memory Mapped File.

-PaulH

于 2011-05-13T17:47:02.477 回答