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

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

问题描述

我是想弄清楚我是多么的内存可以到的malloc最大程度上我的机器
(1 GB RAM 160 GB HD Windows平台)。

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

此外,当一个程序超过了内存消耗到一定程度时,电脑停止工作,因为其他应用程序没有得到足够的内存,它们需要。

所以要确认,我用C写了一个小程序:

  INT的main(){
    INT * P;
    而(1){
        p值=(INT *)malloc的(4);
        如果(对!)打破;
    }
}

我希望会有一个时候,内存分配会失败,并且循环将打破,但我的电脑挂,因为它是一个无限循环。

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

一些问题:


  • 的malloc是否从HD分配内存也?

  • 什么是上述行为的原因是什么?

  • 为什么在任何时间点没有回路断线?

  • 为什么没有任何分配失败?


解决方案

  

我读到的最大内存的malloc 可分配限制为物理内存(堆)。


错误:大多数电脑/ OSS支撑的虚拟的内存,通过磁盘空间支持


  

一些问题:是否的malloc 从硬盘分配内存也


的malloc 问OS,这反过来又可能使用一些磁盘空间。


  

什么是上述行为的原因是什么?为什么没有在任何时间回路断线?


  
  

为什么没有任何分配失败?


您刚才问太少一次:循环将最终被打破(后以及你的机器速度极其缓慢,由于大量过剩的虚拟VS物理内存以及随之而来的超级频繁的磁盘访问,一个问题被称为为颠簸),但在此之前用尽你的耐心很好。尝试获得如在同一时间,而不是一兆字节。

当一个程序超过了内存消耗到一定程度时,电脑停止工作,因为其他应用程序没有得到足够的内存,它们需要。

一个总停的可能性不大,但时通常需要几微秒的操作最终以毫秒(EG)数万,级这四个订单当然可以使它的感觉的好像计算机已基本停止,什么通常需要一分钟可能需要一个星期。

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).

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.

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.

Some questions:

  • Does malloc allocate memory from HD also?
  • What was the reason for above behaviour?
  • Why didn't loop break at any point of time?
  • Why wasn't there any allocation failure?

解决方案

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.

Some questions: does malloc allocate memory from HDD also?

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?

Why wasn't there any allocation failure?

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天全站免登陆