views:

427

answers:

8

I don't understand garbage collection so good, then I want to know, why it's so important to a language and to the developer?

A: 

Automatic garbage collection, like java, reuses memory leaks or memory that is no longer being used, making your program efficient. In c++, you have to control the memory yourself, and if you lose access to a memory, then that memory can no longer be used, wasting space. This is what I know so far from one year of computer science and using/learning java and c++.

Raptrex
+5  A: 

Really, this is a question that Wikipedia will perfectly answer for you... There's even a "Benefits" section in the article... Googling isn't that hard after all...

http://en.wikipedia.org/wiki/Garbage%5FCollection

x3ro
+1 for the link, but be kind to the newbies. one of the goals of this site is to allow developers to ask ANY development-related question, so long as it's within certain parameters. (looking for the post to support this)
David Stratton
+1 And besides - without garbage collection your city will get stinky...
Aaron
@David Stratton: Yeah, you may be right, but then, with more then 2k rep, you should already know when it is more wise to use Google to answer a certain question. Garbage collection is a very complex subject, so he couldn't really expect to get a detailed answer here, could he?
x3ro
I see your point. Still, I've seen a few answers where one of the experienced developers on the site is able to sum up a very complex subject in a very elegant short answer. Not enough to fully understand the subject in depth, but enough information to grasp the essence, along with a good link or two for further learning. I think that this type of answer is the true value of this site, so a part of me can't blame @Nathan Campos for asking this one. You're right in that you can't give a good in depth answer to this type of question, but sometimes you get lucky.
David Stratton
Personally, when I see questions like this, the type that can be so easily googled/wiki'd, I think they are just asking questions to get rep
ACBurk
A: 

Garbage collection is important for many reasons a couple of which are:

  1. It prevents memory leaks
  2. It allows you to determine when an object is to be removed from memory.
    • Java and C# handle this automatically which is nice.

Beyond that it would take some good research to get around these finer points.

Woot4Moo
Even with automatic garbage collection there could be memory leak, but chances are less.
fastcodejava
Yes that's true, but in any real world scenario you should be using a strong set of tools to determine memory leaks.
Woot4Moo
@fastcodejava Agree, almost nothing can fully prevent memory leaks.
Chris Ballance
+5  A: 

Garbage Collection is a form of automatic memory management. It is a special case of resource management, in which the limited resource being managed is memory.

Benefits for the programmer is that garbage collection frees the programmer from manually dealing with memory allocation and deallocation.

QuBaR
+1 for this correct answer
Pascal Thivent
+4  A: 

The bottom line is that garbage collection helps to prevent memory leaks. In .NET, for example, when nothing references an object, the resources used by the object are flagged to be garbage collected. In unmanaged languages, like C and C++, it was up to the developer to take care of cleaning up.

It's important to note, however, that garbage collection isn't perfect. Check out this article on a problem that occurred because the developers weren't aware of a large memory leak.

senfo
GC does help **memory management** (you don't have to allocate memory and free it yourself) but doesn't prevent memory leaks.
Pascal Thivent
That's what I tried to infer by making "helps" bold, in addition to linking to a real-world example of where it doesn't always work, as expected.
senfo
+1  A: 

Garbage collection is one of the features required to allow the automatic management of memory allocation. This is what allows you to allocate various objects, maybe introduce other variables referencing or containing these in a fashion or other, and yet never worry about disposing of the object (when it is effectively not in use anymore).

The garbage collection, specifically takes care of "cleaning up" the heap(s) where all these objects are found, by removing unused objects an repacking the others together.

You probably hear a lot about it, because this is a critical function, which happens asynchronously with the program and which, if not handled efficiently can produce some random performance lagging in the program, etc. etc. Nowadays, however the algorithms related to the memory management at-large and the GC (garbage collection) in particular are quite efficient.

Another reason why the GC is sometimes mentioned is in relation to the destructor of some particular object. Since the application has no (or little) control over when particular objects are Garbage-Collected (hence destroyed), it may be an issue if an object waits till its destructor to dispose of some resource and such. That is why many objects implement a Dispose() method, which allow much of that clean-up (of the object itself) to be performed explicitly, rather than be postponed till the destructor is eventually called from the GC logic.

mjv
+3  A: 

In many older and less strict languages deallocating memory was hard-coded into programs by the programmer; this of course will cause problems if not done correctly as the second you reference memory that hasn't been deallocated your program will break. To combat this garbage collection was created, to automatically deallocate memory that was no longer being used. The benefits of such a system is easy to see; programs become far more reliable, deallocating memory is effectively removed from the design process, debugging and testing times are far shorter and more.

Of course, you don't get something for nothing. What you lose is performance, and sometimes you'll notice irregular behaviour within your programs, although nowadays with more modern languages this rarely is the case. This is the reason many typical applications are written in Java, it's quick and simple to write without the trauma of chasing memory leaks and it does the job, it's perfect for the world of business and the performance costs are little with the speed of computers today. Obviously some industries need to manage their own memory within their programs (the Games industry) for performance reasons, which is why nearly all major games are written in C++. A lecturer once told me that if every software house was in the same area, with a bar in the middle you'd be able to tell the game developers apart from the rest because they'd be the ones drinking heavily long into the night.

EnderMB
+1. Nice consise answer.
David Stratton
+5  A: 

Garbage Collection is a part of many modern languages that attempts to abstract the disposal and reallocation of memory with less direction intervention by the developer.

When you hear talk of "safe" objects, this usually refers to something whose memory can be automatically reallocated by the Garbage Collector after an object falls out of scope, or is explicitly disposed.

While you can write the same program without a garbage collector to help manage memory usage, abstracting this away lets the developer think about more high level things and deliver value to the end user more quickly and efficiently without having to necessarily concentrate as much on lower level portions of the program.

In essence the developer can say

Give me a new object

..and some time later when the object is no longer being used (falls out of scope) the developer does not have to remember to say

throw this object away

Developers are lazy (a good virtue) and sometimes forget things. When working with GC properly, it's okay to forget to take out the trash, the GC won't let it pile up and start to smell.

Chris Ballance