C++大量记忆分配

发布于 2025-01-25 18:06:49 字数 1283 浏览 5 评论 0原文

我在C ++中编写了测试代码:

void handler()
{
    std::cout << "allocation failed" << std::endl;
    std::set_new_handler(nullptr);
}

int main()
{
    size_t allocations_count = 0u;
    std::set_new_handler(handler);

    try {
    
        while (true) {
        new char[1024u * 1024u * 1024u];
           ++allocations_count;
        }
    } catch (const std::bad_alloc& e) {
        std::cout << e.what() << '\n';
    }

    std::cout << "allocated " << allocations_count << " GB" << std::endl;
}

在带有8GB RAM的计算机中,我得到了输出:

BigAllocations(5125,0x1050e7d40) malloc: can't allocate region
:*** mach_vm_map(size=1073741824, flags: 100) failed (error code=3)
BigAllocations(5125,0x1050e7d40) malloc: *** set a breakpoint in malloc_error_break to debug
allocation failed
BigAllocations(5125,0x1050e7d40) malloc: can't allocate region
:*** mach_vm_map(size=1073741824, flags: 100) failed (error code=3)
BigAllocations(5125,0x1050e7d40) malloc: *** set a breakpoint in malloc_error_break to debug
std::bad_alloc
allocated 130676 GB

我分配了130676GB,我认为这太多了,因为我的存储尺寸为500GB,我认为,虚拟内存可以服务于此数量的此数字。记忆...谁能解释为什么我可以在那里进行大量分配?

ram = 8GB 存储= 500GB MacBook Pro(13英寸,M1,2020)

I wrote test code in C++:

void handler()
{
    std::cout << "allocation failed" << std::endl;
    std::set_new_handler(nullptr);
}

int main()
{
    size_t allocations_count = 0u;
    std::set_new_handler(handler);

    try {
    
        while (true) {
        new char[1024u * 1024u * 1024u];
           ++allocations_count;
        }
    } catch (const std::bad_alloc& e) {
        std::cout << e.what() << '\n';
    }

    std::cout << "allocated " << allocations_count << " GB" << std::endl;
}

In my machine with 8GB RAM, I got the output:

BigAllocations(5125,0x1050e7d40) malloc: can't allocate region
:*** mach_vm_map(size=1073741824, flags: 100) failed (error code=3)
BigAllocations(5125,0x1050e7d40) malloc: *** set a breakpoint in malloc_error_break to debug
allocation failed
BigAllocations(5125,0x1050e7d40) malloc: can't allocate region
:*** mach_vm_map(size=1073741824, flags: 100) failed (error code=3)
BigAllocations(5125,0x1050e7d40) malloc: *** set a breakpoint in malloc_error_break to debug
std::bad_alloc
allocated 130676 GB

I allocated 130676GB, I think it's too much, because my storage size is 500GB and I don't think, that virtual memory can server this number of memory... Can anyone explain why I can make a lot of allocations there?

RAM = 8GB
Storage = 500GB
MacBook Pro (13-inch, M1, 2020)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2025-02-01 18:06:49

64位可以解决9313225 GB内存。这是您分配的数量级超过130675 GB。

因此,即使使用OS保留的所有特殊地址范围,虚拟内存也可以轻松符合您分配的内存量。只要您不写入它,它将包含零,并且不需要存储任何地方(注意,C ++语言new不保证内存包含零,但是显然允许允许 /em> it)。

将您分配给非零的内存初始化,然后查看会发生什么。在Linux上,这将触发OOM(无内存)杀手,该杀手将开始终止进程​​,当OS耗尽物理内存并磁盘交换以存储数据时。也许OSX做类似的事情。

64 bits can address 9313225 GB of memory. This is an order of magnitude more than 130675 GB you allocated.

So virtual memory can easily fit the amount of memory you allocated, even with all special address ranges reserved by the OS. As long as you do not write to it, it will contain zeroes and does not need to be stored anywhere (note, C++ language new does not guarantee memory contains zeroes, but it obviously allows it).

Initialize the memory you allocate to non-zero and see what happens then. On Linux this would trigger OOM (out-of-memory) killer, which would start terminating processes, when the OS runs out of physical memory and disk swap to store the data. Maybe OSX does something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文