Cause and mechanism
An ObjectPool keeps finished objects, such as bullets or effects, ready for reuse instead of repeatedly destroying and recreating them. This can reduce initialization and allocation work. Destroy removes Unity objects, whereas garbage collection (GC) reclaims unused managed C# memory; check these costs separately.
Check and correct the setup
- Define create/get/release/destroy callbacks for creation, activation, reset, and final disposal.
Reuse the same object - Reset velocity, events, effects, and async work; prevent double returns and use after return.
Reuse the same object - maxSize normally limits retained inactive objects, not all active instances. Prewarm by holding several acquisitions before returning them.
Reuse the same object
Limits and verification
Pools retain memory. Manage active-object ownership at teardown and profile allocation, creation counts, and retained memory.
A focused diagnostic setup
For a pooled projectile, list state retained at get, release and final destruction. Separate reusable storage from state that must not leak to the next consumer.
Test in this order
- Reset position, velocity, owner, lifetime and display on acquisition and compare consecutive uses of one instance.
Reuse the same object - Stop particles/trails, subscriptions and asynchronous work on return; detect double returns and stale consumers.
Reuse the same object - Test demand beyond retained capacity and scene shutdown, handling both active and inactive ownership.
Reuse the same object
Interpret the result
maxSize does not by itself cap active demand. Add an explicit active limit if required and compare startup, reuse behavior and resident memory.
Related explanations
- Garbage Collection and Frame Spikes
- Trail
- Material count grows after Renderer.material access: sharing, copies and cleanup