Skip to main content

在 PixiJS 中管理垃圾回收

¥Managing Garbage Collection in PixiJS

高效的资源管理对于在任何 PixiJS 应用中保持最佳性能都至关重要。本指南探讨了 PixiJS 如何处理垃圾回收、它提供的工具以及有效管理 GPU 资源的最佳实践。

¥Efficient resource management is crucial for maintaining optimal performance in any PixiJS application. This guide explores how PixiJS handles garbage collection, the tools it provides, and best practices for managing GPU resources effectively.

使用 destroy 进行显式资源管理

¥Explicit Resource Management with destroy

PixiJS 对象(例如纹理、网格和其他 GPU 支持的数据)会保存占用内存的引用。要明确释放这些资源,请在不再需要的对象上调用 destroy 方法。例如:

¥PixiJS 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);
// Use the sprite in your application

// When no longer needed
sprite.destroy();

调用 destroy 可确保立即释放对象的 GPU 资源,从而降低内存泄漏的可能性并提高性能。

¥Calling destroy ensures that the object’s GPU resources are freed immediately, reducing the likelihood of memory leaks and improving performance.

使用 texture.unload 管理纹理

¥Managing Textures with texture.unload

如果 PixiJS 的自动纹理垃圾收集功能不足,你可以使用 texture.unload() 手动从 GPU 卸载纹理:

¥In cases where PixiJS’s automatic texture garbage collection is insufficient, you can manually unload textures from the GPU using texture.unload():

import { Texture } from 'pixi.js';

const texture = Texture.from('image.png');

// Use the texture

// When no longer needed
texture.unload();

这对于动态加载大量纹理并需要精确内存控制的应用尤其有用。

¥This is particularly useful for applications that dynamically load large numbers of textures and require precise memory control.

使用 TextureGCSystem 自动进行纹理垃圾回收

¥Automatic Texture Garbage Collection with TextureGCSystem

PixiJS 还包含 TextureGCSystem,这是一个管理 GPU 纹理内存的系统。默认情况下:

¥PixiJS also includes the TextureGCSystem, a system that manages GPU texture memory. By default:

  • 移除 3600 帧(约 1 小时,60 FPS)内未使用的纹理。

    ¥Removes textures unused for 3600 frames (approximately 1 hour at 60 FPS).

  • 每 600 帧检查一次未使用的纹理。

    ¥Checks every 600 frames for unused textures.

自定义 TextureGCSystem

¥Customizing TextureGCSystem

你可以调整 TextureGCSystem 的行为以适合你的应用:

¥You can adjust the behavior of TextureGCSystem to suit your application:

  • textureGCActive:启用或禁用垃圾回收。默认:true

    ¥textureGCActive: Enable or disable garbage collection. Default: true.

  • textureGCMaxIdle:纹理清理前的最大空闲帧数。默认:3600 帧。

    ¥textureGCMaxIdle: Maximum idle frames before texture cleanup. Default: 3600 frames.

  • textureGCCheckCountMax:垃圾回收检查频率(以帧为单位)。默认:600 帧。

    ¥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
})

垃圾回收最佳实践 PixiJS

¥Best Practices for Garbage Collection in PixiJS

  1. 明确销毁对象:始终对不再需要的对象调用 destroy,以确保及时释放 GPU 资源。

    ¥Explicitly Destroy Objects: Always call destroy on objects you no longer need to ensure GPU resources are promptly released.

  2. 使用池化:使用池化系统重用对象,以减少分配和释放开销。

    ¥Use Pooling: Reuse objects with a pooling system to reduce allocation and deallocation overhead.

  3. 主动管理纹理:必要时使用 texture.unload() 进行手动内存管理。

    ¥Proactively Manage Textures: Use texture.unload() for manual memory management when necessary.

通过遵循这些实践并了解 PixiJS 的垃圾收集机制,你可以创建高效利用系统资源的高性能应用。

¥By following these practices and understanding PixiJS’s garbage collection mechanisms, you can create high-performance applications that efficiently utilize system resources.