Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Will the GC take care of all memory management issues (memory leaks) ?

Is there any case where you don't want the GC to take control of some part of your code ?

share|improve this question
It can't collect itself, even though it should. – rightfold Oct 27 '11 at 10:16
up vote 10 down vote accepted

It will take care of the classical memory management issues (allocated memory not getting deacllocated when no longer in use), though it is still possible to have memory leaks with a GC system - this will be more subtle and will occur when objects still hold references to other objects, even when they no longer need to.

See the question Why can .NET not have memory leaks? and it's answers on StackOverflow.

There may x. situations when you want full control over memory allocation, but frankly, the benefits of a GC system are such that you will normally not need to.

share|improve this answer
The only situations I can think of where the penalties are too high are in systems that move massive amounts of data very quickly through limited memory like high end graphics or systems with very limited memory in the first place, like an embedded controller. – World Engineer Oct 26 '11 at 16:09
@WorldEngineer - True, which is why .NET has pinned objects, allowing you more control over sections of memory. – Oded Oct 26 '11 at 16:42
High Performance Computing, low latency trading systems etc all would love control over their GC :-) – Martijn Verburg Oct 26 '11 at 16:46
Oh, they could tune it just fine. There's even a whole class of high-end GCs dedicated to making pauses tiny and limiting how much time goes into GC (incremental and concurrent GCs), with production-ready canidates. But apparently even that isn't enough for some of them (I can't tell if that's reasonable). So they leave it, along with JIT compilation. – delnan Oct 26 '11 at 16:49
@SamSaffron posted this link on twitter to an article where the explicit behaviour of GC was (ab)used to gain an advantage – Carlo Kuip Oct 26 '11 at 19:09

There are many memory issues (dangling pointers, buffer overruns and similar) and resource issues (memory leaks, other resource leaks). Garbage collector takes care of:

  • dangling pointers
  • mostly memory leaks
  • partially other resource leaks

The language not allowing pointer arithmetic takes care of:

  • buffer overruns and other cases of invalid pointers

Neither can solve other cases of resource problem where you forget to remove reference to the memory/resource from somewhere, so you can still have memory leak with GC (Exception objects are especially notable for referencing many things you are not aware of).

While GC can help you with releasing other resources than memory by use of finalizers (destructors), it will release those resources with non-deterministic delay, which is often not appropriate for resources like files or network sockets. To handle this kind of resource problems you need some language construct for scoped resources like using statement in C#, RAII idiom in C++ or the latest extension to try syntax in Java 7 and it's still only help, because you need to use it in the code.

Not allowing pointer arithmetic and creating addresses from numbers is needed to be able to run precise garbage collector. Some useful optimizations like compacting can only be implemented in precise collectors (you can only move objects if you can tell for sure what is a reference, so you can update it; you can't do that in C/C++).

share|improve this answer

Garbage collection works well for memory. If you have a ton of memory, why run around cleaning up every byte as it is done with? GC typically responds to "memory pressure" and cleans up when needed. This is a good thing for memory. However, if you have an object that holds a non-memory resource (file handle, database connection, lock) waiting until memory pressure triggers GC will generally meaning holding that resource way too long. You need another approach for those resources. That might mean a reference counting shared resource, with deterministic release when the reference count reaches zero, or the using/Dispose approach of .NET.

share|improve this answer

GC systems is a software that attempts to optimize the usage of memory for a running program.

GS system is the responsibility of and is a component of the framework (JVM/.NET).

It can free memory for some objects that it predicts as not going to be needed by an application after a given point in the processing.

In .NET it can only free memory for some .NET native objects and not for MS COM object.

MS COM objects used in .NET must be freed explicitly.


MSDN: Garbage Collection

Wikipedia: Gabage Collection

share|improve this answer
Why demoted-Your feedback is appreciated? – NoChance Oct 27 '11 at 14:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.