Account for Map/Set retained strings in JS_ComputeMemoryUsage, or document partial accounting

Abierto
#617 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 5 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
30/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
c, javascript

Línea de trabajo

Start with JS_ComputeMemoryUsage in quickjs.c, especially the opaque-class TODO near lines 6871-6889, and check how Map and Set storage is reached from the GC object list. Reproduce with the Map/Set/Array script in the issue to confirm that retained strings are missed. Done means either Map/Set keys and values are counted with shared references handled, or the API docs state that these contents are not traversed. The maintainer has not chosen between the two, so confirm the direction before writing code.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Reporting gap

JS_ComputeMemoryUsage() appears to omit strings that are strongly retained only through Map or Set entries. The same strings stored in an Array are counted.

The current source already lists these opaque container classes as a TODO. This issue is intended to track implementing that accounting, or documenting the limitation on the API. It is not a claim of a memory leak or a memory-limit bypass.

Reproducer

Tested with the Bellard QuickJS 2025-09-13 lineage distributed by [email protected] (release-sync WebAssembly). I have not independently run a native C build.

npm install [email protected]
node repro.mjs
import { getQuickJS } from 'quickjs-emscripten';
const QuickJS = await getQuickJS();
for (const kind of ['Map', 'Set', 'Array']) {
  const runtime = QuickJS.newRuntime();
  const context = runtime.newContext();
  function usage() {
    const h = runtime.computeMemoryUsage();
    try { return context.dump(h); } finally { h.dispose(); }
  }
  try {
    usage(); // Warm the wrapper's system context before taking the baseline.
    const before = usage();
    const code = kind === 'Map'
      ? "globalThis.keep = new Map(); for (let i=0;i<128;i++) keep.set(i,'x'.repeat(8192)+i); keep.size;"
      : kind === 'Set'
      ? "globalThis.keep = new Set(); for (let i=0;i<128;i++) keep.add('x'.repeat(8192)+i); keep.size;"
      : "globalThis.keep = []; for (let i=0;i<128;i++) keep.push('x'.repeat(8192)+i); keep.length;";
    const result = context.evalCode(code);
    if (result.error) {
      const error = context.dump(result.error); result.error.dispose();
      throw new Error(JSON.stringify(error));
    }
    const entries = context.dump(result.value); result.value.dispose();
    const after = usage();
    console.log({kind, entries,
      byteDelta: after.memory_used_size - before.memory_used_size,
      strCount: after.str_count, strBytes: after.str_size});
  } finally { context.dispose(); runtime.dispose(); }
}

Observed

Each container remains reachable via globalThis.keep and reports 128 entries:

Container memory_used_size increase str_count
Map 293 bytes 0
Set 293 bytes 0
Array 1,052,255 bytes 128

The values are 128 distinct strings of roughly 8 KiB each. Only the container changes. Ordinary object values may still be counted via the runtime GC list; the example specifically demonstrates missing retained strings.

Expected / request

Account for Map/Set backing storage and retained keys/values, with appropriate handling of shared references, or clearly document that this API does not traverse these contents. This matters when using snapshots to diagnose retained memory: a live Map containing about 1 MiB of strings otherwise appears almost unchanged.

Relevant source: JS_ComputeMemoryUsage opaque-class TODO. The wrapper delegates to that core function.

Related but different: quickjs-emscripten #206 and PR #207 concern allocator / malloc_usable_size accounting. They do not implement this Map/Set content traversal. I searched open/closed issues and PRs in both repositories and did not find an existing report for this specific gap.

Lenguaje dominante
C
Estrellas
11.1k
Forks
1.2k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de bellard/quickjs

Todos los issues de bellard/quickjs

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.