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

potential memory leak issue in subscription #7775

Closed
lsping8 opened this issue Mar 2, 2021 · 11 comments
Closed

potential memory leak issue in subscription #7775

lsping8 opened this issue Mar 2, 2021 · 11 comments

Comments

@lsping8
Copy link

lsping8 commented Mar 2, 2021

Hi, I have this test code to test out if Apollo cache is causing the memory leak issue. I have a subscription resolver that will keep publishing at the interval of 10ms .

import { useMutation, useQuery, useSubscription } from '@apollo/client'
import { QUERY_TEST, SUB_TEST, MUTATE_TEST } from './documentNodes'

export default function Home() {
  const { data, loading } = useQuery(QUERY_TEST);
  const [startTest] = useMutation(MUTATE_TEST)
  useSubscription(SUB_TEST)

  if (loading) return null
  return (
    <div>
      <div><button onClick={() => startTest()}>start test</button></div>
      {JSON.stringify(data, null, 2)}
    </div>
  )
}

Intended outcome:

The heap memory in use should not keep increasing and not dropping at all.

Actual outcome:

This screenshot is taken when the page is active for a few minutes, when it's started the heap size is around 30mb.
Screenshot-2021-02-27-at-9-30-50-PM
How to reproduce the issue:

https://github.com/lsping8/apollo-memory-test
I have created this repo to reproduce the issue, clone the repo and follow the setup process. In the app I have added a button to start interval process, click on the start test button at the left top corner and monitor the memory in chrome.

Versions

System:
OS: Linux 4.15 Ubuntu 16.04.7 LTS (Xenial Xerus)
Binaries:
Node: 12.21.0 - /usr/bin/node
Yarn: 1.22.5 - /usr/bin/yarn
npm: 6.14.11 - /usr/bin/npm
Browsers:
Firefox: 86.0
npmPackages:
@apollo/client: ^3.3.11 => 3.3.11
apollo-server: ^2.21.0 => 2.21.0
apollo-upload-client: ^14.1.3 => 14.1.3

@brainkim brainkim self-assigned this Mar 23, 2021
@brainkim
Copy link
Contributor

brainkim commented Mar 23, 2021

Hey @lsping8! Thank you so much for creating a reproduction, and I wanted to tell you I’m looking into this. I tried running your example and I need to look a bit deeper into it, but it seems like Chrome will collect the memory at some point, creating a saw-tooth pattern in terms of memory usage. So my question is, have you noticed the sample application or your actual code crashing from running out of memory? If you haven’t seen this, I’m hesitant to call this a leak, so much as maybe using too much memory. I, of course, could be wrong about this!

Also note that if you want more consistent memory usage, the cache abstraction provides some utility methods to reclaim and evict objects as described in the following document https://www.apollographql.com/docs/react/caching/garbage-collection/

Again, thanks for opening this issue, and just wanted to let you know this issue is seen and heard.

@lsping8
Copy link
Author

lsping8 commented Mar 25, 2021

Hi @brainkim, thank you for the reply. To answer your question if the application is crashing, the answer is yes. Well not the sample repo, but the production application we have.

Whats-App-Image-2021-02-26-at-4-24-15-PM

Firefox seem to be less tolerance at this issue. As for workaround, we just set all subscription to no-cache fetchPolicy and it seem to resolve the issue.

@brainkim brainkim removed the 🏓 awaiting-contributor-response requires input from a contributor label Mar 25, 2021
@brainkim
Copy link
Contributor

brainkim commented Mar 25, 2021

Potentially related:
#6880
#7013

@fourjuaneight
Copy link

We're seeing similar behavior on our production app as well. We're running Angular with Universal on Heroku. And there's a peak at about 1.5GBs of memory and then it crashes. Tried several patches with various other packages that could've been the culprit, but from our latest findings, seems like Apollo is likely causing it.

@ncioj10
Copy link

ncioj10 commented Jan 7, 2022

@brainkim Hi, we're also experiencing this issue. Is there any news on that?

@etodanik
Copy link

Yup, experiencing this issue HARD on react native

@phryneas
Copy link
Member

@israelidanny for React Native, this might be caused by the fact that we have to fall back to normal Map as data structure instead of WeakMap there. This might change with the new Hermes engine though - and we have an open PR for trying that. Could you try out the release in #10804 and give us feedback there if that improves things?

You can install it with npm i @apollo/[email protected].

@phryneas
Copy link
Member

The aforementioned change is now included in Apollo Client 3.9 beta - please try it out and give us feedback! :)

You can read up more on that in our blog post about the AC 3.9 memory story.

@phryneas
Copy link
Member

phryneas commented Feb 6, 2024

3.9 has been released, so I will close this issue for now.

If this keeps happening in AC => 3.9.0, please open a new issue :)

@phryneas phryneas closed this as completed Feb 6, 2024
Copy link
Contributor

github-actions bot commented Feb 6, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Copy link
Contributor

github-actions bot commented Mar 9, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants