如何分配大型连续内存? [英] How to allocate a large continuous memory ?

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

问题描述



我必须使用new char [1024 * 1024 * 200]分配一个大的连续内存,例如200M。可用内存足够,但可以分为很小的补丁。这导致调用new throws异常。我可以采用一种解决方案吗?

感谢您的关注。

Hi,
I have to allocate a large continuous memory, for instance, 200M, using "new char[1024*1024*200]". The available memory is enough, but may be divided into tiny patches. That results the invoke of new throws a exception. Is there a solution can I adopt ?
Thanks for your attention.

推荐答案

第一个问题是你真的必须要有这样的连续性吗?大阵。为什么不使用100 MB的1 MB数组呢?



200M是一个用于计算机上32位应用程序的大数组,没有太多RAM等。



据我所知,大量的分配被转发到系统,因此在编译器和操作系统上并没有多少分配。分配内存并重复分配内存时会发生碎片。



如果您使用的是STL,则可以使用deque<>如果您只希望内存在代码中连续显示,但数据本身不是连续的。否则如解决方案1中所提到的,内存映射文件可能是另一种解决方案。



顺便说一下,你的问题没有给我们足够的细节来给出最好的答案你的情况。
The first question is do you really have to have such continuous big array. Why not using 200 array of 1 MB for example?

200M is a big array for 32 bit application on computer that does not have much RAM etc.

As far as I know large allocation are forwarded to the system and thus does not dépends much on the compiler but the OS. Fragmentation occurs when memory is allocated and unallocated repetively.

If you are using STL, you can use deque<> if you only want the memory to appears consecutive in code but does not the data itself be consecutive. Otherwise as mentionned in Solution 1, memory mapped file might be another solution.

By the way, your question does not give us enough details to give the best possible answer in your case.


你可以尝试的东西,如果你确定某些东西正在分裂堆(即你分配了大量的对象而只删除了其中的一半):



- 检查操作系统的某些内容是不是在破坏分配(例如32位地址空间不够大; Windows的习惯是及时扩展交换文件和分配超时)并尽可能修复



- 不动态分配你的数组;使数据项成为静态或全局



- 在 main()



- 尝试另一个编译器/操作系统组合的代码(不同的地址空间布局可能会得到回报或者给你一个提示,说明为什么它没有在目标)



- 在Windows上使用类似 VirtualAlloc 的内容



- 在Windows上创建并使用由交换文件支持的内存映射文件



如果该批用完了又回来再问一遍!
Things you can try if you're sure that something's fragmenting the heap (i.e. you're allocating loads of objects and only deleting half of them):

- check that something about the OS isn't knackering the allocation (e.g. 32 bit address space just isn't big enough; Windows habit of just in time expanding the swap file and the allocation timing out) and fix that if possible

- don't dynamically allocate your array; make the data item a static or global

- do large allocations like this before you do anything else in main()

- try the code on another compiler/OS combination (a different address space layout might pay off or give you a hint as to why it's not working on the target)

- use something like VirtualAlloc on Windows

- create and use a memory mapped file backed by the swap file on windows

If that lot run out come back and ask again!


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

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