Sequelize with sqlite leaks memory according to Jest
See original GitHub issueWhat are you doing?
My Jest tests started to crash with “JavaScript heap out of memory”. Enabling --detectLeaks
I narrowed that to every test that uses Sequelize. Here’s a minimal repro:
node --expose-gc node_modules/.bin/jest --detectLeaks repro.js
import * as Sequelize from 'sequelize'
// Experiment: pre-requiring dynamic imports didn't increase memory during tests, but not the behavior of "JavaScript heap out of memory" error.
require('sqlite3')
require('sequelize/lib/dialects/sqlite')
const getMemory = () => {
global.gc()
process.memoryUsage().heapUsed / Math.pow(1024, 2)
}
test.each(['./db.sqlite', './db2.sqlite', undefined, undefined])('Sequelize should not leak', (storage) => {
const mem = getMemory()
new Sequelize('Leaker', 'tester', 'bestpassword', {dialect: 'sqlite', storage})
console.log('Memory (MB):', mem.toFixed(2), '+', (getMemory() - mem).toFixed(2))
})
Furthermore, I found out that the problem is a “sqlite” dialect, because if I add return
before any of the following lines, the Jest’s “Your test suite is leaking memory.” is gone and repro succeeds.
this.dialect = new Dialect(this); // node_modules/sequelize/lib/sequelize.js
this.connectionManager = new ConnectionManager(this, sequelize); // node_modules/sequelize/lib/dialects/sqlite/index.js
this.lib = require('sqlite3').verbose(); // node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
What do you expect to happen?
I expect the repro above to succeed.
What is actually happening?
The repro above throws “JavaScript heap out of memory” by Jest.
node: v8.15.0 Jest: 24.5.0 Dialect: sqlite Dialect version: sqlite3@4.0.6 Database version: n/a Sequelize version: sequelize@4.39.0 Tested with latest release: Yes
P.S. Don’t know if this is either Sequelize or Jest bug, so I posted at both.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (3 by maintainers)
Can anyone please test if this isn’t an issue with the sqlite3 package itself? In other words, what happens if you run Jest with sqlite3 directly (not Sequelize)? Thanks
I’m not seeing any calls to
Sequelize#close
, try adding that and see if the issue resolves itself