Garbage collection (GC) is a form of automatic memory management. The runtime periodically identifies objects that the program can no longer reach and reclaims their memory, so developers do not free it by hand.
Why it matters
GC removes whole categories of bugs, such as use-after-free and double free, and makes code in languages like Java, C#, Go, JavaScript and Python simpler to write. It is not free: collection takes CPU time and can cause short pauses, which matter for latency-sensitive services. Common approaches include tracing collectors that mark reachable objects from roots, generational collectors that treat young and old objects differently, and reference counting, used by Swift and parts of Python.
Example
let cache = { big: new Array(1_000_000).fill(0) };
cache = null; // no references remain
// the engine may now reclaim the array at its next GC cycle
The array becomes unreachable once the only reference is cleared. The engine decides when to actually free it.
Common mistakes
- Assuming GC prevents all leaks; objects still referenced by a cache or listener are never collected.
- Expecting memory to be returned to the operating system immediately after collection.
- Forcing collections manually instead of fixing allocation patterns.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.