Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When might you not want to use garbage collection? [closed]

Garbage collection has been around since the early days of LISP, and now - several decades on - most modern programming languages utilize it.

Assuming that you're using one of these languages, what reasons would you have to not use garbage collection, and instead manually manage the memory allocations in some way?

Have you ever had to do this?

Please give solid examples if possible.

like image 564
Jan Carlsson Avatar asked Jan 02 '09 19:01

Jan Carlsson


People also ask

When should you not use garbage collection?

That said, using Garbage Collection can use more memory than is strictly needed, so in severely memory constrained areas where one can not even spare the memory for managing the GC routines (and the code), then that's a good reason not to use it, too.

What is the problem with garbage collection?

Issue: CPU Usage During a Garbage Collection Is Too High An increased allocation rate of objects on the managed heap causes garbage collection to occur more frequently. Decreasing the allocation rate reduces the frequency of garbage collections.

Are there any disadvantages of garbage collection?

The downside of garbage collection is that it has a negative impact on performance. GC has to regularly run though the program, checking object references and cleaning out memory. This takes up resources and often requires the program to pause.

Why do you need garbage collection?

Garbage collection ensures that a program does not exceed its memory quota or reach a point that it can no longer function. It also frees up developers from having to manually manage a program's memory, which, in turn, reduces the potential for memory-related bugs.


2 Answers

I can think of a few:

Deterministic deallocation/cleanup

Real time systems

Not giving up half the memory or processor time - depending on the algorithm

Faster memory alloc/dealloc and application-specific allocation, deallocation and management of memory. Basically writing your own memory stuff - typically for performance sensitive apps. This can be done where the behavior of the application is fairly well understood. For general purpose GC (like for Java and C#) this is not possible.

EDIT

That said, GC has certainly been good for much of the community. It allows us to focus more on the problem domain rather than nifty programming tricks or patterns. I'm still an "unmanaged" C++ developer though. Good practices and tools help in that case.

like image 56
Tim Avatar answered Sep 29 '22 16:09

Tim


Memory allocations? No, I think the GC is better at it than I am.

But scarce resource allocations, like file handles, database connections, etc.? I write the code to close those when I'm done. GC won't do that for you.

like image 43
duffymo Avatar answered Sep 29 '22 17:09

duffymo