6

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 ?

3
  • 6
    I wonder if you're assuming a false dichotomy between "interpreted" and "native". Java and C#, for example, are neither "interpreted" nor "native" - they run (essentially) in a VM, but from an IL. Commented May 6, 2009 at 10:31
  • Can you please Explain more :? Commented May 6, 2009 at 10:36
  • For that conversation, see the comments on my reply. Commented May 6, 2009 at 10:51

8 Answers 8

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
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.
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.
6

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.

17 Comments

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.
@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 - cheers for the clarification (although see JET/gcj). Actually, MS .NET has an interpreter too - Micro Framework.
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.
Sometimes it's interpreted. But not very often. The bare statement of "Java is interpreted" is very misleading.
|
5

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

Comments

3

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/

Comments

3

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

Comments

2

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

Comments

0

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.

7 Comments

I think that the question is about tracing garbage collection, and refcount is not a substitute.
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: take it up with the rest of the world, not me: codeproject.com/KB/cpp/automatic_gc_using_sp.aspx
@dmitry-vk: The question asked about writing a GC, not about "tracing" vs. "refcount" as a technique for detecting unused pointers.
@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.
|
0

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.

3 Comments

Can you please explain how it works. This is like attaching a GC Thread alongwith your program isn't it ?
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.