系统相关
首页 > 系统相关> > 操作系统实验6 系统内存使用统计

操作系统实验6 系统内存使用统计

作者:互联网

实验6 系统内存使用统计

一、实验目的

(1)了解Windows内存管理机制,理解页式存储管理技术。
(2)熟悉Windows内存管理基本数据结构。
(3)掌握Windows内存管理基本API的使用。

二、实验准备

 GlobaiMemoryStatus()             获取系统物理内存和虚拟内存使用信息,将当前物理内存和虚拟内存和虚拟内存信息存储在结构MEMORYSTATUS中
 VirtualAlloc()                   保留或提交某一点虚拟地址空间
 VirtualFree()                    释放或注销某一段虚拟地址空间
 void *malloc(site_t size);       分配内存空间
 Void free(void * memblock);      释放内存空间           

三、实验内容

(一)实验内容

  使用Windows系统提供的函数和数据结构显示系统存储空间的使用情况,当内存和虚拟存储空间变化时,观察系统显示变化情况。

(二)代码

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

#include "stdafx.h"
#include "zuoye06.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") ;
		//分配32M虚拟地址空间
        BaseAddr = VirtualAlloc(NULL,1024*1024*32,MEM_RESERVE,
		PAGE_READWRITE) ;
		if (BaseAddr == NULL)
		   printf ("Virtual Allocate Fail\n") ;
        //分配2M内存空间
		str = (char * )malloc(1024*1024*2) ;
        //获取分配后系统物理内存和虚拟内存使用信息 
		GetMemSta() ;
		printf("Now Release 32M Virtual Memory and 2M Physical Memory\n\n") ;
		//释放32M虚拟地址空间
        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 %d MB\n",MemInfo.dwTotalPhys/(1024*1024)) ;//物理内存大小
			printf("\t Available Physical Memoryis %d MB\n", MemInfo.dwAvailPhys/(1024*1024));//空闲物理内存大小
			printf("\t Total Page File is %d MB\n",MemInfo.dwTotalPageFile/(1024*1024)) ;//页文件大小
	        printf("\t Available Page File is %d MB\n",MemInfo.dwAvailPageFile/(1024*1024)) ;//空闲页文件大小
	        printf("\t Total Virtual Memory is %d MB\n",MemInfo.dwTotalVirtual/(1024*1024)) ;//虚拟地址空间大小
	        printf("\t Available Virtual Memory is %d MB\n",MemInfo.dwAvailVirtual/(1024*1024));
			printf("\t Memory Load is %d % %\n\n",MemInfo.dwMemoryLoad) ;//空闲虚拟地址空间大小
     }

四、实验结果与总结

(1)程序开始运行时,显示可用物理内存为333MB,可用页文件大小为1092MB,可用虚拟内存为2031MB。
(2)当分别使用函数VirtualAlloc()和malloc()分配了32MB虚拟内存和2MB物理内存后,系统显示可用物理内存为331MB,可用页文件大小为1090MB,可用虚拟内存为1997MB。
(3)当分别使用函数VirtualFree()和free()释放了32MB虚拟内存和2MB物理内存后,系统的显示情况又恢复到了(1)的情况。 
 总结:一开始忘记在开始部分进行函数声明( void GetMemSta(void);),导致了一些错误,后改正。另外在根据实验指导书编写代码时,发现了很多拼写错误,后自己改正。      

image

标签:1024,MemInfo,操作系统,Memory,实验,内存,printf,虚拟内存
来源: https://blog.csdn.net/Happy_change/article/details/111293369