There has always been a question of how much memory space, swap swap space, and physical memory size a process can use, and how ulimit stack size limits the memory usage of the process? Today special hands-on experiment, summarized as follows:
There are 2 ways to open up a memory space, the first: int a[]; the second type of malloc, how much memory can these two ways open up under Linux? The following experiments are performed in turn:
The first way: use malloc to request memory;
Such a way is to apply the memory in the heap area , in Linux, in fact, when the application is basically no limit, such as 32-bit machine, can theoretically malloc (4G) size, because 2^32=4g, but in fact, the Linux process address space is like this:
So after the experiment, the maximum amount of space that can be applied to malloc is around 3G, so be careful to apply for space using the following method:
[CPP]View PlainCopy
- int MB = 0;
- While (malloc (1 <<))
- {
- mb++;
- }
- printf ("Allocate%d MB total\n", MB);
Cannot directly
[CPP]View PlainCopy
- size_t MB = (size_t) (2147483648UL);
- char *buf = (char*) malloc (MB);
Because there may be fragmentation in memory, the sum of memory free space may be 3G, but requesting 3G directly may not succeed because it is not contiguous memory space.
Then I was confused, why the application heap space is not limited by the size of swap space and physical memory? Because Linux uses virtual memory, so the allocation is not affected, but, in use, we use the same amount of memory than the swap space and physical memory size, there will be some problems, here is a very good article, noted: http://www.cfanz.cn/ index.php?c=article&a=read&id=103888
Second way: Use int a[] to apply for memory;
Such a way is to apply the memory in the stack, in Linux, will be affected by the stack size result in ulimit-a
Like my ulimit-a results.
[CPP]View PlainCopy
- Stack size (Kbytes,-s) 8192
So in the code
[CPP]View PlainCopy
- int mb[2097152]; 4*2097152 = 8192kb
- int mb[2090000];
- Mb[0] = 0;
- MB[2090000-1] = 0;
[CPP]View PlainCopy
- int mb[2097152];
Using int mb[2097152] will fail, because the stack may hold parameters, return addresses, and so on, already occupy a portion of the stack, the following mb[2090000] can be successful!
So summarize: If you use malloc, a process can theoretically use 3G of memory (should be said to be visible), but the real maximum memory can be used at the same time only swap space + physical space is so large
Using the form int a[], the requested space is affected by the stack size in the ulimit-a.
PS: In fact, I think the stack should not be said together ~ The concept of the two of them is still a lot worse ~ I searched the article all mixed together said, very easy to confuse people ....
Note the following articles:
http://www.cfanz.cn/index.php?c=article&a=read&id=103888
http://blog.csdn.net/anghlq/article/details/7087069
Http://tech.ddvip.com/2013-05/1369680397196183.html
Http://www.jb51.net/LINUXjishu/34605.html
Go: C language Request memory when stack size limit