2

The test code as follows and compiled by vs2013:

#include <memory>
#include <list>
#include <stdint.h>

int main()
{
    uint32_t one_size = 32 * 1024;
    uint64_t total_size = 0;
    auto deleter = [](char* p) { delete[] p; };
    using SharedBuffer = std::pair<std::shared_ptr<char>, int>;
    std::list<SharedBuffer> buffers;
    while (total_size < (uint32_t)2 * 1024 * 1024 * 1024)
    {
        std::shared_ptr<char> buffer;
        try
        {
            buffer = std::shared_ptr<char>(new char[one_size], deleter);
            total_size += one_size;
        }
        catch (const std::bad_alloc& e)
        {
            printf("%s\n", e.what());
            break;
        }
        try
        {
            buffers.emplace_back(std::make_pair(buffer, one_size));
        }
        catch (const std::bad_alloc& e)
        {
            printf("%s\n", e.what());
            break;
        }
    }
    return 0;
}

When the process's memory reached to 2GB, it will catch a bad_alloc exception, but 10GB physical memory available of 32GB total.

So, why caused it?

WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
Bruce
  • 61
  • 1
  • 9

1 Answers1

3

32 bit Windows programs will only have 2GB of virtual address space available unless the Large Address Aware flag is set in which case it will have 3GB on a 32 bit host and a full 4GB on a 64 bit host. Even so, you won't be able to allocate a contiguous single block over 3GB.

If you want to extend past that boundary you need to make a 64-bit build of your application.

Olipro
  • 3,489
  • 19
  • 25
  • in the worst case, you might need to also get a 64-bit cpu - but hopefully nobody sold you that much RAM with a 32-bit package...! – aho Nov 10 '15 at 03:33
  • @aho I can't remember the last CPU that couldn't run 64 bit, it was so long ago. But even today you can get a 32-bit OS for your 64-bit processor. – Mark Ransom Nov 10 '15 at 03:39
  • 2
    Even if you enable Large Address Aware, you won't be able to allocate 3GB in a single block. – Raymond Chen Nov 10 '15 at 03:51
  • @RaymondChen - Ah yes of course, I'll clarify to say that's just virtual address space. also, Holy cow it's Raymond Chen. The Old New Thing is a great book! – Olipro Nov 10 '15 at 04:02