Experiment 6 system memory usage statistics

Experiment 6 system memory usage statistics

1, Purpose of the experiment

  1. Understand Windows memory management mechanism and page storage management technology.
  2. Familiar with basic data structure of Windows memory management.
  3. Master the use of Windows memory management basic API.

2, Experiment preparation

Related data structure and API function introduction

  1. Related system data structure description

The system structure MEMORYSTATUS contains the current physical memory and virtual memory and virtual memory information, which can be stored in the structure MEMORYSTATUS by using the function globalmemorystatus().

Structural prototype:

Typedef struct_MRMORYSTATUS{
	DWORD dwLength;                        	//MOMORYSTATUS structure size
	DWORD dwMemoryLoad;                  	//Memory utilization
	DWORD dwTotalPhys;                      //Physical memory size
	DWORD dwAvailPhys;                      //Free physical memory size
	DWORD dwTotalPageFile;                  //Page file size
	DWORD dwAvailPageFile;                  //Free page file size
	DWORD dwTotalVirtual;                   //Virtual address space size
	DWORD dwAvailVirtual;                   //Size of free virtual address space
} MEMORYSTATUS, *LPMEMORYSTAYUS;

Member Description:

  • Dwlength; the size of the MEMORYSTATUS data structure, in bytes.
  • dwMemoryLoad: the current memory utilization. The value range is 0-100%. 0 indicates that the memory is not used, and 100% indicates that all the memory is used.
  • dwTotalPhys: the total number of bytes of physical memory.
  • dwAvailPhys: the number of bytes of available physical memory.
  • dwTotalPageFile: the total number of bytes in the page file. The page file is the disk space occupied by the virtual memory system.
  • dwAvailPageFile: the number of bytes available in the page file.
  • dwTotalVirtual: the total number of bytes of virtual address space that can be accessed by the calling process in user mode.
  • dwAvailVirtual: the total number of uncommitted and unreserved memory bytes in the virtual address space of the calling process in user mode, that is, the size of the available virtual address space.
  1. Introduction to related API functions

Get system physical memory and virtual memory usage information

Prototype:

VID GlobalMemoryStatus(
	LPMEMORYSTATUS lpBuffer             //Point to MEMORYSTATUS data structure
);

Parameter Description:

  • lpBuffer: pointer to the MEMORYSTATUS data structure in which the GlobaiMemoryStatus() function stores the current memory information.

Return value:
The parameter does not return a value.

Reserve or submit a point of virtual address space

The VirtualAlloc() function can hold or submit several pages in the virtual address space of the calling process. Reservation means that the virtual address can't be used. When it is submitted, the virtual address is actually assigned to the process.

Prototype:

VPVOID VirtuaiAlloc( 
	LPVOID lpAddress,                   //Start of space to be allocated
	DWORD dwSize,                       //Size of space to be allocated
	DWORD flAllocationType,             //Assignment type
	DWORD flaProtect                    //Types of access protection
);

Parameter Description:

  • lpAddress: the actual location of the space to be allocated. If the value is NULL, the system will assign an appropriate starting address to it, otherwise the user will specify an exact starting address.

  • DwSize: the size of the space to be allocated. If the parameter lpAddress is not NULL, the space to be allocated is in the range of lpAddress~ lpAddress+ dwSize.

  • flAllocationType: allocation type, which can be any combination of flags listed in table 3-1.

  • Table 3-1

    sign describe
    MEM_COMMMIT Commit, which allocates physical memory in a memory or disk page file
    MEM_RESERVE Keep the virtual address space of the process without allocating physical memory. The space left by the table cannot be used by other allocation operations such as Malloe() and LocalAlloc() until it is freed. The reserved address space can then be submitted using the VirtuaiAlloc() function
  • flProtect: Specifies the type of access protection. If the virtual address space has been submitted, the following properties should be specified with the page_ Guard and page_ Nocache (page has no cache) these two properties. The types of access protection bits are shown in table 3-2.

  • Table 3-2

    Records describe
    AGE_READONLY Submitted virtual address space is read-only
    AGE_READWRITE Submitted virtual address space read / write
    AGE_EXECUTE The submitted virtual address space is executable
    AGE_EXECUTE_READ The submitted virtual address space is executable and readable
    AGE_EXECUTE_READWRITE The submitted virtual address space is executable, readable / writable
    AGE_GUARD protect
    AGE_NOACCESS Access not allowed
    AGE_NOCACHE No cache

Return value:
If the function call succeeds, the return value is the starting address of the allocated virtual address space. If the function call fails, the return value is NULL. For more error information, call the GetLast6Error() function.

Release or unregister a virtual address space

The VirtualFree() function is used to free or unregister a virtual address space.
Prototype:

