Memory Management 2

Source: Internet
Author: User

The number of addresses provided by the system to the program is 4 GB. Why not 3G or 5G? Because the maximum value of the 32-bit pointer is $ ffffffff, it cannot represent more, the root cause is to return to the addressing capability of the CPU, address bus, and so on.

In win64, the number of IP addresses provided by the system to the Program reaches 16 EB (0-$ ffffffffffffffff), that is, 18446744073709551616. However, win64 is not popular yet, and we have to return to the actual win32.

For these 4G addresses, the system will leave half of them ($80000000-$ ffffffff, which is shared by various processes) for macro management; only 2G (0-$7 fffffff) for the program ).
The 2G address is not all for the user. The low-end 0-$ FFFF is used for NULL pointer allocation and access is prohibited; high-end $7fff0000-$7 fffff is also left as the critical section of the process and access is prohibited. in fact, the private space address of the process is only $10000-$ 7feffff.

The above result can be confirmed through the getsysteminfo function. The getsysteminfo function can be used to obtain a tsysteminfo structure. The lpminimumapplicationaddress and lpmaximumapplicationaddress in the structure indicate programs (or dynamic link libraries) respectively) the lowest and highest memory address that can be accessed.
--------------------------------------------------------------------------------

VaR
Si: tsysteminfo;
Begin
Getsysteminfo (SI );
Showmessagefmt ('% P-% P', [Si. lpminimumapplicationaddress, Si. lpmaximumapplicationaddress]);
{Result: 000316-7ffeffff}
End;
--------------------------------------------------------------------------------
You can also get an important memory parameter through getsysteminfo: page size (pagesize)
--------------------------------------------------------------------------------

VaR
Si: tsysteminfo;
Begin
Getsysteminfo (SI );
Showmessage (inttostr (Si. dwpagesize); {4096; 4096 bytes, that is, 4 k}
End;
--------------------------------------------------------------------------------
Pagesize is the basic unit for system memory management. getsysteminfo is used to obtain pagesize of different systems.

We need to know that the memory allocated by using the virtualalloc function is in the smallest unit of pagesize (4 K). If we use virtualalloc to allocate memory to an integer (4 bytes, it will waste 4092 bytes. That is to say, virtualalloc is not suitable for allocating small memory, so there are also a variety of memory allocation functions.
--------------------------------------------------------------------------------

Now let's move on to this topic. First, let's take a look at "stack ".

Speaking of "stack", I think of "stack", and "stack" refers to "stack "; "stack" and "Heap" are a small segment in the memory area ($10000-$ ffeffff) that the program can operate on.

System functions include heaprealloc, globalalloc, and other functions that allocate "Heap", but do not allocate "stack" functions, because "stack" is automatically managed by programs; each Program sets aside a piece from its available address range as a "stack". The program can automatically adjust its size as needed, but we can set its maximum and minimum values. in Delphi, you can set it here:
Project-> options-> linker-> [min stack size and Max stack size]

"Stack" is used to store local variables and function parameters. It is applied by the program when needed and released when used up.

Because the space of "stack" is not very large, we should not make the local variable too large (especially when using arrays );
Because accessing the "stack" is simpler and faster than accessing the "Heap", we recommend that you use local variables and global variables as much as possible.

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.