Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongoose jest memory leak #6797

Closed
sibelius opened this issue Aug 1, 2018 · 2 comments
Closed

mongoose jest memory leak #6797

sibelius opened this issue Aug 1, 2018 · 2 comments

Comments

@sibelius
Copy link
Contributor

sibelius commented Aug 1, 2018

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
the heap usage keeps increasing when using mongoose

If the current behavior is a bug, please provide the steps to reproduce.

You can reproduce this bug using this repo https://github.com/graphql-compose/graphql-compose-mongoose from @nodkz

run jest --logHeapUsage

heap usage will keep increasing:

image

You can also run with jest --detectLeaks --detectOpenHandles and check that there are some open handles that keeps Jest from exiting:

image

What is the expected behavior?
The expect behavior is to clean up the heap usage after each test

Please mention your node.js, mongoose and MongoDB version.
mongodb version: 3.x.x and 4.x.x
mongoose 5.2.6
node 8.x.x and 10.x.x

related to #2874

more info here jestjs/jest#6787

I've tried to add this to afterEach, afterAll and it did not help

#2874 (comment)

mongoose.connections.forEach(connection => {
  const modelNames = Object.keys(connection.models)

  modelNames.forEach(modelName => {
    delete connection.models[modelName]
  })

  const collectionNames = Object.keys(connection.collections)
  collectionNames.forEach(collectionName => {
    delete connection.collections[collectionName]
  })
})

const modelSchemaNames = Object.keys(mongoose.modelSchemas)
modelSchemaNames.forEach(modelSchemaName => {
  delete mongoose.modelSchemas[modelSchemaName]
})

is there any place where mongoose keep references? that is not garbage collected properly?

@sibelius
Copy link
Contributor Author

sibelius commented Aug 3, 2018

afterAll worked well to reduce memory leak in mongoose

we fixed the rest of leak mocking graphql-redis-subscriptions

@sibelius sibelius closed this as completed Aug 3, 2018
@murbanowicz
Copy link

What did you add exactly to the afterAll?

@Automattic Automattic locked and limited conversation to collaborators Jan 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants