64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <windows.h>
using namespace std;
int main(void)
{
ReRun:
int *a = new int[1024];
Sleep(1000);
goto ReRun;
//问题:win7-32位,vc6.0,在任务管理器那查看到,内存每秒增量为8K,为什么不是4K呢?
return 0;
}
// vs2008 win7
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
cout <<"情况1" <<endl;
{
HANDLE hp=HeapCreate(0,0,1); // 提交4k。随便写个小于4k的数,表示堆大小限制为4k,不可自动增长
void *p=HeapAlloc(hp,0,2*1024); // 申请从4k的堆里分配2k
if(p)
cout <<"2k:\t" <<p <<endl;
HeapFree(hp,0,p); // 注释掉这行试试
void *q=HeapAlloc(hp,0,2*1024); // 2k
if(q)
cout <<"2k:\t" <<q <<endl;
HeapDestroy(hp);
}
cout <<endl <<"情况2" <<endl;
{
HANDLE hp=HeapCreate(0,0,1); // 提交4k。限制最大4k
void *p=HeapAlloc(hp,0,3*1024); // 申请分配3k,能成功吗?
if(p)
cout <<"3k:\t" <<p <<endl;
else
{
void *q=HeapAlloc(hp,0,2.5*1024); // 2.5k
if(q)
cout <<"2.5k:\t" <<q <<endl;
}
HeapDestroy(hp);
}
cout <<endl <<"情况3" <<endl;
{
HANDLE hp=HeapCreate(0,0,0); // 提交4k。堆大小无限制,可自动增长
void *p=HeapAlloc(hp,0,3*1024); // 申请分配3k,可以,堆自动增长4k
if(p)
{
cout <<"3k:\t" <<p <<endl;
HeapFree(hp,0,p);
void *q=HeapAlloc(hp,0,4*1024); // 4k呢,也可以,堆自动增长4k
if(q)
cout <<"4k:\t" <<q <<endl;
}
HeapDestroy(hp);
}
cout <<endl <<"情况4:请按键,然后观察任务管理器内存增长,大约30秒" <<endl;
_getch();
{
HANDLE hp=HeapCreate(0,0,0);
for(int i=0;i<60;++i) // 大约每隔2秒,堆自动增长4k
{
void *p=HeapAlloc(hp,0,1024);
Sleep(500);
}
HeapDestroy(hp);
}
return 0;
}