Describe the bug
massive request and memory is not clear.
i'm not sure this is bug or intent to recreate prisma client every request.
To Reproduce
Steps to reproduce the behavior:
const { prisma } = require('./generated/prisma-client')
const http = require('http')
const port = 8888
const requestHandler = async function(request, response){
response.end(JSON.stringify(await prisma.users()))
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
ab -c 10 -n 20000 http://localhost:8888
Expected behavior
memory should clear.
or should i recreate prisma client every request instead ?
Screenshots
memory snapshot: https://puu.sh/CjwsF/14cebbab20.png
Versions (please complete the following information):
i found some code in file prisma-client-lib/src/Client.ts.
the function processInstructionsOnce will keep result promise onto _promises.
but it never remove, this will make result from prisma always in memory.
processInstructionsOnce = (id: number): Promise<any> => {
if (!this._promises[id]) {
this._promises[id] = this.processInstructions(id)
}
return this._promises[id]
}
and _currentInstructions will keep instruction array then set to [] when executed, but it still not clear from _currentInstructions, this will leak too ? but this is too small if compare with _promises.
this make me not sure. Should i new prisma instance every request or not ?
I can confirm that Prisma client is probably leaking memory.
I ran several simple queries using Prisma client and memory keeps increasing, when inspected with Chrome devtools.
Some allocation sampling gives me:

This is pretty bad, as the the only sane workaraound is running pm2 and using graceful shutdown, once a day + when exceeding a certain memory level.
Plus: I suggest renaming this issue: "Is Prisma client leaking memory?" or similar. I was not sure what this was about on first sight (clear memory).
@ArtNattapat : Thanks for the reproduction and initial investigation. That helped me a lot in fixing this quickly.
@nickluger : Thanks for the confirmation of the issue and suggesting a potential workaround.
This issue is resolved in the latest beta 1.25.0-beta.31.
The relevant npm module in this case is prisma-client-lib and that can be installed via
yarn add prisma-client-lib@beta.
Please confirm if the fix works for you.
@divyenduz thanks for fix that.
i tried with beta version and it looks good.
but some point i don't understand in your pull request. why in function _releaseMemory doesn't
delete this._currentInstructions[id]
instead of
this._currentInstructions[id] = []
Will this make _currentInstructions always have new keys for each query ? will this make some a bit memory leaking ?
@ArtNattapat : Thanks for the feedback again. This piece of code already existed and I just moved it inside the function but you are right this will have a very very small memory footprint and can be changed to delete this._currentInstructions[id]. I have taken a note of that and would make this change soon.
i see. thank you :)
Hi,
Thanks for fixing the memory leak issue. Here is the chart after update:

I updated on Wed 20 02:00.
There is still some increase in memory. Not sure if it's normal or there is another leakage
Update:
It seems like it's stable now. I even see a 1% - 2% drop on memory now.

I'll follow up in a couple of days and let you know if there is any anomalies
Thanks! This is available in the latest stable already (1.26.x) 馃帀, please install it via npm install -g prisma or brew upgrade prisma.
I am closing this issue, please ping if the issue persists.
I have been experiencing a memory leak for more than three months, but I am not sure if it is my own code problem or a prisma problem, and I have a lot of development tasks. Intermittently tried 4 to 5 times to solve this memory leak problem, the memory will be more than 1.2G in a day, you must restart the system every day to ensure normal operation.
Recently I fixed the problem of prisma-client-lib. In the _releaseMemory method, I saw that your discussion above added the "delete this._promises[id];" code in version 1.26. Maybe this code solves your leak, but it doesn't solve my memory leak.
The scene of my system is like this. At present, there will probably be more than 10 browser pages that will initiate three requests every 5 seconds. The total size of resources acquired by these three requests is about 30K. The point is that these browser pages are never closed, I guess this is what makes me different from you.
The version of prisma-client-lib I am using is 1.29.0. Recently I added "delete this._currentInstructions[id]" to the _releaseMemory method, and the memory leak disappeared. I checked the latest version 1.34.10 of prisma-client-lib and found that the _releaseMemory code is still two sentences. (I think there is a problem)
If you want to reproduce this memory leak, it is recommended to create a scenario similar to my system.
Finally, I am very grateful to prisma for bringing me great convenience. I sincerely hope that you are getting better and better.
Thank you
Most helpful comment
i found some code in file prisma-client-lib/src/Client.ts.
the function processInstructionsOnce will keep result promise onto _promises.
but it never remove, this will make result from prisma always in memory.
and _currentInstructions will keep instruction array then set to [] when executed, but it still not clear from _currentInstructions, this will leak too ? but this is too small if compare with _promises.
this make me not sure. Should i new prisma instance every request or not ?