Monday, February 12, 2007

Possible Performance Hits from Event Handlers and Callbacks

If a delegate uses an instance callback method, then the delegate contains an implicit this pointer, where this is the instance of the class A containing the callback methed. When a garbage collection occurs, the instance of the class A remains reachable from a rooted reference if the delegate instance is reachable. To allow a GC collects the instance of the class A, make the callback function static.

Adopted from Debug Leaky Apps: Identify And Prevent Memory Leaks In Managed Code by James Kovacs.

Labels:

Large Object Heap Fragmentation

If an object is 85,000 bytes or larger, it is allocated on the large object heap. Unlike the rest of the managed heap, the Large Object Heap is not compacted due to the cost of moving the large objects.

To avoid Large Object Heap fragmentation, the basic strategy is to determine how to reduce the application's reliance on temporary large objects, which are causing the gaps in the large object heap. If the fragmentation is due to re-allocating buffers, maintain a fixed set of buffers that are reused. If the fragmentation is being caused by concatenation of large numbers of strings, examine whether the System.Text.StringBuilder class can reduce the number of temporary strings created.

Adopted from Debug Leaky Apps: Identify And Prevent Memory Leaks In Managed Code by James Kovacs.

Labels:

Leaking Stack Memory in Managed Code

There are only two real ways to leak stack space. The first is to have a method call that consumes significant stack resources and that never returns, thereby never releasing the associated stack frame. The other is by leaking a thread, and thus that thread's entire stack.

By default, the stack size on modern desktop and server versions of Windows® is 1MB.

Adopted from Debug Leaky Apps: Identify And Prevent Memory Leaks In Managed Code by James Kovacs.

Labels: