Snippet: Enable Low Fragmentation Heap for all heaps
The following code is a code snippet that enable the Low Fragmentation Heap for all heaps. Remembers that a process has at last one heap but for example a CRT console application has practically at last two heaps. The first heap is default heap of process and the second is the CRT heap. If you use a lot of external DLLs is very common that one of this DLL allocate a private heap.
1: // code snippet that try to enable LFH for all heaps of your runnig program.
2: HANDLE* hHeaps = NULL;
3: int numHeap = GetProcessHeaps(0, NULL);
4: if(numHeap > 0)
5: {
6: ULONG ulEnableLFH = 2;
7: hHeaps = (HANDLE*)malloc(sizeof(HANDLE)*numHeap);
8: if(GetProcessHeaps(numHeap, hHeaps) == numHeap)
9: {
10: for(int i = 0; i < numHeap; i++)
11: {
12: if(!HeapSetInformation(hHeaps[i], HeapCompatibilityInformation, &ulEnableLFH, sizeof(ulEnableLFH)))
13: {
14: printf("Failed to enable LFH for heap %d.", i);
15: }
16: }
17: }
18: else
19: {
20: // quite strange !!!
21: }
22: free(hHeaps);
23: }
Suggested reading Windows Internals 4th edition for more details.
Tags: snippet
May 31st, 2009 at 5:47 am
Matteo,
We have several home & business computers running the Windows XP OS & one running Vista. Each of them have low memory & often run poorly. I have no technical training, but I am trying to better understand the Windows OS. At some point, I may buy the latest and greatest technology. For now, I can only make the most of what I have. Earlier today, I read about Mark Russinovich’s Sysinternals Troubleshooting Utilities. I think that I may take your recommendation to buy his book, Windows Internals, 4th Edition. YOUR LINK took me to Amazon.com and gave this extra option that I had never seen before:
Get online access to your physical books with Amazon Upgrade:
* Start reading the book online while you wait for your physical copy to arrive.
* Add highlights, bookmarks, notes, or tags to any page or section of text.
* Print pages, and even copy and paste text from the book.
* Read your book from any Internet-connected computer, meaning your book is always with you.
I don’t know if enabling Low Fragmentation Heap would help, but more memory would be great. Years ago, when defragging my PC, it seemed to take FOREVER. But, the visual on the screen gave the illusion that the drive was compacted completely. Why can’t it be that simple?
Is it possible to have programs and services hosted on a secure, remote, high-speed, server with infinite resources, for a price that I can afford? If so, sign me up. Then, my energy could be directed towards getting work done. Lately, I’ve been stumbling around inside my pc. That hasn’t produced any good results, so it’s time to read directions before I jump in again.
Thanks,
Janice Sikes
May 31st, 2009 at 5:06 pm
Hi Janice,
For that I understood the Low Fragmentation Heap (LFH) don’t help in your case. For the follownig consideration:
* FLH is useful if a program use intensively Heap memory for a long time, this is the case of a Server application like MySQL.
* This settings is made by the program and affect only itself not all system.
Anyway LFH is enabled by default in Vista.
For the second question the answer is yes ^_^, but I can not suggest any service provider because i didn’t have any direct experience with this kind of service. The only service that I know only for the name is a new service of Amazon http://aws.amazon.com/ec2/
Matteo