m1dugh / native-sound-mixer

MIT License
25 stars 13 forks source link

Memory usage issue #7

Closed D4rkTT closed 3 years ago

D4rkTT commented 3 years ago

Hello, thank you for this great project. I'm facing a big problem with memory usage, when I use simple code like that

setInterval(function(){ 
const d = SoundMixer.devices.filter( dz => dz.type == DeviceType.RENDER);
}, 1);

it get devices based on my filter and store it in variable d every 1ms but every time the loop get devices it increase app memory usage. I tried a lot to insure that's not my code issue.

you can test above code and in less than 5 min the memory usage will be above 1GB !!!!

m1dugh commented 3 years ago

Can't be working on it with tools now i'll run through it by the end of the week, thanks for your issue.

D4rkTT commented 3 years ago

Thank you for ur response, take your time

m1dugh commented 3 years ago

Thanks again for your issue, I fixed the memory issue (or at least I hope so) as of version 3.0.8. Here is a screenshot of my memory debug program. image

as you can see, the rss did not increase after 100 seconds. Here is my memory monitoring program with functions tested.

const { default: SoundMixer, DeviceType, AudioSessionState } = require("../")
const { memoryUsage } = require("process")

let rss = memoryUsage().rss
const heap = memoryUsage().heapTotal

const time = (() => {
    const t = new Date().getTime()

    return () => new Date().getTime() - t
})()

setInterval(() => {
    const devices = SoundMixer.devices
    for (let d of devices) {
        d.volume = d.volume
        d.mute = d.mute
    }
}, 1)

setTimeout(() => rss = memoryUsage().rss, 100);

setInterval(() =>  {
    const d = SoundMixer.getDefaultDevice(DeviceType.RENDER);
    const sessions = d.sessions;
    for(let s of sessions) {
        s.volume = s.volume
        s.mute = s.mute
    }
}, 1);

/*
 * logging the memory stats
 */
setInterval(() => {
    console.clear()
    const usage = memoryUsage()
    console.log("time\trss increase\tcurrent rss\theap increase\theap usage\taverage rss growth")
    console.log(`${Math.round(time()/1000)}s\t${((usage.rss - rss) / Math.pow(1024, 2)).toFixed(1)}mB\t\t${Math.round(usage.rss / 1024)}kB\t\t${((usage.heapTotal - heap) / Math.pow(1024, 2)).toFixed(1)}mB\t\t${Math.round((usage.heapUsed / usage.heapTotal) * 100)}%\t\t${((usage.rss - rss)*1000/(1024*time())).toFixed(1)}kB/s`)
}, 100)