Some garbage-collected languages feature or support various levels of weak references, such as C#, Lua, Java, Lisp, OCaml, MATLAB[1], Perl, Python[2] and PHP since the version 7.4.
Garbage collection is used to clean up unused objects and so reduce the potential for memory leaks and data corruption.
This approach, however, has the downside of not allowing the ability to detect when a parent branch has been removed and deleted.
[citation needed] Some languages have multiple levels of weak reference strength.
For example, Java has, in order of decreasing strength, soft, weak, and phantom references, defined in the package java.lang.ref.
On the other hand, phantom references provide a mechanism to notify the program when an object has been freed (notification is implemented using ReferenceQueues).
[6] Some non-garbage-collected languages, such as C++, provide weak/strong reference functionality as part of supporting garbage collection libraries.
Worse yet, it does not allow for detection of whether another strong reference is already tracking a given plain pointer.
Weak references can be useful when keeping a list of the current variables being referenced in the application.
Older versions of Mac OS X, iOS, and GNUstep support only unsafe_unretained references as weak ones.