Skip to content

Optimization reference

This page is intended as a reference collection of optimization tricks that can be used to speed up JME3 applications.

Maintain low Geometry count

The more Geometry objects are added to the scene, the harder it gets to handle them in a speedy fashion. The reason for this is that a render command must be done for every object, potentially creating a bottleneck between the CPU and the graphics card.

Possible optimization techniques

  • Use GeometryBatchFactory.optimize(node) to merge the meshes of the geometries contained in the given node into fewer batches, each based on common Materials used.
    You can optimize nodes using the SceneComposer in the SDK as well: Right-click a node and select “Optimize Geometry”.

Side-effects

  • Using GeometryBatchFactory merges individual Geometries into a single mesh. Thereby it becomes hard to apply specific Materials or to remove a single Geometry. Therefore it should be used for static Geometry only that does not require frequent changes or individual materials/texturing.

  • Using a Texture Atlas provides limited individual texturing of batched geometries.

  • Using the still experimental BatchNode allows batching Geometry while keeping the single Geometry objects movable separately (similar to animation, the buffer gets updated per Geometry position).

Reduce temporary allocations

Creating many short-lived objects in code that runs every frame can increase the work done by the garbage collector. Profile your application first, then focus on places where allocation causes measurable pauses or overhead.

Many math methods have a Local variant that updates the existing object. For example, vector.multLocal(scale) changes vector, while vector.mult(scale) returns a new vector. Use the local form when changing the original is appropriate.

Reuse temporary variables

TempVars provides reusable temporary objects, including vectors and matrices, for calculations within a method. Acquire an instance with TempVars.get() in a try-with-resources block; it is released automatically when the block ends:

import com.jme3.math.Vector3f;
import com.jme3.util.TempVars;

// Inside a method where position and offset are Vector3f values:
try (TempVars vars = TempVars.get()) {
    Vector3f result = vars.vect1.set(position).addLocal(offset);
    // Use result here.
}

The temporary objects can contain values left over from an earlier use, so initialize them before reading them. Their contents can be reused after release(): copy any result you need to keep into an object owned by your application.

Each thread has its own pool. Release variables on the same thread, and release nested acquisitions in reverse order. Keep the scope short and avoid holding TempVars across frames. For values that need to persist, use your own fields instead.

Avoid large objects in physics

To offload much computation to the less CPU intense physics broadphase collision check, avoid having large meshes that cover e.g. the whole map of your level. Instead, separate the collision shapes into multiple smaller chunks. Obviously, don’t exaggerate the chunking, because having excessive amounts of physics objects similarly cause performance problems.

Check the Statistics

SimpleApplication displays a HUD with statistics. Use app.setDisplayStatView(true); to activate it, and false to deactivate it. The StatsView counts Objects,Uniforms,Triangles,Vertices are in the scene, and it counts how many FrameBuffers, Textures, or Shaders:

  • … were switched in the last frame (S)

  • … were used during the last frame (F)

  • … exist in OpenGL memory (M)

For example, Textures (M) tells you how many textures are currently in OpenGL memory.

Generally jME3 is well optimized and optimizes these things correctly. Read statsview to learn the details about how to interpret the statistics, how to tell whether your values are off, or whether they point out a problem.