Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StereoAudioRecorder gobbles memory; how to release? #863

Open
darrellsilver opened this issue Jan 31, 2024 · 2 comments
Open

StereoAudioRecorder gobbles memory; how to release? #863

darrellsilver opened this issue Jan 31, 2024 · 2 comments

Comments

@darrellsilver
Copy link

Long recordings eat up a lot of memory, how to release it? Just using Chrome / Mac the below simple code, and have tried several methods implied by their names to clear any stored data.

Shutting off the recording stops the accumulation but that won't work in my application. Any ideas?

<html>
    <head>
        <script src="https://www.WebRTC-Experiment.com/RecordRTC.js"></script>
        <script>
        let mediaRecorder;
        function startRecording() {
            navigator.mediaDevices.getUserMedia({audio: true}).then((stream) => {
                // audioStream = stream
                mediaRecorder = new RecordRTC(stream, {
                    type: "audio",
                    mimeType: "audio/wav;codecs=pcm", // endpoint requires 16bit PCM audio
                    recorderType: StereoAudioRecorder,
                    timeSlice: 250, // set 250 ms intervals of data that sends to AAI
                });
                mediaRecorder.startRecording();
            });
        }
        </script>
    </head>
    <body onload="startRecording()">
        <h1>leaky memories</h1>
    </body>
</html>

Trying these either break the recording or have no apaprent effect:

  • clearRecordedData()
  • clearRecordedDataCB()
  • resetVariables() <-- doesn't work
  • reset() <-- stops recording
@darrellsilver
Copy link
Author

darrellsilver commented Jan 31, 2024

Got it working by manually freeing the data stored by StereoAudioRecorder. Seems like a hack so going to leave this issue open w/ my code.

Add a freeMemory func to RecordRTC.js:

    function resetVariables(stopRecording) {
        leftchannel = [];
        rightchannel = [];
        recordingLength = 0;
        if (stopRecording === undefined) {
            console.log('stopRecording all');
            isAudioProcessStarted = false;
            recording = false;
            isPaused = false;
            context = null;
        }

        self.leftchannel = leftchannel;
        self.rightchannel = rightchannel;
        self.numberOfAudioChannels = numberOfAudioChannels;
        self.desiredSampRate = desiredSampRate;
        self.sampleRate = sampleRate;
        self.recordingLength = recordingLength;

        intervalsBasedBuffers = {
            left: [],
            right: [],
            recordingLength: 0
        };
    }

    this.freeMemory = function() {
        resetVariables(false);
    }

Then in my code, as part of ondataavailable I call
mediaRecorder.getInternalRecorder().freeMemory()

According to Chrome Memory tool it works & in Safari too (Silicon Mac 13.4.1).

@augustomaillo
Copy link

I was struggling to deal with this memory consumption. I ended up removing RecordRTC.js lines 3136, 3141 and 3144. This class absolutely save me, but this "memory leak" almost ruins everything.

Maybe some config in constructor do control wheter to keep this buffer or not, or the maximum number of bytes to save, can be very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants