Short answer
Garbage collection is automatic reclamation of memory the program can no longer reach. JavaScript collects objects that are not reachable from roots. Java's HotSpot guide says the collector finds memory still in use and reclaims the rest. .NET releases objects unreachable from roots. Python's gc module supplements reference counting.
Last verified against the sources listed below.
Key takeaways
- MDN says JavaScript allocates memory for values and later frees memory that is no longer used.
- Modern JavaScript engines use mark-and-sweep: they keep objects reachable from roots and collect the rest.
- Python's gc module supplements reference counting and can be disabled with gc.disable.
- HotSpot's guide says the collector allocates memory, finds what is still in use, and reclaims the rest.
- .NET's collector reclaims objects that are unreachable from roots such as stacks and static fields.
What is garbage collection in programming is the automatic reclaiming of memory the program can no longer reach. MDN’s JavaScript memory guide says high-level languages allocate memory when objects are created and free it when the objects are not used anymore. The purpose of a collector is to decide when a block is no longer needed and reclaim it.
The same page is honest about the limit. Deciding whether a piece of memory is still needed, in full generality, is undecidable. Collectors use a narrower test. In the mark-and-sweep account, that test is reachability: an object the program cannot reach is collected.
A programming paradigm does not remove this problem. Manual languages ask the programmer to free memory. The runtimes below do that work themselves, and they do not do it in identical ways.
What does JavaScript collect?
JavaScript collects memory that is no longer reachable. MDN says allocation happens when you declare values, create objects, arrays, and functions, or call some constructors such as Date.
The guide describes two algorithms. Reference counting treats an object as garbage when nothing points at it. MDN notes that no modern JavaScript engine uses reference counting for collection anymore, because a cycle of objects can keep a count above zero after the objects are unused.
Mark-and-sweep starts from roots. In JavaScript the root is the global object. The collector marks everything reachable from those roots and collects the rest.
Cycles are not a problem, because a cycle that nothing reachable points at is still unreachable.
MDN says all modern engines ship a mark-and-sweep collector. Later work, such as generational or concurrent collection, improves that algorithm’s implementation. You cannot trigger collection from the language itself. You make an object unreachable by dropping the references you hold.
JavaScript is one runtime with this model. WeakMap and WeakSet hold keys weakly, so a key can be collected if nothing else points at it. MDN says WeakRef and FinalizationRegistry look directly at the collector and should be avoided when a normal reference will do.
What does Python collect?
Python’s gc module collects unreachable objects that reference counting leaves behind. The gc documentation calls the collector optional and says it supplements reference counting. If you know the program creates no reference cycles, you may call gc.disable().
gc.collect() with no arguments runs a full collection and returns the sum of collected and uncollectable objects. You can pass a generation from 0 to 2. Objects are placed in generations.
New objects start in generation 0. Survivors move to an older generation. Generation 2 is the oldest, and objects that survive there stay there.
Thresholds decide when a collection starts. Setting threshold0 to zero disables collection.
The module can list objects it tracks, report stats, and keep a gc.garbage list of unreachable objects it could not free. Since Python 3.4, that list should be empty most of the time, except for some C extension types. Python programs still drop references. The collector’s extra job is the cycle.
How does Java do it?
Java’s HotSpot collectors reclaim memory the application is not still using. Oracle’s tuning introduction says a garbage collector automatically manages dynamic memory requests. It takes memory from the operating system and returns memory, hands memory to the application, finds which parts are still in use, and reclaims the unused parts.
The guide says this frees the developer from matching every allocation with a deallocation. The cost is runtime overhead. HotSpot provides more than one collector.
The serial collector is described as usually adequate for most small applications, in particular heaps up to about 100 megabytes on modern processors. On server-class machines the Garbage-First collector is selected by default. The page is about the Java platform.
It is not a description of the .NET runtime.
What does .NET collect?
The .NET collector releases objects that are unreachable from the application’s roots. Microsoft’s fundamentals page says the garbage collector in the common language runtime is an automatic memory manager. Developers in managed code do not write the free themselves.
Roots include static fields, local variables on a thread’s stack, CPU registers, GC handles, and the finalize queue. The collector builds a graph of objects reachable from those roots. Objects not in the graph are garbage.
Their memory is released, and reachable objects may be compacted so later allocations stay contiguous. Objects of 85,000 bytes and larger are allocated on the large object heap, which is usually not compacted.
The managed heap is split into generations 0, 1, and 2. New objects start in generation 0. Survivors are promoted.
A generation 2 collection is a full collection because it includes the younger generations. This is a different runtime from HotSpot, even though both are called garbage collection.
How do the four docs compare?
Each document names what its collector takes back. The wording is theirs.
| Runtime | What the doc says it collects |
|---|---|
| JavaScript (MDN) | Memory that is no longer needed; modern engines collect objects that are not reachable from roots |
Python (gc) |
Unreachable objects the optional collector finds, supplementing reference counting, including cycles |
| Java HotSpot (Oracle) | Unused memory, after the collector determines which parts the application is still using |
| .NET (Microsoft) | Objects that are not reachable from the application’s roots, on the managed heap |
A word counter can count the words in a paragraph about these four rows. It does not allocate a managed heap or run gc.collect().
To reason about a leak, use the test the runtime’s own page gives you: a JavaScript object still reachable from a root, a Python cycle that reference counting cannot break, a Java object the collector still treats as in use, or a .NET object still on the root graph. ---
Frequently asked questions
Can you turn JavaScript garbage collection off?
Not from ordinary page code. MDN says you cannot programmatically trigger collection in JavaScript, and that this is unlikely to join the language, though engines may offer flags. Node can be started with an expose-gc flag for debugging. The practical step is to drop references so the object is unreachable. MDN also says to avoid WeakRef when a normal reference will do, because timing is not guaranteed.
Why does Python have a gc module if it counts references?
The gc page says the collector supplements reference counting. You can call gc.disable if you know the program creates no reference cycles. gc.collect with no arguments runs a full collection. You may pass a generation from 0 to 2. An invalid number raises ValueError. Reference counting does not reclaim a cycle of objects that still point at each other. That cycle is the collector's extra job.
What does the Java collector actually do?
Oracle's HotSpot guide says the collector manages dynamic memory requests. It takes memory from the operating system and returns memory, hands memory to the application, finds which parts are still in use, and reclaims the unused parts. The serial collector is described as enough for most small applications. On server-class machines, Garbage-First is the default. That page is about Java, not about .NET.
When does the .NET collector run?
Microsoft lists three conditions. Physical memory is low. Memory used on the managed heap passes a threshold the runtime adjusts. Or code calls GC.Collect, which the page says you almost never need. During a collection, objects reachable from roots stay. Objects outside that graph are released. Objects of 85000 bytes and larger go on a separate heap that is usually not compacted.
Do these collectors free files and sockets too?
Not by themselves. The .NET page says a file handle, window handle, or network connection needs explicit cleanup. The collector can track the managed wrapper, but it does not know how to close the resource. The page recommends a public Dispose method, plus a finalizer or safe handle if the caller forgets. Dropping a JavaScript reference makes memory eligible. It does not close a file.
Sources
- Memory management · MDN, 2026
- gc: Garbage Collector interface · Python Software Foundation, 2026
- Introduction to Garbage Collection Tuning · Oracle, 2026
- Fundamentals of garbage collection · Microsoft, 2026