malloc 可以分配的最大内存 [英] Maximum memory which malloc can allocate

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

问题描述

我试图弄清楚我可以在我的机器上最大程度地分配多少内存(1 Gb RAM 160 Gb HD Windows 平台).

I was trying to figure out how much memory I can malloc to maximum extent on my machine (1 Gb RAM 160 Gb HD Windows platform).

我读到 malloc 可以分配的最大内存仅限于物理内存(在堆上).

I read that the maximum memory malloc can allocate is limited to physical memory (on heap).

此外,当程序消耗的内存超过一定水平时,计算机会停止工作,因为其他应用程序没有获得它们所需的足够内存.

Also when a program exceeds consumption of memory to a certain level, the computer stops working because other applications do not get enough memory that they require.

为了确认,我用C写了一个小程序:

So to confirm, I wrote a small program in C:

int main(){  
    int *p;
    while(1){
        p=(int *)malloc(4);
        if(!p)break;
    }   
}

我希望有一天内存分配会失败,循环会中断,但我的电脑因为无限循环而挂了.

I was hoping that there would be a time when memory allocation would fail and the loop would break, but my computer hung as it was an infinite loop.

我等了大约一个小时,最后我不得不强行关闭我的电脑.

I waited for about an hour and finally I had to force shut down my computer.

一些问题:

  • malloc 是否也从 HD 分配内存?
  • 上述行为的原因是什么?
  • 为什么循环没有在任何时候中断?
  • 为什么没有分配失败?

推荐答案

我读到 malloc 可以分配的最大内存仅限于物理内存(在堆上).

I read that the maximum memory malloc can allocate is limited to physical memory (on heap).

错误:大多数计算机/操作系统支持虚拟内存,由磁盘空间支持.

Wrong: most computers/OSs support virtual memory, backed by disk space.

一些问题:malloc 是否也从 HDD 分配内存?

Some questions: does malloc allocate memory from HDD also?

malloc 询问操作系统,操作系统可能会使用一些磁盘空间.

malloc asks the OS, which in turn may well use some disk space.

上述行为的原因是什么?为什么循环没有随时中断?

What was the reason for above behavior? Why didn't the loop break at any time?

为什么没有分配失败?

您只是一次要求太少:循环最终会中断(在您的机器因虚拟内存与物理内存大量过剩以及随之而来的超频磁盘访问而缓慢爬行之后,这是一个已知问题作为颠簸"),但在那之前它已经耗尽了你的耐心.尝试获取例如改为一次 1 兆字节.

You just asked for too little at a time: the loop would have broken eventually (well after your machine slowed to a crawl due to the large excess of virtual vs physical memory and the consequent super-frequent disk access, an issue known as "thrashing") but it exhausted your patience well before then. Try getting e.g. a megabyte at a time instead.

当程序的内存消耗超过一定程度时,计算机停止工作,因为其他应用程序没有得到足够的他们需要的内存.

When a program exceeds consumption of memory to a certain level, the computer stops working because other applications do not get enough memory that they require.

完全停止是不太可能的,但是当一个通常需要几微秒的操作最终需要(例如)几十毫秒时,这四个数量级肯定会让它感觉好像电脑基本停止了,通常需要一分钟的事情可能需要一周时间.

A total stop is unlikely, but when an operation that normally would take a few microseconds ends up taking (e.g.) tens of milliseconds, those four orders of magnitude may certainly make it feel as if the computer had basically stopped, and what would normally take a minute could take a week.

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

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