views:

407

answers:

8

Is it possible for a Non-Interpreted language to have a Garbage collector. Interpreted languages have the Interpretor executing the Program line by line so the Interpretor might just as well provide a runtime with a GC. But is it possible to have a Garbage collector for any other language without building the GC in your code itself ?

A: 

Yes.

C++ with a smart pointer implementation will garbage collect as the smart pointer reference counts go to zero.

You have garbage collection. You did not build it yourself.

S.Lott
I think that the question is about tracing garbage collection, and refcount is not a substitute.
dmitry_vk
That's not garbage collection. Connect several objects such way that they have a cycle and they are never destroyed unless you break the cycle manually.
sharptooth
@sharptooth: take it up with the rest of the world, not me: http://www.codeproject.com/KB/cpp/automatic_gc_using_sp.aspx
S.Lott
@dmitry-vk: The question asked about writing a GC, not about "tracing" vs. "refcount" as a technique for detecting unused pointers.
S.Lott
Reference-counting smart pointers are well-known to be prone to graph cycles because "garbage collection" in smart pointers relies on C++ code. Connect two or more objects into a double-linked list and you've got a graph with a cycle which will lead to a leak once you've made this graph isolated from other objects. Runtime-supported garbage collection is not prone to this problem. Managing lifetime with smart pointers is not to be confused with garbage collection.
sharptooth
@sharptooth -- smart pointers have limitations. However, they are garbage collection and they are code you don't write. Again, if you don't like this definition, you'll have to take it up with a lot of people. Search for smart pointer garbage collection and you get a lot of hits.
S.Lott
People often do confuse the two - the original version of python used reference counting but described itself has being garbage collected. They had to add true garbage collection later to fix problems with ref counting.
anon
+9  A: 

Garbage collection only requires the pointer variables be marked special way so that the runtime can identify them and use for garbage collection. It has nothing to do with interpretation/compilation, but instead requires special runtime and storing additional data with each variable.

sharptooth
Well Interpreted languages give you a Run-Time. Atleast it is easier for the Interpretor to work as a Run-Time and run a garbage collector too. Any Native language will have the OS as the runtime ? And hence no GC ?
Geek
GC and interpretation are unrelated to each other. Yes, it's potentially easier for an interpreted language to have garbage collection, but non-interpreted languages can do this too. OSes usually don't have embedded support for garbage collection, but many language runtimes have.
sharptooth
And even without marking the pointer variables in a special way you can have so-called conservative GC that treats all memory contents as potential pointers. See Boehm's GC in the other answer.
Laurynas Biveinis
+6  A: 

Well, .NET languages (that emit to IL - C#, VB.NET, MC++, etc) aren't interpreted (especially if you use NGEN) - and has full garbage collection.

Likewise, Java.

Marc Gravell
Java is Interpreted my Friend. Even if it is with HotSpot.
Geek
No - it might not be "native", but that doesn't make it "interpreted" - at least, not under the normal definition.
Marc Gravell
I dontget your answer "at least, not under the normal definition" ?It is Interpreted. ??
Geek
I believe HotSpot interprets for the first pass (unless it sees a loop, or something like that) but will then JIT compile. At that point it's definitely *not* interpreted.
Jon Skeet
"Interpreted", to me, means things without a compiler; javascript etc - or things from this list: http://en.wikipedia.org/wiki/Categorical_list_of_programming_languages#Interpreted_languages
Marc Gravell
The JVM is an interpreter. Interpreting compiled byte-codes is still interpreting.
S.Lott
C# and Java *are* compiled - but not to machine bytecode: to an IL specific to their VM. C# compiles to MSIL; Java compiles to JVM bytecode. The JIT/HotSpot turns the IL into native code, but this is a simple translation - the compiled IL is already optimised for this.
Marc Gravell
A JVM *may* be an interpreter - but it doesn't have to be. If it compiles the byte code into native code and executes that, in what way is it still an interpreter?
Jon Skeet
@Marc: I'd say that Java *source code* is never interpreted, but Java bytecode may be. In real life it's interpreted briefly and then JIT compiled. In .NET, IL is never interpreted - but Mono has an IL interpreter. Note that JavaScript isn't always interpreted either - modern JS engines do JIT compilation.
Jon Skeet
urther, both .NET and Java have native compilers: NGEN and JET/gcj. They still use the runtime for library support, garbage collection, hosting, etc, but are now pre-compiled to machine bytecode.
Marc Gravell
@Jon - cheers for the clarification (although see JET/gcj). Actually, MS .NET has an interpreter too - Micro Framework.
Marc Gravell
So to summarise: Java/C# is compiled to bytecode/IL. That bytecode/IL is *usually* (most of the execution time, most environments) JIT compiled; occasionally it's interpreted. Calling Java and C# "interpreted languages" is a mistake though.
Jon Skeet
@JON so finally we agree that it is Interpreted ;-) Atleast partially :-)
Geek
*Sometimes* it's interpreted. But not very often. The bare statement of "Java is interpreted" is *very* misleading.
Jon Skeet
Java is not an interpreted language. I am sure you have heard of this thing called javac, a compiler. The fact that there is a loop somewhere processing sequences of instructions makes the JVM no more an interpreter than a CPU. (CPUs loop too, you know ..)A JIT is an *optimization* mechanism which *overrides* the default compiled byte codes based on live usage metrics.And of course GC is an entirely orthogonal matter and is specifically related to un-managed heap access.
@alphazero: Early JVMs certainly *were* interpreters, although they were interpreters of bytecode rather than Java code.
Jon Skeet
@Jon: Exactly the point: The JVM "bytecode" != Java expression. QED. Java is not an interpreted *language* and never has been.
A: 

Objective-C 2 has garbage collection now, and there are garbage collection libraries available for C++ as well.

I think it's possible as long as there is it the language allows you to inspect objects so you can traverse the object tree.

sjmulder
Can you please explain how it works. This is like attaching a GC Thread alongwith your program isn't it ?
Geek
Separate thread is unrelated to GC -- that just happens to be the way Java does it. Most C++ (and Objective-C) handle it at delete time when the ref count goes to zero.
S.Lott
Objective-C 2 has real garbage collection; the runtime traverses the object tree. Refcounts are ignored when garbage collection is enabled. I believe it's part of the event loop, but I'm not sure.
sjmulder
+1  A: 

The new C++0x includes features that make implementation of garbage collection easier. See this interview for example.

kgiannakakis
+4  A: 

Yes - http://www.hpl.hp.com/personal/Hans_Boehm/gc/

Richard Nichols
+1  A: 

For an actual implementation in a compiled language, in this case C and/or C++, see the Boehm GC at http://www.hpl.hp.com/personal/Hans_Boehm/gc/

anon
+1  A: 

Haskell has garbage collection, whether it's compiled to native code or interpreted.

Dave Hinton