oven-sh / bun

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

Potentiel memory leak on simple request (bun + fastify + prisma) #12977

Open goldo opened 1 month ago

goldo commented 1 month ago

What version of Bun is running?

1.1.22-canary.42+bec04c734

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

I made a small reproduction project with bun + fastify + prisma (sqlite): https://github.com/goldo/bun-memory

bun install 
bun start
# then start requesting it and check the heapstats logs on the server
curl http://localhost:3010/ 
autocannon -c 30 -d 30 http://localhost:3010

What is the expected behavior?

Memory should be going down, after a while, at approx. the level of the beginning of the server (first request)

What do you see instead?

memory is going down just a little (GC), but stays really high

Additional information

This is a following issue of https://github.com/oven-sh/bun/issues/7377

Investigation of potential bun memory leak

heapStats().objectTypeCounts check:

    heapStats().objectTypeCounts["Array"],
    heapStats().objectTypeCounts["Object"],
    heapStats().objectTypeCounts["Structure"]

at the beginning:

$ bun start
# curl http://localhost:3010
708 2252 7658

after autocannon -c 30 -d 30 http://localhost:3010 :

18905 54499 15811

After a few minutes (waiting for GC), the objectTypeCounts seems still high, and the memory usage is still high (101mb) vs initial (71mb):

# curl http://localhost:3010
17515 51800 15549

The problem seems to be the same with or without the fastify plugin.

Jarred-Sumner commented 1 month ago

A memory leak is indefinite growth in memory usage. 70 MB -> 100 MB is not clearly a leak. If you can make it go from 70 MB -> 1 GB, and then continue rising after receiving the same request volume then that is a leak.

goldo commented 1 month ago

On the full project, I got this: