Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Garbage collection (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Overview == {{More citations needed section|date=July 2014}} Many [[programming language]]s require garbage collection, either as part of the [[language specification]] (e.g., [[RPL (programming language)|RPL]], [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]], [[D (programming language)|D]],<ref name="Mars"/> [[Go (programming language)|Go]], and most [[scripting language]]s) or effectively for practical implementation (e.g., formal languages like [[lambda calculus]]).<ref>{{Cite web |last=Heller |first=Martin |date=2023-02-03 |title=What is garbage collection? Automated memory management for your programs |url=https://www.infoworld.com/article/3685493/what-is-garbage-collection-automated-memory-management-for-your-programs.html |access-date=2024-06-21 |website=InfoWorld |language=en}}</ref> These are said to be ''garbage-collected languages''. Other languages, such as [[C (programming language)|C]] and [[C++]], were designed for use with manual memory management, but have garbage-collected implementations available. Some languages, like [[Ada (programming language)|Ada]], [[Modula-3]], and [[C++/CLI]], allow both garbage collection and [[manual memory management]] to co-exist in the same application by using separate [[Heap (data structure)|heap]]s for collected and manually managed objects. Still others, like [[D (programming language)|D]], are garbage-collected but allow the user to manually delete objects or even disable garbage collection entirely when speed is required.<ref>{{Cite web |date=2020-01-16 |title=A Guide to Garbage Collection in Programming |url=https://www.freecodecamp.org/news/a-guide-to-garbage-collection-in-programming/ |access-date=2024-06-21 |website=freeCodeCamp.org |language=en}}</ref> Although many languages integrate GC into their [[compiler]] and [[runtime system]], ''post-hoc'' GC systems also exist, such as [[Automatic Reference Counting]] (ARC). Some of these ''post-hoc'' GC systems do not require recompilation.<ref>{{Cite web |title=Garbage Collection - D Programming Language |url=https://dlang.org/spec/garbage.html |access-date=2022-10-17 |website=dlang.org}}</ref> === Advantages === {{Unreferenced section|date=April 2021}} GC frees the programmer from manually de-allocating memory. This helps avoid some kinds of [[bug (software)|error]]s:<ref>{{Cite web |title=Garbage Collection |url=https://rebelsky.cs.grinnell.edu/Courses/CS302/99S/Presentations/GC/ |access-date=2024-01-13 |website=rebelsky.cs.grinnell.edu}}</ref> * ''[[Dangling pointer]]s'', which occur when a piece of memory is freed while there are still [[pointer (computer programming)|pointer]]s to it, and one of those pointers is [[dereference operator|dereferenced]]. By then the memory may have been reassigned to another use, with unpredictable results.<ref>{{Cite web |last=Heller |first=Martin |date=2023-02-03 |title=What is garbage collection? Automated memory management for your programs |url=https://www.infoworld.com/article/3685493/what-is-garbage-collection-automated-memory-management-for-your-programs.html |access-date=2024-06-21 |website=InfoWorld |language=en}}</ref> * ''Double free bugs'', which occur when the program tries to free a region of memory that has already been freed, and perhaps already been allocated again. * Certain kinds of ''[[memory leak]]s'', in which a program fails to free memory occupied by objects that have become [[unreachable]], which can lead to memory exhaustion.<ref>{{Cite web|url=https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals|title=Fundamentals of garbage collection {{!}} Microsoft Learn|author=[[Microsoft]]|date=28 February 2023 |access-date=2023-03-29}}</ref> === Disadvantages === GC uses computing resources to decide which memory to free. Therefore, the penalty for the convenience of not annotating object lifetime manually in the source code is [[overhead (computing)|overhead]], which can impair program performance.<ref name="Zorn_1993"/> A peer-reviewed paper from 2005 concluded that GC needs five times the memory to compensate for this overhead and to perform as fast as the same program using idealized explicit memory management. The comparison however is made to a program generated by inserting deallocation calls using an [[oracle machine|oracle]], implemented by collecting traces from programs run under a [[Profiling (computer programming)|profiler]], and the program is only correct for one particular execution of the program.<ref name="Hertz-Berger_2005"/> Interaction with [[memory hierarchy]] effects can make this overhead intolerable in circumstances that are hard to predict or to detect in routine testing. The impact on performance was given by Apple as a reason for not adopting garbage collection in [[iOS]], despite it being the most desired feature.<ref name="wwdc_2011"/> The moment when the garbage is actually collected can be unpredictable, resulting in stalls (pauses to shift/free memory) scattered throughout a [[Session (computer science)|session]]. Unpredictable stalls can be unacceptable in [[real-time computing|real-time environment]]s, in [[transaction processing]], or in interactive programs. Incremental, concurrent, and real-time garbage collectors address these problems, with varying trade-offs.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)