Geek
Geek

Reputation: 23419

Can a Non-Interpreted language have a Garbage Collector?

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 ?

Upvotes: 6

Views: 1162

Answers (8)

dave4420
dave4420

Reputation: 47052

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

Upvotes: 3

Sijmen Mulder
Sijmen Mulder

Reputation: 5819

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.

Upvotes: 0

anon
anon

Reputation:

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/

Upvotes: 3

S.Lott
S.Lott

Reputation: 391854

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.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104178

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

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062820

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.

Upvotes: 6

sharptooth
sharptooth

Reputation: 170499

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.

Upvotes: 12

Related Questions