用新的分配大块内存 [英] Allocating large blocks of memory with new

查看:163
本文介绍了用新的分配大块内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用新的分配大块的内存。



我使用新的,因为我写一个模拟生产者一侧的二零件应用。实际的生产者代码是分配这些大块,我的代码有责任删除它们(处理后)。



有一种方法,我可以确保我的应用程序能够从堆中分配这么大量的内存?我可以将堆设置为更大的大小吗?



我的case是64块的288000字节。有时我得到12分配,其他时间我得到27分配。我得到一个std :: bad_alloc异常。



这是:C ++,Linux上的GCC(32位)。

解决方案

关于C ++ / GCC / Linux(32位)中的 ...



它已经有一段时间了,它的实现依赖,但我相信将幕后,调用 malloc()。除非您要求超出进程的地址空间或超出指定( ulimit / getrusage )限制的内容,否则Malloc() 不会失败。即使您的系统没有足够的RAM + SWAP。例如:在具有256Meg RAM + 0 SWAP的系统上, malloc(1gig)相信会成功。



去使用那个内存,内核通过一个延迟分配机制提供页面。在这一点上,当你第一次读取或写入内存时,如果内核不能为你的进程分配内存页面,它会杀死你的进程。



这可能是一个问题在共享的计算机上,当你的同事有一个缓慢的核心泄漏。特别是当他开始淘汰系统进程时。



所以,你看到std :: bad_alloc异常的事实是有趣。



现在, 将在分配的内存上运行构造函数,在返回之前触摸所有这些内存页。根据实现,它可能可能会捕获内存不足的信号。



malloc ?



您是否已尝试执行免费计划?



正如其他人建议的,您已经检查了 limit / ulimit / getrusage()软约束?



你的代码是什么样子的?我猜新的ClassFoo [N] 。 什么是 N



对于大多数现代机器来说,分配64 * 288000(17.58Meg)系统或其他特殊的东西?



或者,您是否使用自定义的分配器链接?您的类是否有自己的分配器?



您的数据结构(类)是否将其他对象分配为其构造函数的一部分? >

有人篡改你的图书馆吗?您是否安装了多个编译器?您是使用错误的包含或库路径吗?



您是否链接到过期的对象文件?你只需要重新编译你的所有源文件?



你能创建一个简单的测试程序吗?只是几行代码再现错误?



对于有价值的东西,在g ++下的32位linux中用 new 分配了2gig数据块。你的问题在别处。


I have the need to allocate large blocks of memory with new.

I am stuck with using new because I am writing a mock for the producer side of a two part application. The actual producer code is allocating these large blocks and my code has responsibility to delete them (after processing them).

Is there a way I can ensure my application is capable of allocating such a large amount of memory from the heap? Can I set the heap to a larger size?

My case is 64 blocks of 288000 bytes. Sometimes I am getting 12 to allocate and other times I am getting 27 to allocate. I am getting a std::bad_alloc exception.

This is: C++, GCC on Linux (32bit).

解决方案

With respect to new in C++/GCC/Linux(32bit)...

It's been a while, and it's implementation dependent, but I believe new will, behind the scenes, invoke malloc(). Malloc(), unless you ask for something exceeding the address space of the process, or outside of specified (ulimit/getrusage) limits, won't fail. Even when your system doesn't have enough RAM+SWAP. For example: malloc(1gig) on a system with 256Meg of RAM + 0 SWAP will, I believe, succeed.

However, when you go use that memory, the kernel supplies the pages through a lazy-allocation mechanism. At that point, when you first read or write to that memory, if the kernel cannot allocate memory pages to your process, it kills your process.

This can be a problem on a shared computer, when your colleague has a slow core leak. Especially when he starts knocking out system processes.

So the fact that you are seeing std::bad_alloc exceptions is "interesting".

Now new will run the constructor on the allocated memory, touching all those memory pages before it returns. Depending on implementation, it might be trapping the out-of-memory signal.

Have you tried this with plain o'l malloc?

Have you tried running the "free" program? Do you have enough memory available?

As others have suggested, have you checked limit/ulimit/getrusage() for hard & soft constraints?

What does your code look like, exactly? I'm guessing new ClassFoo [ N ]. Or perhaps new char [ N ].

What is sizeof(ClassFoo)? What is N?

Allocating 64*288000 (17.58Meg) should be trivial for most modern machines... Are you running on an embedded system or something otherwise special?

Alternatively, are you linking with a custom new allocator? Does your class have its own new allocator?

Does your data structure (class) allocate other objects as part of its constructor?

Has someone tampered with your libraries? Do you have multiple compilers installed? Are you using the wrong include or library paths?

Are you linking against stale object files? Do you simply need to recompile your all your source files?

Can you create a trivial test program? Just a couple lines of code that reproduces the bug? Or is your problem elsewhere, and only showing up here?

--

For what it's worth, I've allocated over 2gig data blocks with new in 32bit linux under g++. Your problem lies elsewhere.

这篇关于用新的分配大块内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