views:

96

answers:

3

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  A: 

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.

bde
+2  A: 

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

Andrei Taptunov
+1 - how do you notify the load balancer, do you have code that you can share?
kenny
+4  A: 

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.

Henk Holterman
agreed simply put the rendering at a high priority.
kenny
@Henk: Thanks Henk. I thought GC would freeze my app for a noticable time. I have never seen this but I haven't written say a complete 3d app that does heavy intensive calculations with possibly millions of intermediate/garbage data within a relatively short time. Would GC's move, clean, compact data inside the app's memory space would not lock the whole app down? I thought it would have to because the memory can't be modified when GC is doing its things to it.
Joan Venge