Heap or stack -- struct vs. class declaration and the influence on performance in .NET

The XmlSerializer namespace in .NET requires the XML structure of a document to be declared as a class or struct. In fact many examples in the wild use the class method to serialize a XML document, which has major disadvantages. One of these disadvantages is that good coding style requires one separate source file for each class to be created, which can make the source tree very large. However the main disadvantage is the performance overhead created by the declaration of an unnecessary class that determines the structure of the XML document. The reason for this performance overhead is the high consumption of memory which is required in .NET for every class which is stored on a stack.

The solution is quite simple: Refactor the according classes to structs. The data structure "struct" requires only a heap in .NET which consumes less memory.


  1. As far as I know struct is stored in stack and not in heap.

  2. Thank you! I have mixed up the entities: Classes are stored on the heap and structs on the stack.

    However you should use structs if it comes to performance.


Post a Comment

Popular posts from this blog

Tuning ext4 for performance with emphasis on SSD usage

NetBeans 6.1: Working with Google´s Android SDK, Groovy and Grails