BOOL VirtualFree(
	LPVOID lpAddress,              //Start of space to be allocated
	DWORD dwSize,                  //Size of space to be allocated
	DWORD dwFreeType               //Release operation type
);

Parameter Description:

  • lpAddress: the starting location of the space to be freed. If dwFreeType value is MEM_RELEASE, which must use the address returned by the VirtualAlloc() function.

  • DwSize: the size of the space to be freed. If dwFreeType value is MEM_RELEASE, the parameter must be 0, otherwise the space to be freed is in the range of lpAddress~ lpAddress+dwSize.

  • dwFreeType: release type. It can be any combination of the flags shown in table 3-3.

  • Table 3-3

    Records describe
    EM_DECOMMIT If you log off a virtual address space that has not been submitted, you will not fail, that is, you can log off a virtual address space that has been submitted or has not been submitted
    EM_RELEASE Release the reserved virtual address space. If this flag is used, the dwSize parameter must be 0, otherwise the function fails

Return value:
If the function call succeeds, the return value is nonzero.
If the function call fails, the return value is zero. For more error information, call the GetLastError() function.

Allocate memory space

Prototype:

   void *malloc(site_t size);

Parameter Description:

  • Size: the size of memory to be allocated, in B (bytes)

Return value:
This function returns a pointer to the void type of the allocated memory space. If NULL is returned, there is no valid memory space to allocate.

Free up memory

Prototype:

Void free(void * memblock);

Parameter Description:

  • *memblock: the address of the memory to release.

Return value: none.

Usage example:

/*MALLOC.C:This grogram allocates memory with
*malloc,then frees the memory with free
*/
#include<stdlib.h>
   #include<stdio.h>
            #include<malloc.h>
            Void main(void)
           {
              Char * string;
              /*Allocate space for a path name*/
              String=malloc(_MAX_PATH);       //Allocate memory space
              if(string==NULL)
                   printf("Insufficient memory available\n");
                   free(string);
                   printf("Memory freed\n");
              }
}

3, Experiment content

(1) Experiment content

Use the functions and data structures provided by Windows system to display the usage of system storage space. When the memory and virtual storage space change, observe the change of system display.

(2) Main code

// GetMemoryStatus.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "GetMemoryStatus.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void GetMemSta(void);

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	LPVOID BaseAddr;
	char * str;

	GetMemSta();
	printf("Now Allocate 32M Virtual Memory and 2M Physical Memory\n\n") ;
	BaseAddr=VirtualAlloc(NULL,1024*1024*32, MEM_COMMIT,PAGE_READWRITE);
	if (BaseAddr == NULL)
	printf ("Virtual Allocate Fail\n");
	str = (char * )malloc(1024*1024*2);
	GetMemSta();
	printf("Now Release 32M Virtual Memory and 2M Physical Memory\n\n");
	if (VirtualFree(BaseAddr,0,MEM_RELEASE) == 0)
	printf("Release Allocation Fail\n");
	free (str);
	GetMemSta();

	return nRetCode;
}

void GetMemSta(void)
{
    MEMORYSTATUS MemInfo ;
    GlobalMemoryStatus (&MemInfo) ;
    printf("\t Current Memory Status is : \n") ;
    printf("\t Total Physical Memory is % dMB\n",MemInfo.dwTotalPhys/(1024*1024)) ;
    printf("\t Available Physical Memoryis % dMB\n", MemInfo.dwAvailPhys/(1024*1024));
    printf("\t Total Page File is % dMB\n",MemInfo.dwTotalPageFile/(1024*1024)) ;
	printf("\t Available Page File is % dMB\n",MemInfo.dwAvailPageFile/(1024*1024)) ;
	printf("\t Total Virtual Memory is % dMB\n",MemInfo.dwTotalVirtual/(1024*1024)) ;
	printf("\t Available Virtual Memory is % dMB\n",MemInfo.dwAvailVirtual/(1024*1024)) ;
	printf("\t Memory Load is % d% %\n\n",MemInfo.dwMemoryLoad) ;
}

4, Experimental results and summary

  • When the program starts to run, the available physical memory is 1736MB, the available page file size is 3774MB, and the available virtual memory is 2031MB.
  • When the functions VirtualAlloc() and malloc() are used to allocate 32MB of virtual memory and 2MB of physical memory respectively, the system shows that the available physical memory is 1734MB, the available page file size is 3740MB, and the available virtual memory is 1997MB.
  • When 32MB of virtual memory and 2MB of physical memory are released by using the VirtualFree() and free() functions respectively, the display of the system returns to the beginning.

Tags: Windows

Posted on Thu, 18 Jun 2020 22:20:41 -0400 by Cardale