GlobalAlloc, HeapAlloc, malloc, new, default heap, dynamic heap

A. Mem Usage and VM Size

Mem Usage - is the working set size. It is the amount of physical memory which is directly (currently) allocated to the process. It can be accessed without causing a page fault. This includes pages shared with other processes.
VM Size - is the total private virtual memory allocated to the process. This is the number you see when you use the Process Private Bytes counter in Performance Monitor.

 

B.heap and stack
  1、函数里的局部变量是在stack里的.  
  2、全局变量,用new、malloc等分配的内存是在heap里的.  
  3、什么时候用heap,什么时候用stack要看你所使用的内存生存期.  
  4、如果使用的变量只在函数里有用,建议用stack.因为heap很慢  
  5、如果使用很大块的内存,建议用heap.因为stack可能会溢出.  
  6、如果在函数里面分配的内存,还想在函数返回之后使用,要用new、malloc,或者定义成静态变量,静态变量是在heap里的.  

 

C. default heap
每一个Process都有自己的default heap,默认初始值为1M. GetProcessHeap()可以得到default heap的HANDLE. 然后可以使用HeapAlloc(...)在default heap中开辟内存。如果HeapAlloc开辟的内存小于1M,则直接使用process启动时自动开辟的1M default heap中的内存,如果大于1M,那么在heap中开辟新的内存,扩大default heap。
为兼容win16时代代码,GlobalAlloc, LocalAlloc具有一样的表现。
Every process in Windows NT has one heap called the default heap. Processes can also have as many other dynamic heaps as they wish, simply by creating and destroying them on the fly. The Win32 subsystem uses the default heap for all global and local memory management functions, and the C run-time library uses the default heap for supporting malloc functions. The heap memory functions, which indicate a specific heap by its handle, use dynamic heaps.
这个msdn中的说法有点问题,malloc应该不使用default heap,使用自己独有的heap。

 

D.Testing code

int _tmain(int argc, _TCHAR* argv[])
{
 char szstr10[1024*500];//1640 848
 char *szstr11 = new char[1024*1024];//2676 1876
 char *szstr12 = new char[1024*1024];//3712 2912
 delete[] szstr11;//0x00420040 2684 1884
 delete[] szstr12;//0x00530040 1652 852
 HANDLE hDefaultHeap = GetProcessHeap();
 char *szstr0 = (char *)HeapAlloc(hDefaultHeap, HEAP_GENERATE_EXCEPTIONS, sizeof(char)*1024*512); //0x00420020 1660 1368
 HeapFree(hDefaultHeap, HEAP_GENERATE_EXCEPTIONS, szstr0);//1656 852
 char *szstr1 = (char *)HeapAlloc(hDefaultHeap, HEAP_GENERATE_EXCEPTIONS, sizeof(char)*1024*1024*1024); //0x10330020 1660 1,051,480
 HeapFree(hDefaultHeap, HEAP_GENERATE_EXCEPTIONS, szstr1);//1656 852
 char *szstr2 = (char *)GlobalAlloc(GMEM_FIXED, sizeof(char)*1024*512);//0x00420020 1660 1368
 GlobalFree((HGLOBAL)szstr2); //1656 852
 char *szstr3 = (char *)GlobalAlloc(GMEM_FIXED, sizeof(char)*1024*1024*1024);//0x10330020 1660 1,051,480
 GlobalFree((HGLOBAL)szstr3); //1656 852
 char *szstr4 = (char *)malloc(sizeof(char)*1024*512);//0x00420040 2172 1368
 free(szstr4);//1656 852
 char *szstr5 = (char *)malloc(sizeof(char)*1024*1024*1024);//0x10330040, slow, 1,053,308 1,051,480
 free(szstr5);//slow 2680 852
 HANDLE hDHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, sizeof(char)*1024*512, 0);//3192 1364
 char* szstr6 = (char *)HeapAlloc(hDHeap, HEAP_GENERATE_EXCEPTIONS, sizeof(char)*1024*512); //0x004a0020 3200 1884
 HeapFree(hDHeap, HEAP_GENERATE_EXCEPTIONS, szstr6);//3192 1364
 HeapDestroy(hDHeap);//2684 856
 int i;
 i = 1;
 
 return 0;
}

 

E.代码运行解析

GlobalAlloc和HeapAlloc的表现差不多,但是他们与new和malloc的表现不一样。new和malloc的表现差不多。
为兼容C Run time lib, malloc具有一样的表现。稍有不同的是,malloc在扩大了VM的同时,也扩大了Mem Usage. new 和malloc具有同样的表现。最奇怪的是当malloc分配大内存的时候,free之后居然Mem Usage多了大约1M出来。难道在这里就泄露了?在malloc 1G之前用new 1G,那么就会有1M在那里消失了,估计是系统用于管理去了。由此看来malloc和new的表现最接近,他们和GlobalAlloc有一定的区别.传说new关键字就是用malloc实现的
以下为另一个说法
C/C++ Run-time (CRT) allocator: Provides malloc() and free() as well as new and delete operators. Languages like Microsoft Visual Basic® and Java also offer new operators and use garbage collection instead of heaps. CRT creates its own private heap, which resides on top of the Win32 heap.
看起来这个说法跟表现差不多。new/malloc需要一个自己的动态heap,并且这个动态heap需要管理成本,这就是使用了new/malloc之后都会有一些内存一直被hold住。因为那个heap被系统管理,我们无法destroy它。
也许是由于年代问题,两篇msdn对crt实用heap的说法不一致。从表现来看,我倾向于后面的那篇。也许是MS修改了实现。

 

F.YY

GlobalAlloc->HeapAlloc

malloc->HeapAlloc

new->malloc->HeapAlloc

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值