Go: C language Request memory when stack size limit

Source: Internet
Author: User

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
    1. int MB = 0;
    2. While (malloc (1 <<))
    3. {
    4. mb++;
    5. }
    6. printf ("Allocate%d MB total\n", MB);

Cannot directly

[CPP]View PlainCopy
    1. size_t MB = (size_t) (2147483648UL);
    2. 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
    1. Stack size (Kbytes,-s) 8192
So in the code

[CPP]View PlainCopy
    1. int mb[2097152]; 4*2097152 = 8192kb
    2. int mb[2090000];
    3. Mb[0] = 0;
    4. MB[2090000-1] = 0;
[CPP]View PlainCopy
    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.