Java has automatic garbage collection. Why wasn’t similar feature added to C++ in revised versions of language?

It depends on what you mean by "garbage collector."
First there are two major classifications:
1. Those that are deterministic: you know when the resources are freed. And those that arent
2. Those that compact memory addresses by moving object in.memory, and those that dont
First, consider the stack: it allocates memory, and automatically frees it. Coupled with destructors, ANY resource is easily managed, not just memory. C++ heavily relies on this GC, but try this with Java or C# and you're stuck with finally.
Java GC means memory only: locks, file handles etc you're on your own.
For heap memory, C++ has long had reference counting available. Perl and python, and lisp I believe as well, both use reference counted GC. You just have to tell C++ which object are reference counted and which are stack. Additionally, there are other methods, such as scoped_ptr.
I review a lot of c++ code, and I can tell you a huge no-no is when I see a programmer use "delete." A raw delete is an esoteric keyword, and when I see it used liberally, its guaranteed there's a leak somewhere.
So C++ has long had GC capability, its just not the same approach as Java.
The other classification, is GC that compacts. It's already true there is some magic in the OS and TLB about virtual addresses, a compacting GC is another layer on top of that.
C and C++ have the notion of allocation "arenas." Meaning I can place different objects in memory and have them keep their relative position. Furthermore, I can specify with great detail just how a object is laid out. I can't stress enough how important this is. The vast majority of the Standard library woundnt work if data in memory could be moved. And no one wants it moved– that would confuse the optimizer.

GC normally means non deterministic algorithms that sweep through names to see which ones cannot be reached, and reorders the object placement along the way. But it can mean deterministic routines, such as stacks and reference counts, that preserve location. And in that sense C++ has long had GC capability

Answer by Lance Diduck:

It depends on what you mean by "garbage collector."
First there are two major classifications:
1. Those that are deterministic: you know when the resources are freed. And those that arent
2. Those that compact memory addresses by moving object in.memory, and those that dont
First, consider the stack: it allocates memory, and automatically frees it. Coupled with destructors, ANY resource is easily managed, not just memory. C++ heavily relies on this GC, but try this with Java or C# and you're stuck with finally.
Java GC means memory only: locks, file handles etc you're on your own.
For heap memory, C++ has long had reference counting available. Perl and python, and lisp I believe as well, both use reference counted GC. You just have to tell C++ which object are reference counted and which are stack. Additionally, there are other methods, such as scoped_ptr.
I review a lot of c++ code, and I can tell you a huge no-no is when I see a programmer use "delete." A raw delete is an esoteric keyword, and when I see it used liberally, its guaranteed there's a leak somewhere.
So C++ has long had GC capability, its just not the same approach as Java.
The other classification, is GC that compacts. It's already true there is some magic in the OS and TLB about virtual addresses, a compacting GC is another layer on top of that.
C and C++ have the notion of allocation "arenas." Meaning I can place different objects in memory and have them keep their relative position. Furthermore, I can specify with great detail just how a object is laid out. I can't stress enough how important this is. The vast majority of the Standard library woundnt work if data in memory could be moved. And no one wants it moved– that would confuse the optimizer.

GC normally means non deterministic algorithms that sweep through names to see which ones cannot be reached, and reorders the object placement along the way. But it can mean deterministic routines, such as stacks and reference counts, that preserve location. And in that sense C++ has long had GC capability

Java has automatic garbage collection. Why wasn't similar feature added to C++ in revised versions of language?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s