How to prevent or minimize the negative effects of .NET GC in a real time app?

353 views Asked by At

Are there any tips, tricks and techniques to prevent or minimize slowdowns or temporary freeze of an app because of the .NET GC?

Maybe something along the lines of:

  1. Try to use structs if you can, unless the data is too large or will be mostly used inside other classes, etc.
3

There are 3 answers

2
Henk Holterman On BEST ANSWER

The description of your App does not fit the usual meaning of "realtime". Realtime is commonly used for software that has a max latency in milliseconds or less.

You have a requirement of responsiveness to the user, meaning you could probably tolerate an incidental delay of 500 ms or more. 100 ms won't be noticed.

Luckily for you, the GC won't cause delays that long. And if it did you could use the Server (background) version of the GC, but I know little about the details.

But if your "user experience" does suffer, it probably won't be the GC.

1
Andrey Taptunov On

Another trick is to use GC.RegisterForFullNotifications on back-end.

Let say, that you have load balancing server and N app. servers. When load balancer recieves information about possible full GC on one of the servers it will forward requests to other servers for some time therefore SLA will not be affected by GC (which is especially usefull for x64 boxes where more than 4GB can be addressed).

Updated

No, unfortunately I don't have a code but there is a very simple example at MSDN.com with dummy methods like RedirectRequests and AcceptRequests which can be found here: Garbage Collection Notifications

0
WildCrustacean On

IMHO, if the performance of your application is being affected noticeably by the GC, something is wrong. The GC is designed to work without intervention and without significantly affecting your application. In other words, you shouldn't have to code with the details of the GC in mind.

I would examine the structure of your application and see where the bottlenecks are, maybe using a profiler. Maybe there are places where you could reduce the number of objects that are being created and destroyed.

If parts of your application really need to be real-time, perhaps they should be written in another language that is designed for that sort of thing.