Skip to content

Memory Leak in High-Frequency triggerAttackRelease() Loops #1379

@kdulcet

Description

@kdulcet
Image

You can see from the DevTools memory browser that this example has a leak. I encountered this in the app I am working on.

Reproduction:

  • Single Tone.Synth with basic ADSR envelope
  • Tone.Loop calling synth.triggerAttackRelease(440, '32n', time) at 8Hz
  • Chrome heap snapshot after 60 seconds: 1,738+ AudioParam objects with methods {linearRampToValueAtTime, cancelScheduledValues, setValueCurveAtTime} retained in memory

Workaround I'm using (more or less)

// Setup (once)
const osc = new Tone.Oscillator(440, 'sine').start();
const gain = new Tone.Gain(0);
osc.connect(gain).toDestination();

// Loop (8Hz)
new Tone.Loop((time) => {
  const attack = 0.005;
  const duration = Tone.Time('32n').toSeconds();
  const release = 0.005;
  
  // Cancel automation at pulse start only
  gain.gain.cancelScheduledValues(time);
  
  // Manual envelope with smooth ramps
  gain.gain.setValueAtTime(0, time);
  gain.gain.linearRampToValueAtTime(1, time + attack);
  gain.gain.setValueAtTime(1, time + attack + (duration - attack - release));
  gain.gain.linearRampToValueAtTime(0, time + duration);
}, '16n');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions