64位大mallocs [英] 64 bit large mallocs

查看:450
本文介绍了64位大mallocs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么原因一个malloc()会失败,特别是在64位?

What are the reasons a malloc() would fail, especially in 64 bit?

我的具体问题是试图对malloc的RAM一个巨大的10GB块在64位系统。 该机拥有12GB的RAM和32 GB交换的。 是的,malloc的是极端的,但为什么会是一个问题吗?这是Windows XP64与英特尔和MSFT编译器。 malloc的有时成功,有时没有,约50%。 8GB mallocs总是工作,20GB mallocs总是失败。如果一个malloc失败,多次请求将无法工作,除非我退出进程,并重新开始一个新的进程(届时将有50%的射门成功)。没有其他大的应用程序正在运行。它一个新的重新启动后发生的,甚至马上。

My specific problem is trying to malloc a huge 10GB chunk of RAM on a 64 bit system. The machine has 12GB of RAM, and 32 GB of swap. Yes, the malloc is extreme, but why would it be a problem? This is in Windows XP64 with both Intel and MSFT compilers. The malloc sometimes succeeds, sometimes doesn't, about 50%. 8GB mallocs always work, 20GB mallocs always fail. If a malloc fails, repeated requests won't work, unless I quit the process and start a fresh process again (which will then have the 50% shot at success). No other big apps are running. It happens even immediately after a fresh reboot.

我能想象一个malloc未能在32位,如果你已经使用了32(​​或31)位的地址空间可用,比如,有没有地址范围足够大,分配给您的要求。

I could imagine a malloc failing in 32 bit if you have used up the 32 (or 31) bits of address space available, such that there's no address range large enough to assign to your request.

我也能想象的malloc失败,如果你已经使用了你的物理内存你的硬盘交换空间。这是不是这样的我。

I could also imagine malloc failing if you have used up your physical RAM and your hard drive swap space. This isn't the case for me.

但是,为什么其他人可以一个malloc失败?我想不出别的原因。

But why else could a malloc fail? I can't think of other reasons.

我更感兴趣的是一般的malloc问题比我的具体的例子,我很可能会取代内存映射文件反正。在失败的malloc()仅仅是多一个谜比什么都重要......那渴望了解你的工具,而不是由基本面​​感到惊讶。

I'm more interested in the general malloc question than my specific example, which I'll likely replace with memory mapped files anyway. The failed malloc() is just more of a puzzle than anything else... that desire to understand your tools and not be surprised by the fundamentals.

推荐答案

的malloc尝试分配一个连续的内存范围,这将首先在实际内存只是由于如何交换内存的工作(至少据我记得) 。它很容易被您的操作系统有时找不到内存10GB的邻接块,仍然留下所有需要的RAM实际内存在同一时间(此时你的malloc会失败)的过程。

malloc tries to allocate a contiguous memory range, and this will initially be in real memory simply due to how swap memory works (at least as far as I remember). It could easily be that your OS sometimes can't find a contiguous block of 10gb of memory and still leave all the processes that require real memory in RAM at the same time (at which point your malloc will fail).

你真的需要连续内存10GB,否则你将能够环绕几个更小的块存储类/结构和块使用你的记忆呢?这放宽了巨大的连续的要求,也应该让你的程序使用swap文件少用大块。

Do you actually require 10gb of contiguous memory, or would you be able to wrap a storage class/struct around several smaller blocks and use your memory in chunks instead? This relaxes the huge contiguous requirement and should also allow your program to use the swap file for less used chunks.

这篇关于64位大mallocs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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