react-audio-visualize icon indicating copy to clipboard operation
react-audio-visualize copied to clipboard

Live Audio Visualise continuously increasing the tab in brower's Memory utlisation.

Open theguidingstar opened this issue 1 year ago • 4 comments

Example Usage :

{mediaRecorder && ( <LiveAudioVisualizer mediaRecorder={mediaRecorder} height={100} width={300} barWidth={5} gap={3} /> )}

When starting to visualise it uses 250mb (Project files) as soon as we start to visualise it. Within 2-3 mins, the memory usuage of tab shoots upto 2gb.

anyways it can be addressed or handled? Something i am doing wrong here?

theguidingstar avatar Apr 11 '24 06:04 theguidingstar

We are facing the same issue, Chrome Tab Memory goes up to 1.6 GB in 10 minutes, Anyone has got any solution for this ?

sharjeelaqdusrb avatar May 09 '24 08:05 sharjeelaqdusrb

I don't think the logic here handles when going from paused to closed (i.e if you pause and then stop the stream, audio context won't get closed)

if (mediaRecorder.state === "recording") {
      analyser?.getByteFrequencyData(data);
      processFrequencyData(data);
      requestAnimationFrame(report);
    } else if (mediaRecorder.state === "paused") {
      processFrequencyData(data);
    } else if (
      mediaRecorder.state === "inactive" &&
      context.state !== "closed"
    ) {
      context.close();
    }
  }, [analyser, context.state]);

report get's called only when in recording state, and with the recursive requestAnimation callback:

 useEffect(() => {
    if (analyser && mediaRecorder.state === "recording") {
      report();
    }
  }, [analyser, mediaRecorder.state]);

zmays avatar Jul 29 '24 18:07 zmays

In our repo we were mounting and unmounting this component in a user flow, and found that the context.close call never happens FYI, we updated the effect to close on unmount/re-run of the hook:

	useEffect(() => {
		if (!mediaRecorder.stream) return;

		const audioContext = new AudioContext();
		const analyserNode = audioContext.createAnalyser();
		analyserNode.fftSize = fftSize;
		analyserNode.minDecibels = minDecibels;
		analyserNode.maxDecibels = maxDecibels;
		analyserNode.smoothingTimeConstant = smoothingTimeConstant;
		const source = audioContext.createMediaStreamSource(mediaRecorder.stream);
		source.connect(analyserNode);

		setAnalyser(analyserNode);

		return () => {
			setAnalyser(undefined);
			void audioContext?.close();
		};
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [mediaRecorder.stream]);

zmays avatar Jul 31 '24 20:07 zmays

pr for it here: https://github.com/samhirtarif/react-audio-visualize/pull/23

zmays avatar Jul 31 '24 20:07 zmays

Really sorry for missing this and thank you for pointing this out. I've left a review on the PR and hoping to get this fix merged soon! 🙏🏻

samhirtarif avatar Aug 31 '24 12:08 samhirtarif

#23 and #27 add cleans ups for the audio context, analyser node and source node which should ideally fix any memory leaks.

samhirtarif avatar Sep 07 '24 12:09 samhirtarif