Windows Memory Management Mechanism and C ++ memory allocation instance (2): memory status query

Source: Internet
Author: User

2. Memory status query function

2.1 system information

Windows provides APIs to query some memory attributes. Sometimes we need to obtain page size, allocation granularity, and other attributes for memory allocation.
See the following C ++ program:
System_info sysinfo;
Getsysteminfo (& sysinfo );
Cout <"machine attributes:" <Endl;
Cout <"page size =" <sysinfo. dwpagesize <Endl;
Cout <"allocation granularity =" <sysinfo. dwallocationgranularity <Endl;
Cout <"minimum value of user zone =" <sysinfo. lpminimumapplicationaddress <Endl;
Cout <"maximum value of user zone =" <sysinfo. lpmaximumapplicationaddress <Endl;
The result is as follows:

It can be seen that the page size is 4 K, the regional allocation granularity is 64 K, and the process user area is 0x0001 0000 ~ 0x7ffe FFFF.

2.2 memory status

· The memory status can be used to obtain the total memory and available memory, including page files and physical memory.
See the following C ++ program:
Memorystatus memstatus;
Globalmemorystatus (& memstatus );
Cout <"Initial memory status:" <Endl;
Cout <"memory busy degree =" <memstatus. dwmemoryload <Endl;
Cout <"total physical memory =" <memstatus. dwtotalphys <Endl;
Cout <"available physical memory =" <memstatus. dwavailphys <Endl;
Cout <"total page file =" <memstatus. dwtotalpagefile <Endl;
Cout <"Available page file =" <memstatus. dwavailpagefile <Endl;
Cout <"total process space =" <memstatus. dwtotalvirtual <Endl;
Cout <"available process space =" <memstatus. dwavailvirtual <Endl;
The result is as follows:

It can be seen that the total physical memory is 1 GB, the available physical memory is 510 MB, and the total page file is 2.5 GB. This is a page file containing the physical memory; the available page file is 1.9 GB. The total process space and available process space are also identified here. The program only uses 22 MB of memory space. Here we talk about the approximate number.
The memory busy program is a busy program that identifies the current system memory management. It ranges from 0 to 100, which is of little use.

· After statically allocating some memory in the function, let's see what happened.
Char stat [65536];
Memorystatus memstatus1;
Globalmemorystatus (& memstatus1 );
Cout <"static space allocation:" <Endl;
Printf ("pointer address = % x/N", STAT );
Cout <"reduce physical memory =" <memstatus. dwAvailPhys-memStatus1.dwAvailPhys <Endl;
Cout <"reduce available page file =" <memstatus. dwAvailPageFile-memStatus1.dwAvailPageFile <Endl;
Cout <"reduce available process space =" <memstatus. dwavailvirtual-memsta tus1.dwavailvirtual <Endl;
The result is as follows:

It can be seen that there is no loss of physical memory, Available page files, and process space. Because local variables are allocated in the thread stack, each thread system creates a stack of 1 MB by default for the thread function call. If the allocation exceeds 1 MB, a stack overflow occurs.

· After dynamically allocating MB of memory in the function, let's see what happened.
Char * dynamic = new char [300*1024*1024];
Memorystatus memstatus2;
Globalmemorystatus (& memstatus2 );
Cout <"dynamically allocate space:" <Endl;
Printf ("pointer address = % x/N", dynamic );
Cout <"reduce physical memory =" <memstatus. dwAvailPhys-memStatus2.dwAvailPhys <Endl;
Cout <"reduce available page file =" <memstatus. dwAvailPageFile-memStatus2.dwAvailPageFile <Endl;
Cout <"reduce available process space =" <memstatus. dwAvailVirtual-memStatus2.dwAvailVirtual <Endl;
The result is as follows:

In the case of dynamic allocation, the system will allocate it until the memory page file is used up. Of course, the system will leave the page used by the system.

