heroiclabs / nakama

Distributed server for social and realtime games and apps.
https://heroiclabs.com
Apache License 2.0
8.58k stars 1.06k forks source link

How to properly manage Memory? Leaking Memory.... #1216

Closed exotexot closed 1 month ago

exotexot commented 1 month ago

I'm running Nakama Typescript server in my docker, and I do see memory leaking. Screenshot 2024-05-22 at 18 04 58

The graph dropping down is when I Restarted the machine.. However I Create matches and terminate them, still I see memory aggregating.

I do have logging active. People tell me to disable logging would save memory. However, I do feel like there is something wrong. I use TS template code mainly from your demo repo. And I even optimise my matchLoop to use dispatch.roadcastMessageDeferred() in order to avid excessive socket messaging.


let matchLoop: nkruntime.MatchLoopFunction<State> = function (
    ctx: nkruntime.Context,
    logger: nkruntime.Logger,
    nk: nkruntime.Nakama,
    dispatcher: nkruntime.MatchDispatcher,
    tick: number,
    state: State,
    messages: nkruntime.MatchMessage[]
) {
    const unifiedTransforms: { [key: string]: any } = {}

    for (const message of messages) {
        switch (message.opCode) {

            // Transform Update
            case OpCode.TRANSFORM_UPDATE:
                let transformMsg = {} as any
                try {
                    transformMsg = JSON.parse(nk.binaryToString(message.data))
                    unifiedTransforms[message.sender.userId] = transformMsg
                } catch (error) {
                    logger.error("Bad data received: %v", error)
                    continue
                }
                break

            case OpCode.STOP_SESSION:
                return null

                break

            default:
                // No other opcodes are expected from the client, so automatically treat it as an error.
                dispatcher.broadcastMessage(OpCode.STATE_UPDATE_REJECTED, null, [message.sender])
                logger.error("Unexpected opcode received: %d", message.opCode)
        }
    }

    state.transforms = combineObjects(state.transforms, unifiedTransforms)

    // Broadcast the unified state to all clients
    dispatcher.broadcastMessageDeferred(OpCode.UNIFIED_STATE_UPDATE, JSON.stringify(state))

    return { state }
}

There was a similar issue on the forums: https://forum.heroiclabs.com/t/memory-leak-profiling/615 - but it seemed amazon specific.

Your Environment

linear[bot] commented 1 month ago

NK-553 How to properly manage Memory? Leaking Memory....

sesposito commented 1 month ago

Hello @exotexot, we'll need more information, could you please take this over to our forums and open a thread there?

zyro commented 1 month ago

Forum here.