oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.23k stars 2.77k forks source link

Possible memory leak #14846

Open kwaitsing opened 2 weeks ago

kwaitsing commented 2 weeks ago

What version of Bun is running?

1.1.31+e448c4cc3

What platform is your computer?

x64

What steps can reproduce the bug?

Setup https://github.com/kwaitsing/Artemis and wait for a few mins

It only happens on client side

What is the expected behavior?

My prog takes far less memory than 409MB

What do you see instead?

Image Image

Additional information

code that might be the cause

// Real Time Data Monitor
import { networkInterfaceDefault, currentLoad, networkStats, fsSize, mem } from "systeminformation";
import type { uploadDataType } from "./type";
import { GlobalConfiguration } from "..";

const doRecordFs: string[] = [
    "apfs",
    "hfs",
    "ext4",
    "ext3",
    "ext2",
    "f2fs",
    "reiserfs",
    "jfs",
    "btrfs",
    "fuseblk",
    "zfs",
    "simfs",
    "ntfs",
    "fat32",
    "exfat",
    "xfs",
    "fuse.rclone",
]

// Network
export let RTNetFlow: uploadDataType['network'] = {
    total: {
        up: 0,
        down: 0
    },
    current: {
        up: 0,
        down: 0
    }
}
// cpu
export let RTcpu = 0;
// Storage
export let RTstorage: uploadDataType['storage'] = {
    used: 0,
    total: 0
};
// Mem
export let RTMem: uploadDataType['mem'] = {
    onboard: {
        used: 0,
        total: 0
    },
    swap: {
        used: 0,
        total: 0
    }
}

export const startTimer = async () => {

    // Runtime Conf
    const mainInterface = await networkInterfaceDefault();
    // Avoid deadlock
    let isTimerRunning = false;

    setInterval(async () => {

        if (!isTimerRunning) {
            isTimerRunning = true;
            // CPU
            RTcpu = (await currentLoad()).currentLoad;
            // NetFlow
            const data = (await networkStats()).find((nw) => nw.iface === mainInterface)
            RTNetFlow = {
                total: {
                    up: data?.tx_bytes || 0,
                    down: data?.rx_bytes || 0
                },
                current: {
                    up: data?.tx_sec || 0,
                    down: data?.rx_sec || 0
                }
            };
            // Storage
            let singleRTStorage = {
                used: 0,
                total: 0
            };
            (await fsSize()).forEach((fsObj) => {
                if (doRecordFs.includes(fsObj.type))
                    RTstorage = {
                        used: fsObj.used + singleRTStorage.used,
                        total: fsObj.size + singleRTStorage.total
                    };
            });
            // Mem
            const memData = await mem()
            RTMem = {
                onboard: {
                    used: memData.active,
                    total: memData.total
                },
                swap: {
                    used: memData.swapused,
                    total: memData.swaptotal
                }
            }
            isTimerRunning = false;
        }

    }, GlobalConfiguration.updInterval * 1000);
}

I think this should be related to systeminformation on npm, maybe bunch of open files caused that?, and i'm using a bundle executable

Thank you guys for developing bun, it would be nice if we could find the cause to this problem together

kwaitsing commented 2 weeks ago

Image Tried using the debugger and here's what i found

kwaitsing commented 2 weeks ago

This array grow over times and becomes huge

kwaitsing commented 2 weeks ago

Fixed by rewritten in golang

Update for anyone why may wanna try it'

Check that link above, after I rewritten the system information with Bun fashion still the mem usage climb, but much slower

However the strange thing is that, this time I can't find any sign of a memory leak in the inspector timeline, the size of each snapshot is completely normal but in reality, I mean the top or htop, it goes insane on my server and crashed it