Reducing memory usage of .NET applications?

.NET applications will have a bigger footprint compared to native applications due to the fact that they both have to load the runtime and the application in the process. If you want something really tidy, .NET may not be the best option.

However, keep in mind that if you application is mostly sleeping, the necessary memory pages will be swapped out of memory and thus not really be that much of a burden on the system at large most of the time.

If you want to keep the footprint small, you will have to think about memory usage. Here are a couple of ideas:

  • Reduce the number of objects and make sure not to hold on to any instance longer than required.
  • Be aware of List<T> and similar types that double capacity when needed as they may lead to up 50% waste.
  • You could consider using value types over reference types to force more memory on the stack, but keep in mind that the default stack space is just 1 MB.
  • Avoid objects of more than 85000 bytes, as they will go to LOH which is not compacted and thus may easily get fragmented.

That is probably not an exhaustive list by any means, but just a couple of ideas.

Leave a Comment