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.

potential memory leak issue in subscription

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
brainkimcommented, Mar 24, 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.

2reactions
lsping8commented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular- How to avoid Memory Leaks from Subscriptions?
Failing to Unsubscribe can cause memory leaks. Solution. There are many solutions on to this problem. But the most elegant and error-proof solution...
Read more >
RxJS: Why memory leaks occur when using a Subject
It's not uncommon to see the words 'unsubscribe', 'memory leaks', 'subject' in the same phrase when reading upon RxJS-related materials.
Read more >
Preventing Angular Subscription Memory Leaks | Jake Cyr
Preventing Angular Subscription Memory Leaks. Don't let your app get sluggish, handle your subscriptions. Handling Angular Subscriptions Properly.
Read more >
Best practices for avoiding memory leaks in angular
... subscription. You are correctly unsubscribing in the ngOnDestroy block. Both ways are valid and are probably only a matter of preference.
Read more >
How to Detect and Fix Memory Leaks With Chrome DevTools
Identify JS Heap Memory Leaks · Open DevTools · Go to the Memory panel. · select the “Allocation instrumentation on timeline” radio button....
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