Testing of applying for large memory in linux

Source: Internet
Author: User
Test the application for large memory in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. Operating System: Redhat Linux AS5 32bit
Server Memory: 4 GB
Server Type: I32

Recently, I wrote a search engine. Because creating indexes requires a large amount of memory, I have tested the large memory application in Linux. I hope this will help you.

(1) char * p = (char *) malloc (2G bytes );
=> Application failed.
(2) char * p = (char *) malloc (1.9G bytes );
=> Application successful
(3) apply for 10 MB of memory in a row
For (I = 0; I <10; I ++)
P = (char *) malloc (MB bytes)
=> The first nine successes and the last application failed
(4) first apply for 1.9 GB, then apply for 900 M
P = (char *) malloc (1.9G bytes );
P = (char *) malloc (MB bytes );
=> Both applications are successful.

My understanding is as follows:
For linux kernels in general default 2. 6!
In a 32-bit machine, the memory address space of a process ranges from 0 to 3G, and the last G is the kernel address space, therefore, only the first three G space is left for the user-mode memory address space. in this way, malloc can apply for a memory of less than 3 GB, but this is not the case. in (1), we have not applied for 2 GB of memory. Why? Let's take a look at what the 4G memory space of the process is or is mapped:

------------------------------------------------------------------------------
| User-state address space | library function ing | kernel-state memory space |
------------------------------------------------------------------------------
0 2 3 4
The user-state address space also contains the address space occupied by the Process Code and the stack space.
There is a lot of free space in the G space between 2 and 3.

Now let's explain these four questions:
The (1) problem shows that there is no continuous 2G memory, so applying for 2G continuous memory will certainly fail.
(2), the application for a 1.9G space is successful, because the first two G may have a continuous space of 1.9G.
(3), it is successful to apply for 300 M * 9 = 2.7G. Yes, the first 3G may have a space of GB, add the idle part of the first two G and the idle part of the first 3rd G. however, if you apply for 2.7G at a time, it will not work because there is no consecutive 2.7G address space. the reason why the last M application was not successful is that the size of the requested space cannot exceed 3 GB.
(4). It is interesting to see that 1.9G was successfully applied for in the address space 1-2G. The last 900M was successfully applied for in the address space of 3rd G. we applied for a total of GB "Memory", but it was not continuous.

Note: The test program has no other Code except the code for applying for and finally releasing the memory.

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.