Jump to content
Tuts 4 You

low-fragmentation heap (LFH) C#


CodeExplorer

Recommended Posts

The low-fragmentation heap (LFH) helps to reduce heap fragmentation.

The LFH is not a separate heap. Instead, it is a policy that applications

can enable for their heaps. When the LFH is enabled, the system allocates

memory in certain predetermined sizes. When an application requests a memory allocation

from a heap that has the LFH enabled, the system allocates the smallest block of memory

that is large enough to contain the requested size. The system does not use the LFH for

allocations larger than 16 KB, whether or not the LFH is enabled.

// C# code:

// Imports:

[DllImport("kernel32.dll")]

private static extern uint GetProcessHeaps(uint NumberOfHeaps,

IntPtr[] ProcessHeaps);

[DllImport("kernel32.dll", SetLastError = true)]

private unsafe static extern uint HeapSetInformation(

IntPtr HeapHandle,

int HeapInformationClass,

void *HeapInformation,

uint HeapInformationLength

);

private const int HeapCompatibilityInformation = 0;

public static void SetLFH()

{

uint hcount = GetProcessHeaps(0,null);

IntPtr[] aheaps = new IntPtr[hcount];

hcount = GetProcessHeaps(hcount,aheaps);

uint info = 2;

IntPtr cac = GetProcessHeap();

for (uint i=0;i<hcount;i++)

{

try

{

// Enable the low-fragmentation heap (LFH)

HeapSetInformation(aheaps,

HeapCompatibilityInformation,

(void *)&info,

sizeof(uint));

}

catch

{

// If that method isn't available, like on Win2K,

// don't worry about it.

}

}

}

An the Private bytes increase whit 2 MB and performance is not increased

WTF ? :dunno:

Edited by CodeRipper
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...