Click here to Skip to main content
15,887,333 members
Please Sign up or sign in to vote.
1.89/5 (3 votes)
See more: , +
In C#.NET, I need an application that will run smoothly. I have many serial chunks of computations I need to consecutively perform in short periods of time each, so I don't mind the GC doing it's job and I even can take more frequent collections but what I need to minimize is the length of each GC collection.

I would like (if possible) to have 1 milli max pause of thread activity due to the GC each time.

what is the best way to acheive this in .NET (I know that .NET it not the technology for such demands but if it will meet my demands when optimized the save of development hours and flexibility for future specs is good incentive to try it out)?
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jul-15 2:14am    
What makes you thinking that you need this? It sounds like a big misconception to me. This is not what makes your application not smooth, but some pure design. It's hard to discuss without knowing a lot more detail on your application.
—SA
Joezer BH 9-Jul-15 3:53am    
This time, you have made a premature judgement Sergey perhaps you didn't understand the question. Short time scheduled tasks and time syncronizations are integral to my system's structure (cannot elborate here due to intelectual property). What makes the application not smooth is occational "lengthy" GC activity that take up to 10 milli seconds. So if I can reduce those to around 1 milli, and providing that I manage my computation cycles correctly, I can improve the "smoothness" of my application tenfold. This is a major imporvment for my system.
Sergey Alexandrovich Kryukov 9-Jul-15 11:06am    
Sorry if it sounded as my categorical statement. I did not mean it. I says "sounds like", because I did not really understand the problem; you are right here. At the same time, all you say sounds way too doubtful to me. You are not dealing with a real-time system, and you cannot directly and deterministically affect thread scheduling. You can only change priorities and... your design. You cannot really affect the scheduling of GC behavior. All you mention is completely out of control.

At the same time, some kind of "smoothness" is quite possible. For example, I can explain why animation of some "physics" can be smooth. This is done by getting real time measurement to be used in equations, without making any assumptions on the timing of your computational cycle.

Unfortunately "cannot elborate here due to intellectual property" reduces your prospects of getting a helpful advice greatly. Isn't that funny? you are trying too keep "intellectual property" in secret, and yet, you need an open-source advice.

—SA

You are not dealing with a real-time system, and you cannot directly and deterministically affect thread scheduling. You can only change priorities and… your design. You cannot really affect the scheduling of GC behavior. All you mention is completely out of control.

At the same time, some kind of "smoothness" is quite possible. For example, I can explain why animation of some "physics" can be smooth. This is done by getting real time measurement to be used in equations, without making any assumptions on the timing of your computational cycle.

The conclusion would be: you either can achieve "smoothness" by certain application design, and, in worse cases, by adjusting process and thread priorities (which can give you only probabilistic benefits), or you cannot improve anything at all. In all cases, you cannot get any improvements based on affecting scheduling of GC or, say, thread scheduler. GC scheduling is done by its own algorithm and cannot be affected. For example, if you take memory in smaller chunks, it won't be reclaimed in the same smaller chunks. Besides, you probably exaggerate the performance cost of GC.

And please think at my comment to your question, on the premature judgement and intellectual property.

—SA
 
Share this answer
 
If garbage collection is a problem for your application, then maybe you should not use .NET in that case. This is well known that managed code could have those delays. So for application like video editing or low-latency multi-track audio, native programming would generally be recommand.

.NET make it easy to write many kind of applications but when very low latency is required, it is not the appropriate technology.

Depending on the application you might be able to mostly avoid the problem if you don't use much managed memory or optimize how it is used by maybe allocating memory before critical processing.

As already mentionned, there are not much you can do if you need to ensure the system won't pause for more than 1 ms. As far as I know, pause can be much higher than that.

Maybe one solution would be that critical code would run in its own umnmaged thread. Not sure if it possible in same application but it should be possible with 2 applications that communicate with pipe or shared memory or something like that.

If that does not work, then consider rewriting your application as a native one.
 
Share this answer
 
Comments
Philippe Mori 9-Jul-15 20:40pm    
By the way native applications are not real-time but are not affected by GC collecting cycles so they are much more deterministic. This is well known.
Philippe Mori 9-Jul-15 20:50pm    
If you can tolerate some slow garbage collections, then optimizing the application by using less memory allocation might help. You might also do explicit collections at some interval or number of iteration to help ensure that collection would be fast. You would then have to tune your application on a slow system with not much memory...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900