PixiJS objects like textures and meshes consume GPU memory that JavaScript's garbage collector can't reclaim automatically. This page covers the three ways to manage that memory: calling destroy() explicitly, unloading textures manually, and configuring the automatic texture garbage collector.
destroyPixiJS objects, such as textures, meshes, and other GPU-backed data, hold references that consume memory. To explicitly release these resources, call the destroy method on objects you no longer need. For example:
import { Sprite } from 'pixi.js';
const sprite = new Sprite(texture);
// Destroy the sprite only (texture is preserved for reuse)
sprite.destroy();
// Destroy the sprite AND its texture (frees GPU memory)
sprite.destroy({ children: true, texture: true, textureSource: true });
Calling destroy ensures that the object’s GPU resources are freed immediately, reducing the likelihood of memory leaks and improving performance.
texture.unloadIn cases where PixiJS’s automatic texture garbage collection is insufficient, you can manually unload textures from the GPU using texture.unload():
import { Assets } from 'pixi.js';
const texture = await Assets.load('image.png');
// Use the texture
// When no longer needed
texture.source.unload();
This is particularly useful for applications that dynamically load large numbers of textures and require precise memory control.
TextureGCSystemPixiJS also includes the TextureGCSystem, a system that manages GPU texture memory. By default:
These thresholds are frame-based, not time-based. If your app runs at a lower frame rate, textures will persist longer before being collected.
TextureGCSystemYou can adjust the behavior of TextureGCSystem to suit your application:
textureGCActive: Enable or disable garbage collection. Default: true.textureGCMaxIdle: Maximum idle frames before texture cleanup. Default: 3600 frames.textureGCCheckCountMax: Frequency of garbage collection checks (in frames). Default: 600 frames.Example configuration:
import { Application } from 'pixi.js';
const app = new Application();
await app.init({
textureGCActive: true, // Enable texture garbage collection
textureGCMaxIdle: 7200, // 2 hours idle time
textureGCCheckCountMax: 1200, // Check every 20 seconds at 60 FPS
});
destroy() on objects you no longer need. Pass { texture: true, textureSource: true } if the texture won't be reused.Assets.unload() for loaded assets: If you loaded a texture via Assets.load('image.png'), use Assets.unload('image.png') to release it. This removes it from the cache and unloads the GPU resource.For more optimization strategies, see Performance Tips.