MeshPool

ceramic.MeshPool (Class)

A global object pool for efficiently reusing Mesh instances and their arrays.

MeshPool provides a memory-efficient way to manage Mesh objects by recycling them instead of creating new instances. This reduces garbage collection pressure and improves performance in scenarios with frequent mesh creation/destruction.

The pool also manages arrays used by meshes (vertices, indices, colors, uvs) to further optimize memory usage.

Key features:

  • Mesh instance recycling with automatic cleanup
  • Array buffer recycling for vertices, indices, colors, and UVs
  • Thread-safe array clearing on native platforms
  • Debug tracking for allocation and recycling
// Get a mesh from pool (creates new if pool is empty)
var mesh = MeshPool.get();
mesh.createQuad(100, 100);
mesh.texture = myTexture;
parent.add(mesh);

// When done, recycle the mesh back to pool
MeshPool.recycle(mesh);
// The mesh is automatically cleaned up and ready for reuse
See: Mesh The mesh class being pooled

Static Members

get(): Mesh

Gets a mesh from the pool or creates a new one if the pool is empty.

The returned mesh is:

  • Active and ready to be displayed
  • Reset to default values (visible, touchable)
  • Provided with empty arrays for indices, vertices, colors, and UVs

Debug mode tracks allocation positions for memory leak detection.

Returns Description
Mesh A ready-to-use Mesh instance * haxe var mesh = MeshPool.get(); mesh.createQuad(100, 100); parent.add(mesh);

recycle(mesh: Mesh): Void

Returns a mesh to the pool for reuse.

The mesh is automatically:

  • Cleared of all visual data
  • Removed from its parent (if any)
  • Reset to default property values
  • Made inactive (not displayed)
  • Arrays recycled to their respective pools

Debug mode checks for double-recycling and tracks recycling positions.

Name Type Description
mesh Mesh The mesh to recycle

clear(): Void

Clears the entire mesh pool and destroys all pooled meshes.

This permanently destroys all meshes in the pool, freeing their resources. Use this when:

  • Switching between major application states
  • Freeing memory before loading new content
  • Shutting down the application

Note: Array pools are not cleared by this method.

// Before loading a new level
MeshPool.clear();
// All pooled meshes are now destroyed

Private Members

availableMeshes: Array<Mesh>

Pool of available mesh instances ready for reuse.


availableFloatArrays: Array<Array<Float>>

Pool of available float arrays for vertices and UVs.


availableIntArrays: Array<Array<Int>>

Pool of available integer arrays for indices and colors.