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
Weak reference
(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!
==Garbage collection== {{main|Garbage collection (computer science)}} Garbage collection is used to clean up unused objects and so reduce the potential for [[memory leak]]s and data corruption. There are two main types of garbage collection: tracing and [[reference counting]]. Reference counting schemes record the number of references to a given object and collect the object when the reference count becomes zero. Reference-counting cannot collect cyclic (or circular) references because only one object may be collected at a time. Groups of mutually referencing objects which are not directly referenced by other objects and are unreachable can thus become permanently resident; if an application continually generates such unreachable groups of unreachable objects this will have the effect of a [[memory leak]]. Weak references (references which are not counted in reference counting) may be used to solve the problem of circular references if the reference cycles are avoided by using weak references for some of the references within the group. A very common case of such strong vs. weak reference distinctions is in tree structures, such as the [[Document Object Model]] (DOM), where parent-to-child references are strong, but child-to-parent references are weak. For example, Apple's [[Cocoa (API)|Cocoa]] framework recommends this approach.<ref>{{cite web|url=https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html|title=Practical Memory Management|website=developer.apple.com}}</ref> Indeed, even when the object graph is not a tree, a tree structure can often be imposed by the notion of object ownership, where ownership relationships are strong and form a tree, and non-ownership relationships are weak and not needed to form the tree β this approach is common in [[C++]] (pre-C++11), using raw pointers as weak references. This approach, however, has the downside of not allowing the ability to detect when a parent branch has been removed and deleted. Since the [[C++11]] standard, a solution was added by using [[Smart_pointer#shared_ptr_and_weak_ptr|shared_ptr and weak_ptr]], inherited from the [[Boost (C++ libraries)|Boost]] library. Weak references are also used to minimize the number of unnecessary objects in memory by allowing the program to indicate which objects are of minor importance by only weakly referencing them.{{citation needed|date=February 2023}}
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)