question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sequelize with sqlite leaks memory according to Jest

See original GitHub issue

What 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:open
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
papbcommented, Jan 24, 2020

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

0reactions
ephyscommented, Apr 4, 2022

I’m not seeing any calls to Sequelize#close, try adding that and see if the issue resolves itself

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest returns a table not found error with sqlite - Stack Overflow
I suspect the problem is in the fact that when a connection closes, the in-memory database is terminated so I tried running the...
Read more >
Sequelize v6
Sequelize is a promise-based Node.js ORM tool for Postgres, MySQL, MariaDB, SQLite, Microsoft SQL Server, Amazon Redshift and Snowflake's Data ...
Read more >
Memory Storage for Test Environment with Fastify, Sequelize ...
Memory Storage for Test Environment with Fastify, Sequelize, and SQLite · Fastify — Node.js web framework (We will use it for making RESTful...
Read more >
Clustering 4000 Stack Overflow tags with BigQuery k-means
c++, c, c++11, templates, assembly, cmake, gcc, memory, opengl, ... x86, linux-kernel, memory-management, compilation, memory-leaks, ...
Read more >
SQLite3 Sequelize Example Starter - CodeSandbox
SQLite3 database demo, storing database file on local filesystem. Uses Sequelize ORM. sqlite3. sequelize. database.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found