weakref
Weak References
What are they?
Weak references are like sticky notes that you attach to objects in your program. Unlike regular references (which are like strong glue), weak references don't keep objects alive in memory.
Why use them?
Imagine you have a lot of big photos in your computer. You might want to store their names in a list. If you use a regular list, the photos will stay in memory even if you don't use them anymore. This can waste space and slow down your computer.
But if you use a weak reference list, when the last photo is deleted, the weak reference list will automatically remove its name. This frees up space and keeps your computer running smoothly!
How to create weak references:
Weak Dictionaries
What are they?
Weak dictionaries are like regular dictionaries, but they use weak references to their keys or values.
Why use them?
Imagine you have a dictionary of file names and their contents. If you use a regular dictionary, the file objects will stay in memory even if you close them. This can lead to memory leaks and crashes.
But if you use a weak dictionary, when a file is closed, the weak reference will automatically remove its entry from the dictionary. This prevents memory leaks and keeps your program stable.
How to create weak dictionaries:
Weak Sets
What are they?
Weak sets are like regular sets, but they use weak references to their elements.
Why use them?
Imagine you have a set of images. If you use a regular set, the images will stay in memory even if you delete them from your program. This can waste space and slow down your program.
But if you use a weak set, when an image is deleted, the weak reference will automatically remove it from the set. This frees up space and speeds up your program!
How to create weak sets:
Finalize
What is it?
Finalize is a function that allows you to register a cleanup function to be called when an object is deleted.
Why use it?
Imagine you have an object that needs to be closed when it is no longer needed. If you forget to close it, it can cause memory leaks and crashes.
But if you use finalize, you can register a cleanup function that will automatically close the object when it is deleted. This ensures that your program always runs smoothly and without leaks.
How to use finalize:
Real-World Applications
Caching: Weak references can be used to implement caches that automatically remove expired items.
Event handling: Weak references can be used to store event handlers that are automatically removed when the corresponding widget is destroyed.
Object tracking: Weak references can be used to track objects without keeping them alive, making it easier to identify and clean up orphaned objects.
Database connections: Weak references can be used to track database connections that are automatically closed when the owning object is destroyed.
Complete Code Implementations
Weak Reference:
Weak Dictionary:
Weak Set:
Finalize:
Weak References
Explanation: Weak references are a special type of reference in Python that allow an object to be referenced without keeping it alive. This means that the object can be garbage collected even if there are still weak references to it.
Real-World Example: Imagine you have a dictionary that maps the names of people to their addresses. You want to keep track of the addresses, but you don't want to hold on to the actual objects for the people, as you don't need them anymore. You can use weak references to store the addresses without preventing the people objects from being garbage collected.
Code Example:
Subclassing
Explanation: You can create a custom class that inherits from dict
and supports weak references. This allows you to store objects in the dictionary without preventing them from being garbage collected.
Real-World Example: Suppose you have a class that represents a cache of objects. You want to store the cached objects in a way that they can be accessed quickly, but you don't want to hold on to them indefinitely. You can create a custom WeakDict
class that inherits from dict
and supports weak references:
Code Example:
Applications:
Weak references are useful in situations where you want to keep track of objects without preventing them from being garbage collected. Some potential applications include:
Caching: Weak references can be used to create caches that don't hold on to objects indefinitely.
Event handling: Weak references can be used to store event listeners without preventing the objects that generated the events from being garbage collected.
Object tracking: Weak references can be used to track objects without affecting their lifetime.
Weak References in Python
A weak reference is a way to hold a reference to an object without preventing it from being garbage collected.
Creating Weak References
Use the weakref.ref()
function to create a weak reference to an object.
Retrieving the Object
You can retrieve the object from the weak reference if it still exists. Otherwise, it will return None
.
Callbacks
You can specify a callback function to be called when the object is about to be garbage collected.
Hashability and Equality
Weak references are hashable and can be compared for equality. If the referent is still alive, they compare based on the referent. If the referent is gone, they compare based on the reference objects.
Real-World Applications
Caching: Weak references can be used to cache objects without preventing them from being garbage collected.
Events: Weak references can be used to listen for events without holding onto the object that emits the events.
Circular References: Weak references can break circular references, preventing memory leaks.
Example
This example shows how to use a weak reference to cache objects.
Weak References
Imagine you have a piece of paper with a name written on it. If you lose the paper, the name will also be lost. This is like a strong reference.
But what if you have a piece of paper with a name written on it, but you also take a photo of the paper? Even if you lose the paper, you still have the photo, so you can still see the name. This is like a weak reference.
The callback Attribute
With weak references, you can create callbacks, which are functions that get run when the referent (the object being referenced) is no longer alive. This attribute returns the callback currently associated with a weak reference. If there is no callback or if the referent is no longer alive, the attribute will be None
.
Code Example
Real-World Applications
Weak references are useful in situations where you want to track an object but don't want to keep it alive indefinitely. For example:
Event listeners: You can use weak references to attach event listeners to objects without preventing them from being garbage collected.
Object pools: You can use weak references to manage a pool of objects, ensuring that objects are released when they are no longer needed.
Caching: You can use weak references to cache objects, allowing them to be garbage collected when they are no longer used.
Proxy Objects
Proxy objects are a way to access and interact with an object even after it has been garbage collected.
Creating a Proxy Object
To create a proxy object, use the proxy() function. It takes two arguments:
object: The object you want to create a proxy for.
callback: (Optional) A function that will be called when the object is garbage collected.
Accessing Attributes of a Proxy Object
You can access the attributes of a proxy object in the same way you would access the attributes of the original object. However, if the original object has been garbage collected, accessing its attributes will raise a ReferenceError.
Potential Applications
Proxy objects can be useful in situations where you want to access an object that may not exist anymore. For example, you could use a proxy object to hold a reference to a GUI element, even after the GUI element has been closed.
Real-World Example
Here's an example of how you could use a proxy object to hold a reference to a GUI element:
Even though the GUI window has been closed, we can still access the text of the button using the proxy object.
getweakrefcount
The getweakrefcount
function returns the number of weak references and proxies which refer to an object.
How to use it:
Real-world applications:
Object lifetime management: You can use
getweakrefcount
to determine if an object is still being referenced by other objects, even if those references are weak. This can help you to avoid memory leaks.Weak caching: You can use
getweakrefcount
to create a cache that automatically removes entries when they are no longer needed. This can help you to improve performance and reduce memory usage.
getweakrefs(object)
Function
getweakrefs(object)
FunctionWeak references are objects that store references to other objects without preventing those objects from being garbage collected. When you create a weak reference, you're telling Python that you want to keep track of an object, but you don't need to prevent it from being garbage collected. If the object is garbage collected, the weak reference will be automatically destroyed.
The getweakrefs(object)
function returns a list of all weak reference and proxy objects that refer to the given object. This can be useful for debugging purposes, or for finding out what objects are keeping a reference to a particular object.
Real-World Code Implementation and Example
The following code creates a weak reference to an object and then prints the list of weak references that refer to the object:
Output:
Potential Applications in Real World
Weak references can be used for a variety of purposes, including:
Keeping track of objects that may be garbage collected.
Preventing circular references.
Creating caches of objects that can be garbage collected when they are no longer needed.
WeakKeyDictionary
WeakKeyDictionary is a type of dictionary where the keys are not strongly referenced. This means that if the only reference to a key is the one in the dictionary, the key will be garbage collected and the corresponding value will be removed from the dictionary.
This can be useful in situations where you want to associate data with an object without preventing the object from being garbage collected. For example, you could use a WeakKeyDictionary to store metadata about objects in a cache. When the objects are no longer needed, they will be garbage collected and the metadata will be removed from the dictionary.
Code Snippet:
Applications in the Real World
WeakKeyDictionaries have a number of potential applications in the real world, including:
Caching: WeakKeyDictionaries can be used to store metadata about objects in a cache. When the objects are no longer needed, they will be garbage collected and the metadata will be removed from the dictionary.
Event handling: WeakKeyDictionaries can be used to store event handlers for objects. When the objects are no longer needed, the event handlers will be removed from the dictionary.
Object pooling: WeakKeyDictionaries can be used to store objects that are available for reuse. When an object is no longer needed, it will be garbage collected and removed from the dictionary.
Additional Methods:
WeakKeyDictionary objects have an additional method that exposes the internal references directly. This method is called references()
. The references are not guaranteed to be "live" at the time they are used, so the result of calling the references needs to be checked before being used. This can be used to avoid creating references that will cause the garbage collector to keep the keys around longer than needed.
Code Snippet:
Method: WeakKeyDictionary.keyrefs()
Simplified Explanation:
Imagine you have a dictionary where the keys are objects that can be deleted from memory (Python calls them "weak references"). keyrefs()
gives you a list of all the weak references to those keys.
Code Snippet:
Real-World Applications:
Object Tracking: Suppose you want to track objects in a program without holding strong references to them (avoiding memory leaks). You can use a
WeakKeyDictionary
to store weak references to the objects, andkeyrefs()
allows you to iterate through them and check if they are still valid.Cleaning Up Resources: You can use a
WeakKeyDictionary
to store resources that can be released when the keys (which are objects) are deleted. This helps manage resources efficiently and prevent resource leaks.
Code Example (Object Tracking):
Code Example (Cleaning Up Resources):
Note: Weak references can become invalid at any time when the associated object is deleted. Therefore, always check if the references are valid before using them.
WeakValueDictionary
The WeakValueDictionary class is a specialized type of dictionary that uses weak references to store values. Weak references are special objects in Python that do not prevent their target objects from being garbage collected. This means that if the only reference to an object is a weak reference, that object will eventually be deleted by the garbage collector.
Ordinary dictionaries use strong references, which means that the object being referenced will not be garbage collected as long as the dictionary contains a reference to it. This can lead to memory leaks in situations where a dictionary is used to cache objects that are no longer needed, but the dictionary still holds a strong reference to them.
WeakValueDictionary avoids this problem by using weak references for its values. This means that if the only reference to a value is the one in the WeakValueDictionary, the value will be garbage collected when it is no longer needed, and the dictionary will automatically remove the entry.
WeakValueDictionary has the following methods and properties:
clear(): Removes all entries from the dictionary.
copy(): Returns a copy of the dictionary.
get(): Returns the value associated with the specified key, or None if the key is not in the dictionary.
items(): Returns a list of tuples containing the keys and values of the dictionary.
keys(): Returns a list of the keys in the dictionary.
pop(): Removes the entry with the specified key and returns its value, or None if the key is not in the dictionary.
setdefault(): Returns the value associated with the specified key, or creates a new entry with that key and the specified default value if the key is not in the dictionary.
update(): Updates the dictionary with the keys and values from the specified other dictionary.
values(): Returns a list of the values in the dictionary.
Real-world applications
WeakValueDictionary can be useful in any situation where you need to cache objects, but you don't want to worry about memory leaks. For example, you could use a WeakValueDictionary to cache the results of a function call, or to store objects that are only needed temporarily.
Here is a simple example of how to use a WeakValueDictionary:
In this example, we create a WeakValueDictionary and add an object to it. We then get the object from the dictionary and print its value. Finally, we delete the object, which causes it to be garbage collected and the entry to be removed from the dictionary.
WeakValueDictionary
Simplified Explanation:
Imagine you have a dictionary where you store some objects as values. However, these objects are "weak", meaning that they don't prevent the garbage collector from removing them from memory if they're not being used. So, if you hold a reference to a key in the dictionary, it won't keep the corresponding value alive.
valuerefs() Method:
The valuerefs()
method returns an iterable containing weak references to all the values in the dictionary. Weak references are like normal references, but they don't keep the objects they refer to alive in memory.
Code Example:
Output:
Real-World Applications:
WeakValueDictionaries are useful when you want to store values that can be safely garbage collected. For example:
Temporary caching: You can use a WeakValueDictionary to store cached data that doesn't need to be kept alive permanently. If the cached data is no longer needed, the garbage collector will automatically remove it.
Object pooling: You can use a WeakValueDictionary to manage a pool of objects that can be reused. When you're done with an object, you can release it back into the pool. The weak references in the dictionary will prevent the objects from being garbage collected until they're needed again.
WeakSet
WeakSet is a special set type that holds weak references to its elements. Unlike normal sets, elements in a WeakSet will be automatically removed when they are no longer referenced by any other part of the program. This is useful for preventing memory leaks and keeping your code clean.
Here is an example:
Real-World Applications:
Caching: WeakSets can be used to cache objects that are not frequently used. When the object is no longer needed, it will be automatically removed from the cache, freeing up memory.
Managing Event Listeners: WeakSets can be used to store event listeners to avoid memory leaks. When an object is destroyed, its event listeners will also be automatically removed.
Improved Example:
In this example:
MyClass
is a class that manages a WeakSet of event listeners.add_listener
andremove_listener
are methods that add and remove listeners from the WeakSet respectively.listener1
andlistener2
are two simple lambda functions that act as event listeners.When listener1 is removed from the WeakSet, it will be automatically garbage collected, even if there are still references to it in other parts of the program.
WeakMethod
Explanation:
Imagine you have a function called play_music()
that you want to weakly reference. This means that you want to keep track of it, but not hold a strong reference to it that would prevent it from being garbage collected.
WeakMethod comes to the rescue! It's a special kind of "ref" (reference) that allows you to do just that. It's like saying, "Hey, I know about play_music()
, but I'm not going to hold onto it forever."
Code Snippet:
Real-World Application:
WeakMethod is useful when you need to keep track of functions or methods that you may need in the future, but you don't want to prevent them from being garbage collected if they're no longer needed.
For example, in a GUI application, you might want to weakly reference event handlers to avoid keeping them alive even after the window that triggered those events is closed.
Weak References
Definition: A weak reference is a kind of reference that doesn't prevent an object from being garbage collected.
Purpose: Use weak references when you want an object to be accessible only if it still exists.
How it works: A weak reference stores a weak pointer to the object. When the object is garbage collected, the weak pointer becomes invalid and the weak reference can no longer be used to access the object.
Weak Methods
Definition: A weak method is a special type of weak reference that stores a reference to a method bound to an object.
Purpose: Use a weak method when you want to access a method of an object only if the object still exists.
How it works: A weak method stores a weak pointer to the bound method. When the object is garbage collected, the weak pointer becomes invalid and the weak method can no longer be used to call the method.
Real-World Example:
Caching: Suppose you have a function that takes a long time to run. You can use a weak method to cache the result of the function. If the object that the function was called on is garbage collected, the weak method will become invalid and the cache will automatically be cleared.
Code Example:
Potential Applications:
Caching
Event handlers
Callbacks
Debugging
Weak references allow you to keep track of objects without preventing them from being garbage collected.
In Python, objects can be referenced by multiple variables. When an object is no longer referenced by any variable, it is garbage collected and its memory is reclaimed.
Weak references are a type of reference that does not prevent an object from being garbage collected. This means that you can keep track of an object without worrying about keeping it alive.
Creating a weak reference
To create a weak reference, you can use the weakref.ref()
function. This function takes an object as an argument and returns a weak reference to that object.
Using a weak reference
You can use a weak reference to get the object it refers to, or to check if the object has been garbage collected.
Weak methods
Weak methods are a type of weak reference that refers to a method of an object. This means that you can keep track of a method without worrying about keeping the object alive.
To create a weak method, you can use the weakref.WeakMethod()
function. This function takes an object and a method name as arguments and returns a weak method to that method.
Using a weak method
You can use a weak method to call the method it refers to, or to check if the object has been garbage collected.
Real-world applications
Weak references and weak methods can be used in a variety of real-world applications. Here are a few examples:
Event listeners: Weak references can be used to create event listeners that do not prevent the objects they listen to from being garbage collected.
Callbacks: Weak methods can be used to create callbacks that do not prevent the objects they are called from being garbage collected.
Caching: Weak references can be used to create caches that do not prevent the objects they cache from being garbage collected.
Conclusion
Weak references and weak methods are a powerful tool for managing objects in Python. They allow you to keep track of objects without preventing them from being garbage collected. This can be useful in a variety of applications, such as event listeners, callbacks, and caching.
Finalizers
Explanation:
Imagine you created an object called "my_object" and gave it to someone to use. But after a while, you realized you needed it back. Normally, the person would just return the object to you, right?
With finalizers, it's kind of like that. You can create a "finalizer" that will be notified when the person (in this case, the garbage collector) is done with "my_object" and ready to delete it. Then, your finalizer can step in and do something, like log an event or clean up some resources related to "my_object."
Code Example:
In this example, we create a finalizer that will call the my_finalizer
function when my_object
is deleted.
Real-World Application:
Finalizers can be useful in situations where you need to clean up resources or perform actions when an object is no longer needed. For example, you could use a finalizer to close a file or release a lock.
Other Features:
Alive vs. Dead: A finalizer is "alive" until it's called, and then it becomes "dead."
Exceptions: Finalizer callbacks can raise exceptions, but they won't be propagated to the main program.
Atexit: You can prevent a finalizer from being called when the program exits by setting its
atexit
attribute toFalse
.Interpreter Shutdown: Finalizers won't be called during the later part of the interpreter shutdown when module globals may be replaced with
None
.
What is a weakref?
A weakref is a reference to an object that doesn't prevent the object from being garbage collected. This is in contrast to a strong reference, which keeps the object alive as long as the reference exists.
Why use a weakref?
Weakrefs are useful in situations where you want to keep track of an object without preventing it from being garbage collected. For example, you might use a weakref to store a reference to a widget in a GUI application. If the widget is deleted, the weakref will automatically be set to None, and you won't have to worry about manually removing the reference.
How to create a weakref
You can create a weakref using the weakref.ref()
function. This function takes the object you want to reference as its argument.
How to use a weakref
Once you have created a weakref, you can use it to access the object it references. However, you need to be aware that the object may have been garbage collected, in which case the weakref will be set to None.
Real-world example
One real-world example of using a weakref is in a cache. A cache stores a mapping of keys to values. When a new key is added to the cache, a strong reference is created to the value. However, if the value is no longer needed, the strong reference can be replaced with a weakref. This will allow the value to be garbage collected if it is no longer needed, but it will still be accessible through the cache.
Potential applications
Weakrefs have a variety of potential applications, including:
Caching: As mentioned above, weakrefs can be used to create caches that automatically expire when the cached objects are no longer needed.
Event handling: Weakrefs can be used to store references to event handlers. This allows the event handlers to be garbage collected if they are no longer needed, but it also ensures that they will be called if the event occurs.
Object pooling: Weakrefs can be used to create object pools. This allows objects to be reused, which can improve performance.
Weak References
Imagine a weak reference as a sticky note attached to an object. If the object gets deleted, the sticky note also falls off.
detach() Method
The detach()
method checks if the sticky note (weak reference) is attached to a live object (meaning, the object still exists). If the object is alive, the detach()
method removes the sticky note and returns a tuple with information about the object (name, function that created it, arguments, and keyword arguments). If the object is already deleted, the detach()
method returns None
.
Code Snippet:
Real-World Applications:
Weak references are useful when you want to keep track of objects without preventing them from being garbage collected. For example:
Tracking Objects in a Cache: You can use weak references to keep track of objects stored in a cache. If an object is not needed anymore, the weak reference will disconnect and the object will be garbage collected.
Event Listeners: You can use weak references to attach event listeners to objects. If the object is deleted, the event listener will automatically detach.
Object Pools: Weak references can be used to manage objects in an object pool. When an object is no longer needed, the weak reference will detach and the object will be returned to the pool.
Simplified Explanation:
What is peek()?
peek()
lets you see information about a weak reference if it's still alive. If the reference is dead or has been garbage collected, it returns None
.
What does it return?
A tuple containing the following information:
obj
: The original object that the weak reference points to.func
: A function that the weak reference uses to notify you when the object is destroyed.args
: Any arguments passed to the notification function.kwargs
: Any keyword arguments passed to the notification function.
Real-World Example:
Suppose you have a list of objects:
You can create weak references to these objects and store them in a dictionary:
Later, when you want to check if an object is still alive, you can use peek()
:
Potential Applications (Use Cases):
Event Handling: Weak references can be used to keep track of event listeners that are no longer needed. When the listeners are garbage collected, the weak references disappear, freeing up memory.
Caching: Weak references can be used for caching purposes. If the cached data is no longer needed, the weak references allow the data to be garbage collected without explicitly deleting it.
Attribute: alive
Simplified Explanation:
This attribute tells you whether the object that a weakref
is referencing is still alive. An object is considered alive if it hasn't been garbage collected.
Real-World Example:
Imagine you have a weakref
to a list of numbers. If you want to check if the list still exists and hasn't been removed by the garbage collector, you can use the alive
attribute. If alive
is True
, the list is still there; if it's False
, the list has been garbage collected.
Code Example:
Potential Application:
The alive
attribute is useful when you need to track the existence of an object without holding a strong reference to it. For example, if you want to create a cache that stores weak references to objects, you can use the alive
attribute to clean up the cache when objects have been garbage collected.
What is a Weak Reference?
Imagine you have a friend named Alice and you know where she lives. Now, imagine you only have a piece of paper with her address, but you don't have her phone number or email. If you want to visit Alice, you can use the address to find her, as long as she hasn't moved.
In Python, a weak reference is like the address on that piece of paper. It points to an object, but if the object is deleted, the weak reference becomes useless.
Why Use Weak References?
Weak references are useful when you want to keep track of an object, but you don't want to prevent it from being deleted when it's no longer needed.
For example, if you have a list of objects, you could use weak references to keep track of them without preventing them from being deleted by other parts of your program.
Creating Weak References
To create a weak reference to an object, you can use the weakref.ref
function. Here's an example:
Using Weak References
You can use the weak_ref()
function to access the object pointed to by the weak reference. If the object still exists, it will be returned. Otherwise, None
will be returned.
Applications of Weak References
Caching: Weak references can be used to create a cache that doesn't prevent objects from being deleted.
Event handling: Weak references can be used to create event handlers that don't prevent the objects they're attached to from being deleted.
Circular references: Weak references can be used to break circular references, which occur when two objects refer to each other and prevent each other from being deleted.
Real World Example
Here's an example of how weak references can be used in a real-world application:
In this example, the Cache
class uses a WeakValueDictionary
to store data. The WeakValueDictionary
uses weak references to prevent the stored values from preventing the keys from being deleted. This allows the data to be cached without preventing it from being deleted by other parts of the program.
Weak References
Weak references are a way to refer to an object without preventing it from being garbage collected. This can be useful in situations where you need to keep track of an object, but you don't want to prevent it from being deleted.
Weak references are created using the weakref.ref
class. The weakref.ref
class takes a reference to an object as its argument. This reference can be to any type of object, including instances of classes, lists, dictionaries, and so on.
Once a weak reference has been created, it can be used to access the original object. If the original object has been garbage collected, the weak reference will return None
.
Weak references can be used in a variety of ways. Here are a few examples:
Caching: Weak references can be used to implement a cache that does not prevent the cached objects from being garbage collected. This can be useful in situations where the cached objects are large or expensive to create.
Event handling: Weak references can be used to implement event handlers that do not prevent the event sources from being garbage collected. This can be useful in situations where the event sources are short-lived or frequently created and destroyed.
Object tracking: Weak references can be used to track objects without preventing them from being garbage collected. This can be useful in situations where you need to keep track of objects for debugging or performance monitoring purposes.
Finalizers
Finalizers are a way to register a callback function that will be called when an object is garbage collected. This can be useful in situations where you need to perform some cleanup when an object is deleted.
Finalizers are created using the weakref.finalize
function. The weakref.finalize
function takes a reference to an object and a callback function as its arguments. The callback function will be called when the object is garbage collected.
Once a finalizer has been created, it can be used to access the original object. If the original object has been garbage collected, the finalizer will not be called.
Finalizers can be used in a variety of ways. Here are a few examples:
Resource cleanup: Finalizers can be used to release resources that are associated with an object. This can be useful in situations where the resources are expensive or difficult to release.
Object validation: Finalizers can be used to validate the state of an object before it is garbage collected. This can be useful in situations where the object is being used in a critical context.
Debugging: Finalizers can be used to help debug memory leaks. By registering a finalizer for an object, you can track when the object is garbage collected and help identify any potential memory leaks.
Applications
Here are a few real-world applications for weak references and finalizers:
Caching: Weak references can be used to implement a cache that does not prevent the cached objects from being garbage collected. This can be useful in situations where the cached objects are large or expensive to create. For example, a web browser might use weak references to cache frequently visited pages.
Event handling: Weak references can be used to implement event handlers that do not prevent the event sources from being garbage collected. This can be useful in situations where the event sources are short-lived or frequently created and destroyed. For example, a graphical user interface might use weak references to register event handlers for widgets.
Object tracking: Weak references can be used to track objects without preventing them from being garbage collected. This can be useful in situations where you need to keep track of objects for debugging or performance monitoring purposes. For example, a profiler might use weak references to track the allocation and deallocation of objects.
Resource cleanup: Finalizers can be used to release resources that are associated with an object. This can be useful in situations where the resources are expensive or difficult to release. For example, a database connection might use a finalizer to close the connection when the object is garbage collected.
Object validation: Finalizers can be used to validate the state of an object before it is garbage collected. This can be useful in situations where the object is being used in a critical context. For example, a critical section might use a finalizer to unlock the section when the object is garbage collected.
Examples
Here is a simple example of how to use weak references:
Here is a simple example of how to use finalizers: