简体   繁体   English

找到最大的内存分配

[英]Find largest allocation of memory possible

In 32bit mode application has access to 2GB of virtual address space. 在32位模式下,应用程序可以访问2GB的虚拟地址空间。 How would you find out the maximum size of memory you can allocate to this virtual address space, without malloc or new failing? 如果没有malloc或新的失败,您如何找到可以分配给此虚拟地址空间的最大内存大小?

For example, lets say you want to take up the whole 2GB of virtual address space, but you just allocated 2MB of data in the middle of the 2GB address space. 例如,假设您想要占用整个2GB的虚拟地址空间,但您只是在2GB地址空间的中间分配了2MB的数据。 Is there a Windows API call that you can find out the max concurrent address space that you can allocate? 是否有Windows API调用,您可以找到可以分配的最大并发地址空间? So that when you call malloc or new the call does not fail? 那么当你调用malloc或new时,调用不会失败?

Thank you for your time. 感谢您的时间。

Source: http://www.voyce.com/index.php/2009/08/21/largest-free-block-of-address-space/ 资料来源: http//www.voyce.com/index.php/2009/08/21/largest-free-block-of-address-space/

DWORD FindLargestSizeOfMemory()
{

    MEMORY_BASIC_INFORMATION mbi;
    DWORD start = 0;
    bool recording = false;
    DWORD freestart = 0, largestFreestart = 0;
    __int64 free = 0, largestFree = 0;

    while (true)
    {
        SIZE_T s = VirtualQuery((LPCVOID)start, &mbi, sizeof(mbi));
        if (s != sizeof(mbi)) break;

    if (mbi.State == MEM_FREE)
        {
            if (!recording) freestart = start;

            free += mbi.RegionSize;
            recording = true;
        }
        else
        {
            if (recording)
            {
                if (free > largestFree)
                {
                    largestFree = free;
                    largestFreestart = freestart;
                }
            }
            free = 0;
            recording = false;
        }
        start += mbi.RegionSize;
    }

  return largestFree;
}

You might never get a satisfactory answer. 你可能永远得不到满意的答案。 The largest allocatable memory block is dependend on memory fragmentation and can be altered with memory defragmentation tools, different memory managers, different boot time switches (eg /3GB on windows) or even a reboot. 最大的可分配内存块依赖于内存碎片,可以通过内存碎片整理工具,不同的内存管理器,不同的启动时间开关(例如Windows上的/ 3GB)甚至重新启动来更改。

I'm not sure what you mean by "without malloc or new failing", do you not have access to malloc? 我不确定你的意思是“没有malloc或新的失败”,你没有访问malloc吗? Because the correct way to do this would be to allocate memory using var=malloc(...) in incrementing steps and looking for the step where var is null. 因为正确的方法是在递增步骤中使用var = malloc(...)分配内存并查找var为null的步骤。

I am pretty sure that the information you seek can only be determined by probing for it. 我很确定您所寻求的信息只能通过探测来确定。 The information is always in fluid motion by allocation and deallocation of running processes, it could only be a snapshot of the current situation, and of course, for taking the snapshot, all processes would have to be suspended temporarily. 通过分配和释放正在运行的进程,信息始终处于流畅的运动状态,它只能是当前情况的快照,当然,对于拍摄快照,所有进程都必须暂时暂停。 So the information you would get after Windows scanning for a result and returning it to you would not be accurate or resilient, as the structure of the memory allocation could very well have changed by now. 因此,在Windows扫描结果并将其返回给您后,您将获得的信息将不准确或有弹性,因为内存分配的结构现在很可能已经改变。

您可以使用VirtualQuery函数查询可能的页面,看看它们是否返回MEM_FREE

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

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