Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can garbage collection be faster than manual memory management? [closed]

Tags:

Under what circumstances is garbage collection more efficient than manual memory management? (Here manual could mean using malloc and free as in C, or the cleaner RAII and smart pointer techniques popularized by C++)

I like how garbage collection can remove some accidental complexity from writing software, but I was even more pleased at how RAII and smart pointers can eliminate that complexity while also working on resources other than memory, being deterministic, and providing performance guarantees and being more efficient overall. So I thought I could safely ignore garbage collection. However, I've noticed that people have been saying that garbage collection is faster than the tight resource management used in C++, such as when there is a lot of extra memory available.

So when exactly can garbage collection outperform manual memory management? I like RAII and smart pointers but would happy to accept garbage collection as another tool if it is faster.

like image 335
Jeff Linahan Avatar asked Aug 08 '11 01:08

Jeff Linahan


People also ask

Is manual memory management faster?

With essentially zero optimization effort, the managed versions started off many times faster than the manual. Eventually the manual beat the managed, but only by optimizing to a level that most programmers would not want to go to. In all versions, the memory usage of the manual was significant better than the managed.

How can I make my garbage collection faster?

Short of avoiding garbage collection altogether, there is only one way to make garbage collection faster: ensure that as few objects as possible are reachable during the garbage collection. The fewer objects that are alive, the less there is to be marked. This is the rationale behind the generational heap.

Is garbage collection slow or fast?

Impacts On Performance If it was, every language would use it. GC is slow, mostly because it needs to pause program execution to collect garbage. Think of it like this — your CPU can only work on one thing at a time. With C++, it's always working on your code, including the bits that delete memory.

What are the limitations of garbage collection?

Drawbacks of garbage collection in Java Garbage collectors bring some runtime overhead that is out of the programmer's control. This could lead to performance problems for large applications that scale large numbers of threads or processors, or sockets that consume a large amount of memory.


1 Answers

GC advantages:

  • a GC allocates simply by incrementing a pointer, heap allocators must take counter-measures to avoid heap fragmentation
  • a GC improves cpu cache locality, a big deal on modern processors
  • a GC requires no extra code to release memory, low odds for leaks
  • a generational GG can collect garbage concurrently, making memory management close to free on a multi-core cpu.

GC disadvantages:

  • difficult to make efficient in a language that treats pointers as first class types
  • uses more virtual memory space due to collection latency
  • leaky abstraction for operating system resources other than memory
  • can cause pauses in program operation in some cases while garbage is being collected.

It is a slamdunk for perf, a GC beats a heap allocator easily without effort. The Chinese Dictionary programming contest between Rico Mariani and Raymond Chen is often quoted, overview is here. Raymond's C++ program eventually won but only after rewriting it several times and giving up on the standard C++ library.

like image 54
Hans Passant Avatar answered Sep 17 '22 17:09

Hans Passant