Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid garbage collection in real time .NET application?

I'm writting a financial C# application which receive messages from the network, translate them into different object according to the message type and finaly apply the application business logic on them.

The point is that after the business logic is applied, I'm very sure I will never need this instance again. Rather than to wait for the garbage collector to free them, I'd like to explicitly "delete" them.

Is there a better way to do so in C#, should I use a pool of object to reuse always the same set of instance or is there a better strategy.

The goal being to avoid the garbage collection to use any CPU during a time critical process.

like image 550
Seb Avatar asked Sep 17 '08 16:09

Seb


People also ask

How does .NET framework manage garbage collection?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

Does .NET have automatic garbage collection?

Automatic memory management is made possible by Garbage Collection in . NET Framework. When a class object is created at runtime, certain memory space is allocated to it in the heap memory.

How does C# handle garbage collection?

How GC works? GC works on managed heap, which is nothing but a block of memory to store objects, when garbage collection process is put in motion, it checks for dead objects and the objects which are no longer used, then it compacts the space of live object and tries to free more memory.


1 Answers

Don't delete them right away. Calling the garbage collector for each object is a bad idea. Normally you really don't want to mess with the garbage collector at all, and even time critical processes are just race conditions waiting to happen if they're that sensitive.

But if you know you'll have busy vs light load periods for your app, you might try a more general GC.Collect() when you reach a light period to encourage cleanup before the next busy period.

like image 104
Joel Coehoorn Avatar answered Sep 28 '22 06:09

Joel Coehoorn