Skip to main content
125 votes

How does garbage collection work in languages which are natively compiled?

Does the compiler store a copy of some garbage collection program and paste it into each executable it generates? It sounds unelegant and weird, but yes. The compiler has an entire utility library, ...
Kilian Foth's user avatar
104 votes
Accepted

What happens to garbage in C++?

The programmer is responsible for ensuring that objects they created via new are deleted via delete. If an object is created, but not destroyed before the last pointer or reference to it goes out of ...
87 votes

Why aren't Java objects deleted immediately after they are no longer referenced?

Because properly knowing something is no longer referenced isn't easy. Not even close to easy. What if you have two objects referencing each other? Do they stay forever? Extending that line of ...
whatsisname's user avatar
  • 27.7k
82 votes

What happens to garbage in C++?

C++ does not have garbage collection. C++ applications are required to dispose of their own garbage. C++ applications programmers are required to understand this. When they forget, the result is ...
John R. Strohm's user avatar
79 votes
Accepted

Why aren't Java objects deleted immediately after they are no longer referenced?

First of all, Java has weak references and another best-effort category called soft references. Weak vs. strong references is a completely separate issue from reference counting vs. garbage ...
Karl Bielefeldt's user avatar
60 votes

How does garbage collection work in languages which are natively compiled?

Or does the compiler include some minimal garbage collector in the compiled program's code. That’s an odd way of saying “the compiler links the program with a library that performs garbage collection”...
Konrad Rudolph's user avatar
60 votes
Accepted

Does Garbage Collection Scan The Entire Memory?

I was reading a bit about garbage collectors and I am wondering if the garbage collector of a program scans the entire heap memory or what is allocated to it? That depends on the garbage collector. ...
Jörg W Mittag's user avatar
57 votes
Accepted

How does garbage collection work in languages which are natively compiled?

Garbage collection in a compiled language works the same way as in an interpreted language. Languages like Go use tracing garbage collectors even though their code is usually compiled to machine code ...
avdgrinten's user avatar
45 votes

Why aren't Java objects deleted immediately after they are no longer referenced?

AFAIK, the JVM specification (written in English) does not mention when exactly an object (or a value) should be deleted, and leaves that to the implementation (likewise for R5RS). It somehow requires ...
Basile Starynkevitch's user avatar
44 votes

What happens to garbage in C++?

In C, C++ and other systems without a Garbage Collector, the developer is offered facilities by the language and its libraries to indicate when memory can be reclaimed. The most basic facility is ...
Matthieu M.'s user avatar
27 votes

What happens to garbage in C++?

C++ has this thing called RAII. Basically it means garbage gets cleaned up as you go rather than leave it in a pile and let the cleaner tidy up after you. (imagine me in my room watching the football -...
gbjbaanb's user avatar
  • 48.6k
26 votes

What happens to garbage in C++?

It should be noted that it is, in the case of C++, a common misconception that "you need to do manual memory management". In fact, you don't usually do any memory management in your code. Fixed-size ...
Felix Dombek's user avatar
  • 2,119
23 votes

How does garbage collection work in languages which are natively compiled?

How would this work with compiled languages though? Your wording is wrong. A programming language is a specification written in some technical report (for a good example, see R5RS). Actually you are ...
Basile Starynkevitch's user avatar
23 votes

Why aren't Java objects deleted immediately after they are no longer referenced?

To use Objective-C terminology, all Java references are inherently "strong". That's not correct - Java does have both weak and soft references, though these are implemented at the object level rather ...
berry120's user avatar
  • 1,747
18 votes
Accepted

C++ delete vs Java GC

The C++ object lifecycle If you create local objects, you don't need to delete them: the compiler generates code to delete them automatically when the object goes out of scope If you use object ...
Christophe's user avatar
  • 78.4k
15 votes

A reference counting pattern for memory managed languages?

In general, you avoid it by having a single owner - even in unmanaged languages. But the principle is the same for managed languages. Instead of immediately closing the expensive resource on a Close()...
Telastyn's user avatar
  • 109k
14 votes

A reference counting pattern for memory managed languages?

In a garbage collected language (where GC is not deterministic), it is not possible to reliably tie the cleanup of a resource other than memory to the lifetime of an object: It is not possible to ...
amon's user avatar
  • 135k
14 votes

Does Garbage Collection Scan The Entire Memory?

There are a lot of details in garbage collection. Lots lots lots lots lots. Each one of them has different behaviors, so it's hard to cover everything. Garbage collectors almost universally scan the ...
Cort Ammon's user avatar
  • 11.6k
10 votes
Accepted

Managed code: could GC be taken care of during compile time?

The compiler would analyse the code and insert the destruct methods into the appropriate places in the object code. The answer to this is the same as the answer to the second paragraph of this ...
Jörg W Mittag's user avatar
10 votes
Accepted

Mark&sweep GC with automatic freeing resources

“100% deterministically, once they are no longer used they should be freed right away” – that is incompatible with garbage collection. But it's not necessarily at odds with automatic resource ...
amon's user avatar
  • 135k
10 votes
Accepted

Why is it difficult to create a truly portable garbage collector for C?

Garbage collection requires the possibility to find objects in memory that are no longer used. One way of doing it is to have a marking algorithm that goes through all the active references. Another ...
Christophe's user avatar
  • 78.4k
10 votes

Does Garbage Collection Scan The Entire Memory?

No garbage collector scans the entire memory. Because, when reading some random memory content, it is not possible to guess if it's used or not. So garbage collection goes through objects or list of ...
Christophe's user avatar
  • 78.4k
9 votes

How often can we use garbarge collection in C#

In general, asking the garbage collector to collect is more of a recommendation than an actual order. You should not do this at all. The garbage collector will properly respond to memory pressure. ...
Telastyn's user avatar
  • 109k
9 votes
Accepted

How a Garbage Collector follows pointers to discover live objects

I'm sure Erik's answer has lots of great info but I think you are stuck on the fundamentals. I'm more familiar with Java GC but the basic principles are pretty universal. I understand the rest of ...
JimmyJames's user avatar
  • 27.6k
9 votes

Does Garbage Collection Scan The Entire Memory?

Top-level, GC is specific to the application. It doesn't really make much sense for a Python application to be scanning a Java application's memory space, even if it was allowed to do so. Also, not ...
JimmyJames's user avatar
  • 27.6k
8 votes

What happens to garbage in C++?

With respect to C specifically, the language gives you no tools to manage dynamically-allocated memory. You are absolutely responsible for making sure every *alloc has a corresponding free somewhere. ...
John Bode's user avatar
  • 10.9k
8 votes
Accepted

Low latency TCP sockets in .NET

For example, while searching for low-latency open source apps I found this OpenPDC application on Github which seems to be the de-facto standard application for "high-performance data collection" used ...
MaulingMonkey's user avatar
8 votes
Accepted

High Performance Garbage Collection

Different GC algorithms have different tradeoffs. Some optimize for high throughput, others for low latency. There is no single best algorithm, so platforms like the Hotspot JVM are very tunable and ...
amon's user avatar
  • 135k
7 votes
Accepted

Garbage collection virtual machine

For garbage collection we need to know two things: Which memory regions were allocated? Given a memory region, is it reachable? If in your instruction set S must be a literal, then all memory ...
amon's user avatar
  • 135k
6 votes

Explicit call to finalize() method in Java

A finalize() method is a method like any other. It does whatever you program it to do. The only thing special is that it may be called unpredictably by the JVM before an object is garbage collected. ...
Kilian Foth's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible