C语言 malloc 可以分配的最大内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2798330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
maximum memory which malloc can allocate
提问by Vikas
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).
我试图弄清楚我可以在我的机器上最大程度地分配多少内存(1 Gb RAM 160 Gb HD Windows 平台)。
I read that the maximum memory malloc can allocate is limited to physical memory (on heap).
我读到 malloc 可以分配的最大内存仅限于物理内存(在堆上)。
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:
所以为了确认,我用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?
- malloc 是否也从 HD 分配内存?
- 上述行为的原因是什么?
- 为什么循环没有在任何时间点中断?
- 为什么没有任何分配失败?
采纳答案by Alex Martelli
I read that the maximum memory
malloccan allocate is limited to physical memory (on heap).
我读到
malloc可以分配的最大内存仅限于物理内存(在堆上)。
Wrong: most computers/OSs support virtualmemory, backed by disk space.
错误:大多数计算机/操作系统都支持虚拟内存,由磁盘空间支持。
Some questions: does
mallocallocate memory from HDD also?
一些问题:是否
malloc也从 HDD 分配内存?
mallocasks the OS, which in turn may well use some disk space.
malloc询问操作系统,这反过来可能会使用一些磁盘空间。
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.
您一次只要求太少:循环最终会中断(在您的机器因虚拟内存与物理内存大量过剩以及随之而来的超频繁磁盘访问而减速到爬行之后,这个问题被称为“颠簸") 但在那之前它已经耗尽了你的耐心。尝试一次获取例如 1 兆字节。
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 feelas if the computer had basically stopped, and what would normally take a minute could take a week.
完全停止是不太可能的,但是当一个通常需要几微秒的操作最终需要(例如)几十毫秒时,这四个数量级肯定会让人感觉好像计算机已经基本停止了,而通常需要什么一分钟可能需要一周时间。
回答by Sebastian
I know this thread is old, but for anyone willing to give it a try oneself, use this code snipped
我知道这个线程很旧,但是对于任何愿意自己尝试的人,请使用此代码剪断
#include <stdlib.h>
int main() {
int *p;
while(1) {
int inc=1024*1024*sizeof(char);
p=(int*) calloc(1,inc);
if(!p) break;
}
}
run
跑
$ gcc memtest.c
$ ./a.out
upon running, this code fills up ones RAM until killed by the kernel. Using calloc instead of malloc to prevent "lazy evaluation". Ideas taken from this thread: Malloc Memory Questions
运行时,此代码会填满 RAM,直到被内核杀死。使用 calloc 而不是 malloc 来防止“懒惰评估”。来自该线程的想法: Malloc Memory Questions
This code quickly filled my RAM (4Gb) and then in about 2 minutes my 20Gb swap partition before it died. 64bit Linux of course.
这段代码很快填满了我的 RAM (4Gb),然后在大约 2 分钟内填满了我的 20Gb 交换分区,然后它就死了。当然是 64 位 Linux。
回答by human.js
Try this
尝试这个
#include <stdlib.h>
#include <stdio.h>
main() {
int Mb = 0;
while (malloc(1<<20)) ++Mb;
printf("Allocated %d Mb total\n", Mb);
}
Include stdlib and stdio for it.
This extract is taken from deep c secrets.
为它包含 stdlib 和 stdio。
此摘录取自deep c secrets。
回答by mdma
mallocdoes its own memory management, managing small memory blocks itself, but ultimately it uses the Win32 Heap functionsto allocate memory. You can think of mallocas a "memory reseller".
malloc它自己进行内存管理,自己管理小内存块,但最终它使用 Win32堆函数来分配内存。您可以将其malloc视为“内存经销商”。
The windows memory subsystem comprises physical memory (RAM) and virtual memory (HD). When physical memory becomes scarce, some of the pages can be copied from physical memory to virtual memory on the hard drive. Windows does this transparently.
windows内存子系统包括物理内存(RAM)和虚拟内存(HD)。当物理内存变得稀缺时,可以将某些页面从物理内存复制到硬盘驱动器上的虚拟内存。Windows 透明地执行此操作。
By default, Virtual Memory is enabled and will consume the available space on the HD. So, your test will continue running until it has either allocated the full amount of virtual memory for the process (2GB on 32-bit windows) or filled the hard disk.
默认情况下,虚拟内存已启用,并将消耗 HD 上的可用空间。因此,您的测试将继续运行,直到它为进程分配了全部虚拟内存(在 32 位 Windows 上为 2GB)或填满了硬盘。
回答by Chris Cooper
I don't actually know why that failed, but one thing to note is that `malloc(4)" may not actually give you 4 bytes, so this technique is not really an accurate way to find your maximum heap size.
我实际上不知道为什么会失败,但有一点需要注意的是,`malloc(4)" 实际上可能不会给你 4 个字节,所以这种技术并不是找到最大堆大小的准确方法。
I found this out from my question here.
For instance, when you declare 4 bytes of memory, the space directly before your memory could contain the integer 4, as an indication to the kernel of how much memory you asked for.
例如,当您声明 4 个字节的内存时,您的内存之前的空间可以包含整数 4,作为向内核指示您请求多少内存。
回答by mav_2k
As per C90 standard guarantees that you can get at least one object 32 kBytes in size, and this may be static, dynamic, or automatic memory. C99 guarantees at least 64 kBytes. For any higher limit, refer your compiler's documentation.
根据 C90 标准保证您可以获得至少一个 32 KB 大小的对象,这可能是静态、动态或自动内存。C99 保证至少 64 KB。对于任何更高的限制,请参阅编译器的文档。
Also, malloc's argument is a size_t and the range of that type is [0,SIZE_MAX], so the maximum you can requestis SIZE_MAX, which value varies upon implementation and is defined in <limits.h>.
此外,malloc 的参数是 size_t 并且该类型的范围是 [0,SIZE_MAX],因此您可以请求的最大值是 SIZE_MAX,该值因实现而异,并在<limits.h>.
回答by Swapnil
when first time you allocate any size to *p, every next time you leave that memory to be unreferenced. That means
当您第一次为 *p 分配任何大小时,下次您将该内存保留为未引用时。这意味着
at a time your program is allocating memory of 4 bytes only
一次你的程序只分配 4 个字节的内存
. then how can you thing you have used entire RAM, that's why SWAP device( temporary space on HDD)is out of discussion. I know an memory management algorithm in which when no one program is referencing to memory block, that block is eligible to allocate for programs memory request. That's why you are just keeping busy to RAM Driverand that's why it can't give chance to service other programs. Also this a dangling reference problem.
. 那么你怎么能使用整个RAM,这就是为什么SWAP设备(硬盘上的临时空间)不在讨论范围内。我知道一种内存管理算法,其中当没有程序引用内存块时,该块有资格为程序内存请求分配。这就是为什么你只是忙于RAM 驱动程序,这就是为什么它不能给其他程序提供服务的机会。这也是一个悬而未决的参考问题。
Ans : You can at most allocate the memory of your RAM size. Because no program has access to swap device.
Ans : 您最多可以分配您的 RAM 大小的内存。因为没有程序可以访问交换设备。
I hope your all questions has got satisfactory answers.
我希望你所有的问题都能得到满意的答复。

