MSMQ RAM Usage, how to free memory after processing without restarting the Windows service


I have a windows service which process xml files. The details of files to be processed are added to MSMQ. The application retrieves the items in MSMQ using threads, then process these files. When I process more than hundred files I get 'Out of memory exception'. The usage of RAM is high, that means memory in RAM is not freed even after processing of files. I can continue the processing only after restarting the service. How can I free memory without restring the service?


Optimising .NET application memory can be challenging. The first you have to know that in most of the cases the enormous memory consumption is caused by the bad management of the object relations. However, there are a few cases in which the consumption can be caused by the wrong usage of .NET infrastructure.

Consider the following articles:

So what you can do is to change the apartment state or call the garbage collector explicitly. If none of these would not help, the only you can do is to inspect relations between project and most likely you will discover some collection that is not properly maintained.