Skip to content

How to delete a logger instance to free memory, and kill worker threads #2231

@moda20

Description

@moda20

Hi I am trying to create multiple pino instances and then delete them after finishing using them.
I am using workers to log messages as that drives well with my use case. I recently found that most pino instances, after creation, cannot be freed from memory even when deleted and the created threads don't seem to close.

Here is an example of my use case, it's not a real world use case but shows the issue best

import http from "http"
import pino from "pino"

const start = async () => {

    const server = http.createServer((_req, res) => {
        res.writeHead(200, { "Content-Type": "text/plain" })
        res.end("Server is running\n")
    })

    server.listen(3000, () => {
        console.log("HTTP server running on http://localhost:3000")
    })

    const loggers: pino.Logger[] = []

    for (let i = 0; i < 500; i++) {
        const transport = pino.transport({
            targets: [
                {
                    target: "pino-pretty",
                    level: "info",
                    options: {
                        destination: 1,
                        colorize: true,
                        ignore: "pid,hostname",
                        sync: true,
                    },
                }
            ]
        })
        const logger = pino(
            transport
        )
        logger.info(`Logger ${i} initialized`)
        loggers.push(logger)
    }

    const ll = loggers.length
    for (let i = 0; i < ll; i++) {
        loggers[i].flush(()=> { // the flush di not prove to be useful in reducing memory usage or thread count
            delete loggers[i]
            console.log("logger flushed")
        });
        await new Promise((res) => setTimeout(() => res(), 250)); // this is not useful in any way 

    }

};

start()

It's an HTTP server with 500 pino instance loggers being created that use the worker method, they also use pino-pretty as a large enough transport. Running this will take about 5Gb of RAM and will spawn around 1k+ threads. under no circumstances the memory will be freed.

I am using the latest [email protected] and [email protected] and on macOs (although the issue was seen on a linux server as well).

Note : I read about the child loggers, but in my current use-case my transports include different files and the child loggers don't allow for that kind of alteration

Note : I am running this via Bun

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions