views:

400

answers:

6

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.

+4  A: 

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.

Konrad Rudolph
You may explicitly delete an object in C# if you are using unmanaged code and not be affected by the GC. http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx It appears that Java may be different in that you cannot because it doesn't have an unmanaged mode.
John K
@jdk: that misses the point. Nobody would write a .NET application entirely in unmanaged C# code. And if they did, it simply *wouldn’t be* a .NET application any more because you cannot really use the .NET framework, nor a lot of C#’s capabilities. So while it’s true that C# *can* delete pinned objects (actually, VB can, too, without the need to resort to so-called unsafe code), this doesn’t help in the day to day development.
Konrad Rudolph
@Konrad: I don't disagree with your day-to-day development assertion, just that your original post might be misleading. In context of the author's question about "explicit delete" it's important he be aware of all possibilities made available by Microsoft to programmers. We don't know in detail how he plans to use the knowledge. Even large parts of apps written in unmanaged code exist and that's reason enough to mention it here.
John K
-1 - explicitly calling the garbage collector is a bad idea because (if the runtime system pays any attention to the call) it is most likely to make your application run slower.
Stephen C
@Stephen C: exactly. Which is why advised *against* it. So I don’t understand the -1.
Konrad Rudolph
+7  A: 

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

280Z28
+1 hadn't heard of that before, definitely looks cool.
David Berger
I'm all for good stuff. Thanks for the link. Never heard of it either.
ShaChris23
A: 

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%28VS.71%29.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%28VS.71%29.aspx

For Java, there are many options to optimize it:

http://java.sun.com/docs/hotspot/gc5.0/gc%5Ftuning%5F5.html

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

This is a real science within CS itself.

Nissan Fan
Java and C# are merely languages. The "GC issue" is that of the JVM/CLR implementations.
pst
Thanks. I wasn't aware of that fact and appreciate input from someone like yourself with a vast knowledge in the industry.
Nissan Fan
+3  A: 

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.

John K
using pointers in C#... well it'll be fine as far as you are not dependent on MFCL... which you are not likely to be... And if you are then why use any managed language then... why not just utilize the speed and power of unmanaged environment... which gives you much better performance and speed.
S M Kamran
I agree with your comment; however my response is in context of the author's question about the garbage collector's relationship and affects within C# - unmanaged/managed mode is important to know about for this. Unfortunately I cannot tackle the Java question as well.Also, you might ask Microsoft the same question because they made unmanaged mode available to C# programmers.
John K
@SMKamran - "unmanaged environment ... much better performance and speed". Time to go and check your base assumptions - a managed environment does not automatically mean poor performance and slow execution. In fact, because the runtime has more information available, and more scope to adapt, more optimization is possible. You wouldn't believe any references I gave - so please, go do some real research and check your facts.
Bevan
@Bevan - This is much hyped debate. However not related with the question in context. So I'll save it for some other time, perhaps... cheers.
S M Kamran
@S M Kanran: "why use any managed language then... why not just utilize the speed and power of unmanaged environment". Memory safety?
Jon Harrop
+2  A: 

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.

S M Kamran
+1  A: 

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.

Luke Quinane