2.3 process region address query
After a process space address is given, you can query its region and the status of adjacent pages, including page protection attributes and Storage types.
· C ++ statically allocates two times of memory, 4 K at a time, and about K at a time.
Char arraya [4097];
Char arrayb [900000];
First query:
Long Len = sizeof (memory_basic_information );
Memory_basic_information MBIA;
Virtualquery (arraya, & MBIA, Len );
Cout <"static memory address attribute:" <Endl;
Cout <"region base address =" <MBIA. allocationbase <Endl;
Cout <"area adjacent Page Status =" <MBIA. State <Endl;
Cout <"region protection attribute =" <MBIA. allocationprotect <Endl;
Cout <"Page Base Address =" <MBIA. baseaddress <Endl;
Printf ("arraya pointer address = % x/N", arraya );
Cout <"size starting from the base address of the page =" <MBIA. regionsize <Endl;
Cout <"physical memory type =" <MBIA. Type <Endl;
Cout <"Page protection attribute =" <MBIA. Protect <Endl;
Second query:
Memory_basic_information mbib;
Virtualquery (arrayb, & mbib, Len );
Cout <"static memory address attribute:" <Endl;
Cout <"region base address =" <mbib. allocationbase <Endl;
Cout <"area adjacent Page Status =" <mbib. State <Endl;
Cout <"region protection attribute =" <mbib. allocationprotect <Endl;
Cout <"Page Base Address =" <mbib. baseaddress <Endl;
Printf ("arrayb pointer address = % x/N", arrayb );
Cout <"size starting from the base address of the page =" <mbib. regionsize <Endl;
Cout <"physical memory type =" <mbib. Type <Endl;
Cout <"Page protection attribute =" <mbib. Protect <Endl;
Note: The region base address refers to the process space area where the given address is located;
The adjacent Page Status refers to the properties of the page in the same status as the page where the given address is located: mem_free (idle = 65536), mem_reserve (Reserved = 8192), and mem_commit (submit = 4096 ).
The region protection attribute is the protection attribute that is granted when the region is reserved for the first time: page_readonly (2), page_readwrite (4), page_writecopy (8), page_execute_writecopy (128), and so on.

The base address of a page refers to the base address of the page where the given address is located.
The size of the regional page starting from the base address of the page refers to the page with the same status and protection attribute as the given address.
The physical memory type of the adjacent page refers to the same memory type as the page where the given address is located, including: mem_private (page file = 131072), mem_mapped (File ing = 262144) and mem_image (exe image = 16777216 ).

The page protection attribute refers to the protection attribute specified for the page and is updated after the region protection attribute is specified.
The result is as follows:

As mentioned above, this is allocated in the stack area 0x0004 0000, and the allocated address arrayb is smaller, meeting the characteristics of the stack. Arraya and arrayb are on different pages. Pages are supported by page files, and all regions are submitted. They are submitted when the system is online.

· C ++ dynamically allocates two times of memory, one of which is 1 kb larger and the other is about 64 KB. So it should not be in a region.
Char * dynamica = new char [1024];
Char * dynamicb = new char [65467];
Virtualquery (dynamica, & MBIA, Len );
Cout <"dynamic memory address attribute:" <Endl;
Cout <"region base address =" <MBIA. allocationbase <Endl;
Cout <"area adjacent Page Status =" <MBIA. State <Endl;
Cout <"region protection attribute =" <MBIA. allocationprotect <Endl;
Cout <"Page Base Address =" <MBIA. baseaddress <Endl;
Printf ("dynamica pointer address = % x/N", dynamica );
Cout <"size starting from the base address of the page =" <MBIA. regionsize <Endl;
Cout <"physical memory type =" <MBIA. Type <Endl;
Cout <"Page protection attribute =" <MBIA. Protect <Endl;
Virtualquery (dynamicb, & mbib, Len );
Cout <"dynamic memory address attribute:" <Endl;
Cout <"region base address =" <mbib. allocationbase <Endl;
Cout <"area adjacent Page Status =" <mbib. State <Endl;
Cout <"region protection attribute =" <mbib. allocationprotect <Endl;
Cout <"Page Base Address =" <mbib. baseaddress <Endl;
Printf ("dynamicb pointer address = % x/N", dynamicb );
Cout <"size starting from the base address of the page =" <mbib. regionsize <Endl;
Cout <"physical memory type =" <mbib. Type <Endl;
Cout <"Page protection attribute =" <mbib. Protect <Endl;
The result is as follows:

Dynamic Distribution is used here. dynamica and dynamicb are in two different regions. Similarly, pages are supported by page files and all regions are submitted.
The second region is larger than 64 K. The distribution granularity shows that the region is at least 128 K. Is the remaining space submitted? If yes, it would be too wasteful. You can see that 0x00e2 1000 is definitely in this space, so the query is as follows:
Virtualquery (char *) 0xe23390, & mbib, Len );
Cout <"dynamic memory address attribute:" <Endl;
Cout <"region base address =" <mbib. allocationbase <Endl;
Cout <"area adjacent Page Status =" <mbib. State <Endl;
Cout <"region protection attribute =" <mbib. allocationprotect <Endl;
Cout <"Page Base Address =" <mbib. baseaddress <Endl;
Printf ("dynamicb pointer address = % x/N", 0xe21000 );
Cout <"size starting from the base address of the page =" <mbib. regionsize <Endl;
Cout <"physical memory type =" <mbib. Type <Endl;
Cout <"Page protection attribute =" <mbib. Protect <Endl;
The result is as follows:

It can be seen that the adjacent page status is reserved and has not been submitted yet. expected; the size of 0x00e1 0000 in this area can be calculated as follows: 69632 + 978944 = 1024 K. The system dynamically allocates 1 MB space, which is about 64 kB space. It may be to avoid redistribution when there is a need for allocation next time.

 

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.