liveBook · Manning

Weak reference

Also called weak pointers

A weak reference is a non-owning reference that does not prevent its referent from being reclaimed when no strong references remain.

How 3 Manning books explain it

100 Java Mistakes and How to Avoid Them

WeakReference is a Java reference type whose referent may be reclaimed when it is not strongly reachable.

How it is used
Deploys weak references for discardable caches and map keys whose mappings may vanish when no strong references remain.
How deep it goes
Explains reachability categories, garbage-collection timing, failure behavior, WeakHashMap, and reachabilityFence, but not collector internals.
Examples
Uses minimal Java dereference snippets, WeakHashMap mappings, and a reachabilityFence comparison.
What you can do after
Apply it via WeakHashMap or guarded WeakReference access while avoiding assumptions that the referent remains available.

Read how 100 Java Mistakes and How to Avoid Them explains weak reference →

Fast by Default

A weak reference is an object reference that does not keep its referenced object alive.

How it is used
Breaks retain cycles so objects can be released after navigation.
How deep it goes
Explains strong-reference cycles and their practical fix, but does not develop weak-reference implementation syntax.
Examples
Uses mutually referencing objects and a screen retained after navigation to demonstrate the leak and its repair.
What you can do after
Diagnose/debug with memory profiling by identifying retained objects and breaking a strong-reference cycle with a weak reference.

Read how Fast by Default explains weak reference →

100 C++ Mistakes and How to Avoid Them

How it is used
Uses weak references to break circular shared ownership and prevent resource leaks.
How deep it goes
Defines non-ownership and explains conversion before dereferencing, including the nullptr result when conversion fails.
Examples
Uses a doubly linked deque whose nodes hold shared pointers to previous and next nodes.
What you can do after
Apply std::weak_ptr to break circular ownership and convert it before dereferencing.

Read how 100 C++ Mistakes and How to Avoid Them explains weak reference →

Also covered in

What to know first

Related concepts

Questions about weak reference

What is weak reference?

A weak reference is a non-owning reference that does not prevent its referent from being reclaimed when no strong references remain.

Which Manning books explain weak reference?

100 Java Mistakes and How to Avoid Them, Fast by Default and 100 C++ Mistakes and How to Avoid Them.

What should I know before learning weak reference?

Manning books that teach weak reference usually introduce garbage collection, primitive value and Map first.

What is weak reference a kind of?

Weak reference is a kind of reference. A reference is an indirect value, binding, or relationship that identifies or provides access to another object, value, resource, or source.

What is weak reference also called?

Weak reference is also called weak pointers.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.