5

are there any CLR implementations that have deterministic garbage collection?

Nondeterministic pauses in the MS CLR GC inhibit .Net from being a suitable environment for real-time development.

Metronome GC and BEA JRockit in Java are two deterministic GC implementations that I'm aware of.

But have there any .Net equivalents?

Thanks

DayOne
  • 1,027
  • 1
  • 11
  • 16

3 Answers3

5

There is no way to make the GC deterministic, expect of course from calling GC.Collect() exactly every second using a timer ;-).

The GC however does contain a notification mechanism (since .NET 3.5 SP1) that allows you to be notified when a gen 2 collect is about to happen. You can read about it here.

The GC now also contains multiple latency modes that make it possible to prevent any GC collects from occurring. Of course you should be very careful with this, but is especially useful for real-time systems. You can read more about it here.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • 1
    Just to add, this doesn't make it deterministic - it can still call itself whenever it likes and you've no idea what it will GC, or how long it will take. And your timer is not going to be able to tick *exactly* every 250ms. – Adam Houldsworth Jul 02 '10 at 15:35
  • 4
    I hoped the smiley at the end of that sentence made it clear that I wasn't serious. But seriously, calling `GC.Collect()` every 250 ms. is a very bad idea. While having the collects at deterministic intervals, you would have very bad performance because you trigger many gen 2 collects. – Steven Jul 02 '10 at 23:21
  • @DayOne: That sounds like a *really* bad idea. By calling GC.Collect like that you will force the most expensive type of collection on a regular basis. You application will spend more time collecting than it needs to. Keep in mind that all your user threads are suspended during parts of the collection. – Brian Rasmussen Jul 06 '10 at 07:40
  • @BrianRasmussen Good point Brian, but I have seen some other companies address this. This is off topic for CLR, but Azul Systems (Linux based) has a JVM that optimizes continuoulsy. They do this by running GC in parallel on a seperate thread and leveraging extra memory to move things around. This way your pauses are very small (almost non-existant) and you never hit stop the world / old generational GC. I see CLR making some progress (use of seperate thrades for GC in ASP.NET 4.5) but as mentioned, microsoft is not a real time OS ... I wonder if Mono could offer a work-around? – Zack Jannsen Dec 12 '12 at 17:37
  • 1
    @Steven: even with your tongue in cheek (due to the poor performance) it is important that readers understand: Calling GC.Collect() will **not** free **everything** that is not currently reachable. At minimum, do `GC.Collect(0, GCCollectionMode.Forced)`, `GC.WaitForPendingFinalizers()`, `GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced)`. This call sequence: gets recent garbage to its finalizers, waits for those finalizers, then collects all generations. Even then, unused memory may not be released back to OS; pages may still be marked reserved for specific use by .Net. – ToolmakerSteve Mar 22 '14 at 20:06
  • 1
    Its also important to be aware that calling GC.Collect() too often can be harmful: it forces stuff from generation 0 to generation 1, and eventually to generation 2, making it slower to collect. **ONLY** call GC.Collect() at key points, after you have finished a major computation sequence, and no longer have any references to a lot of large temporary data objects. This includes no references in stack frames; may require setting local variables to "none", or only doing from a high level in your code, after returning from your main computation methods. – ToolmakerSteve Mar 22 '14 at 20:12
2

No, there are non. From my experience .net can't be used to create real time systems for many reasons, not only about garbage collection. C or C++ are better choice. Also modern OSes do not provide deterministic scheduling, and it is about all applications, regardless of language.

Andrey
  • 59,039
  • 12
  • 119
  • 163
0

You would have to control the GC yourself in order to get predictable real-time behaviour, but if you are doing this then you may as well not use a managed language.

For real-time systems you need control over everything that is running. There are third-party modifications to Windows XP that make it real-time (can't remember if it's soft or hard real-time though).

Completely unfeasible option. Look into Cosmos OS - written in C# and compiled to assembler I think - might be able to do something with that :)

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187