[Bug]: Building a scene with a large terrain holds ~8 GB of scratch memory on a support table that is never read

Author: Kashu7100Created Sep 18, 2026Updated Sep 18, 2026

Bug Description

Building a scene that contains a terrain spends most of its time and memory on a support table for the terrain geom that no kernel ever reads, and on a mesh simplification whose result is discarded for single-sided surfaces.

Collider.__init__ activates the support field whenever has_terrain, and SupportField.activate then builds a table for every geom, the terrain included. A terrain has one vertex per height field sample, so its table costs 32400 directions x n_verts products, computed as dot_chunk = v1[i:end] @ this_pos.T in chunks of 5e8 elements. v1 is float64, so one chunk is 4 GB, and because the previous dot_chunk is still bound while the next one is computed, two are alive at once: ~8 GB of scratch memory, held for as long as the loop runs (15 s for 200k vertices, minutes for 2M).

The table is dead weight: both support_drivers (mpr.py, gjk_support.py) route GEOM_TYPE.TERRAIN to _func_support_prism (built from collider_info.terrain_hf), and count_support_driver only reads the table for GEOM_TYPE.MESH.

Separately, convert_heightfield_to_watertight_trimesh always runs fast_simplification.simplify(..., lossless=True) on the closed mesh, but only returns the result (vmesh_full) when surface.double_sided; for the default single-sided surface it is thrown away (6.7 s and +2.1 GB at 2001x1001).

This is what #498 ran into in early 2025; the 5e8 chunking bounds the allocation but the work and most of the memory are still there.

Steps to Reproduce

python
import resource, time
import numpy as np
import genesis as gs

rows, cols = 801, 251  # or 2001, 1001
gs.init(backend=gs.gpu, logging_level="warning")
scene = gs.Scene(show_viewer=False)
scene.add_entity(gs.morphs.Terrain(height_field=np.zeros((rows, cols), dtype=np.float32), horizontal_scale=0.08, vertical_scale=0.005))
scene.add_entity(gs.morphs.Box(size=(0.2, 0.2, 0.2), pos=(rows * 0.04, cols * 0.04, 0.5)))
t0 = time.time()
scene.build(n_envs=16)
print(f"build={time.time() - t0:.1f}s peak_rss={resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 2**20:.2f}GB")

On main (c27875eb), measured inside a cgroup with MemoryMax=14G:

height field vertices scene.build peak RSS
801 x 251 201k 31.5 s 9.15 GB
2001 x 1001 2.0M OOM-killed at 14 GB > 14 GB

Per-stage RSS sampling on a real task (Go2 on a 10x20 parkour course grid, 2001x1001 height field) attributes the spike to SupportField.activate: on the 801x251 subset it alone takes 15.0 s and lifts RSS from 2.2 GB to 9.7 GB while every other stage of the build stays under 3 GB. On a 60 GB desktop the full-size build ran for ~3 minutes in that state before systemd-oomd killed the whole session for memory pressure.

Expected Behavior

A terrain is "just a height field": building it should not cost more than the mesh and the height field themselves. With the terrain skipped in the support field and the simplification made conditional, the same script gives:

height field scene.build peak RSS
801 x 251 2.1 s 1.96 GB
2001 x 1001 18.7 s 8.99 GB

Screenshots/Videos

No response

Relevant log output

No response

Environment

  • OS: Ubuntu, Linux 7.0
  • GPU/CPU: RTX 3080 (10 GB), 24-core CPU, 60 GB RAM
  • GPU-driver/CUDA: CUDA 13.0 (torch 2.14+cu130)

Release version or Commit ID

c27875eb56947808b3105d25d3c09b6dd96484b3 (main, 1.4.1)

Additional Context

What remains after the fix at 2001x1001 is mostly Mesh.get_vert_adjacency (8.4 s, +2.8 GB: trimesh's vertex_neighbors flattened through Python tuples), which could be built from edges_unique with numpy; I left it out because the neighbour order it produces may matter to the hill-climbing lookups. I have a PR ready for the two changes above.

Source: Genesis-Embodied-AI/genesis-world