views:

560

answers:

3

What are the most common memory optimizations in csharp, dotnet 2.0. Wanted to see if there common things that people may not be doing by default in winform app

+6  A: 
  • use structs for small wrapper objects to avoid heap fragmentation
  • think carefully about object lifetimes, especially for learge objects so they do not end up on the LOH unless you intend them to
  • think about allocations inside of a loop
  • make sure dynamically sized array will be of reasonable size, otherwise partition the problem
Nick
+2  A: 

Use StringBuilder instead of directly modifying a string if you're performing many modifications to the same string.

hwiechers
A: 

Sealing as much classes as possible should also help. AFAIK this is one trick that SmartAssembly uses to reduce memory consumption.

Roger Ween
This isn't for memory consumption, but rather for speed. Sealed classes are faster to load and call since you don't have to consider their methods might be overridden.
Omer van Kloeten
Maybe in a future version of the platform the CLR will load all classes as sealed first and patch them if it loads derived types later.
kokos