简体   繁体   English

C ++中堆栈,静态和堆内存的最大内存

[英]Maximum memory for stack, static and heap memory in C++

I am trying to find the maximum memory that I could allocate on stack, global and heap memory in C++. 我试图找到我可以在C ++中的堆栈,全局和堆内存上分配的最大内存。 I am trying this program on a Linux system with 32 GB of memory, and on my Mac with 2 GB of RAM. 我正在具有32 GB内存的Linux系统上以及在具有2 GB RAM的Mac上尝试该程序。

/* test to determine the maximum memory that could be allocated for static, heap and stack memory  */

#include <iostream>
using namespace std;

//static/global
long double a[200000000];

int main()
{
//stack
long double b[999999999];

//heap
long double *c = new long double[3999999999];
cout << "Sizeof(long double) = " << sizeof(long double) << " bytes\n";
cout << "Allocated Global (Static) size of a = " << (double)((sizeof(a))/(double)(1024*1024*1024)) << " Gbytes \n";
cout << "Allocated Stack size of b = " << (double)((sizeof(b))/(double)(1024*1024*1024)) << " Gbytes \n";
cout << "Allocated Heap Size of c = " << (double)((3999999999 * sizeof(long double))/(double)(1024*1024*1024)) << " Gbytes \n";

delete[] c;

return 0;

}

Results (on both): 结果(两个):

Sizeof(long double) = 16 bytes
Allocated Global (Static) size of a = 2.98023 Gbytes 
Allocated Stack size of b = 14.9012 Gbytes 
Allocated Heap Size of c = 59.6046 Gbytes

I am using GCC 4.2.1. 我正在使用GCC 4.2.1。 My question is: 我的问题是:

Why is my program running? 为什么我的程序正在运行? I expected since stack got depleted (16 MB in linux, and 8 MB in Mac), the program should throw an error. 我预计,由于堆栈耗尽(Linux中为16 MB,而Mac中为8 MB),该程序应引发错误。 I saw some of the many questions asked in this topic, but I couldn't solve my problem from the answers given there. 我看到了本主题中提出的许多问题中的一些,但是我无法从那里给出的答案解决我的问题。

Linux overcommits, meaning that it can allow a process more memory than is available on the system, but it is not until that memory is actually used by the process that actual memory (physical main memory or swap space on disk) is allocated for the process. Linux过量使用,这意味着它可以允许一个进程使用比系统上可用内存更多的内存,但是直到该进程实际使用该内存时,才为该进程分配实际内存(物理主内存或磁盘上的交换空间) 。 My guess would be that Mac OS X works in a similar way. 我的猜测是Mac OS X的工作方式类似。

On some systems you can allocate any amount of memory that fits in the address space. 在某些系统上,您可以分配适合地址空间的任意数量的内存。 The problems begin when you start actually using that memory. 当您开始真正使用该内存时,问题就开始了。

What happens is that the OS reserves a virtual address range for the process, without mapping it to anything physical, or even checking that there's enough physical memory (including swap) to back that address range up. 发生的情况是,操作系统为进程保留了虚拟地址范围,而没有将其映射到任何物理地址,甚至没有检查是否有足够的物理内存(包括交换空间)来备份该地址范围。 The mapping only happens in a page-by-page fashion, when the process tries to access newly allocated pages. 当进程尝试访问新分配的页面时,映射仅以逐页的方式发生。 This is called memory overcommitment . 这称为内存过量使用

Try accessing every sysconf(_SC_PAGESIZE) th byte of your huge arrays and see what happens. 尝试访问巨大数组的每个sysconf(_SC_PAGESIZE)个字节,然后看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM