Garbage Collector in Real-Time System

2.9k views Asked by At

I'm new to C#/Java and plan to prototype it for soft real-time system.

If I wrote C#/Java app just like how I do in C++ in terms of memory management, that is, I explicitly "delete" the objects that I no longer use, then would the app still be affected by garbage collector? If so, how does it affect my app?

Sorry if this sounds like an obvious answer, but being new, I want to be thorough.

6

There are 6 answers

2
Sam Harwell On BEST ANSWER

Take a look at IBM's Metronome, their garbage collector for hard real-time systems.

0
Luke Quinane On

Here are some references for developing real-time systems with the .net compact framework:

They both talk about the memory requirements of using the .net framework.

12
Konrad Rudolph On

Your premise is wrong: you cannot explicitly “delete” objects in either Java or C#, so your application will always be affected by the GC.

You may try to trigger a collection by calling GC.Collect (C#) with an appropriate parameter (e.g. GC.MaxGeneration) but this still doesn’t guarantee that the GC won’t be working at other moments during execution.

2
Keith Adler On

C# and Java are not for Real-Time development. Soft real-time is attainable however as you note.

For C#, the best you can do is implement the finalize/dispose pattern:

http://msdn.microsoft.com/en-us/library/b1yfkh5e(VS.71).aspx

You can request it to collect, but typically it's much better at determining how to do this.

http://msdn.microsoft.com/en-us/library/system.gc(VS.71).aspx

For Java, there are many options to optimize it:

http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html

Along with third party solutions like IBM Metronome as noted above.

This is a real science within CS itself.

5
John K On

By explicitly "delete" if you mean releasing the reference to the object then you are reliant on the garbage collector in C# managed code - see the System.GC class for ways of controlling it.

If you choose to write unmanaged C# code then you will have more control over memory, akin to C++, and will be responsible for deleting your instantiated objects, able to use pointers, etc. For more info see MSDN doc - Unsafe Code and Pointers (C# Programming Guide).

In unmanaged code you will not be at the mercy of the the Garbage Collector and its indeterminate cleanup algorithms.

I don't know if Java has an equivalent unmanaged mode, but this Microsoft info might help provide some direction on C#/.NET to use its available features for your requirement of dealing with the garbage collector.

0
S M Kamran On

In Csharp or Java you can't delete object. What you can do is only mark them available for deletion. The memory free up will be done by Garbage Collector.. It might be the case that Garbage Collector may not run during the life time of your application. However it's likely to run. When your system is becoming short of resources it is the most likely time when GC routines are run by the runtime. And when resources are low GC becomes the highest priority thread. So your application do get effected. However you can minimize the effect by calculating the correct load and required resources for your application life time and make sure to buy the right hardware which is good enough for that. But still you can't just bench mark your performance.

Besides just GC the managed application do get a slight overhead over the traditional C++ application due to the extra delegation layer involved. And a slight first time performance panelty since the run time needs to be up and running before your application get started.