# Does Apollo client perform any garbage collection on watches?

**URL:** <https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282>\
**Category:** Client SDKs\
**Tags:** client\
**Created:** [May 19, 2023, 5:13pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282 "2023-05-19T17:13:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Davison](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/john_davison/32/810_2.png) [@John\_Davison](https://community.apollographql.com/u/John_Davison)\
**Post date:** [May 19, 2023, 5:13pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/1 "2023-05-19T17:13:38Z")

</div>

I am working on a large app and have noticed performance degrade over time.

From inspecting the cache I can see over 20,000 watches in the cache:  
`InMemoryCache2 {watches: Set(21126), typenameDocumentCache: Map(286), txCount: 0, getFragmentDoc: ƒ, makeVar: ƒ, …}`

The watches don’t appear to get garbage collected when the component that called `useQuery` is unmounted so the watches `Set` just increases in size over time.

Is this expected behaviour?

And is there way to remove watches on unmount, or trigger a garbage collect somehow?

Thanks

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [May 19, 2023, 6:29pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/2 "2023-05-19T18:29:12Z")

</div>

Hi @John_Davison 👋 thanks for posting! What version of `@apollo/client` are you using?

---

<div class="post-metadata">

**Author:** ![John\_Davison](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/john_davison/32/810_2.png) [@John\_Davison](https://community.apollographql.com/u/John_Davison)\
**Post date:** [May 19, 2023, 6:39pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/3 "2023-05-19T18:39:41Z")

</div>

Hi @JeffAuriemma thanks for the quick response. We’re on `"@apollo/client": "3.7.14"`

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [May 31, 2023, 3:53pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/4 "2023-05-31T15:53:09Z")

</div>

Thanks! @John_Davison can you confirm this is a web application running in the browser and not a React Native app?

---

<div class="post-metadata">

**Author:** ![John\_Davison](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/john_davison/32/810_2.png) [@John\_Davison](https://community.apollographql.com/u/John_Davison)\
**Post date:** [May 31, 2023, 4:42pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/5 "2023-05-31T16:42:21Z")

</div>

Hi @JeffAuriemma yes this is an web app running in the browser.

I’ve been investigating further and found that the main culprit is a hook that we use to make several useQuery calls (relying on cache hits to avoid network requests after the first one).

This hook gets called from several places within a very busy part of our app which means the useQuery hooks are called many times in quick succession.

From what I’ve observed, when this happens in quick succession, not all the watches are unregistered on unmount, which leads to a gradual increase in watches that will never be removed.

Looking at the useQuery hook code, the unsubscribe part looks pretty straight forward so I’m not sure why this is happening.

For now, I’ve mitigated this somewhat by switching from useQuery to client.query for queries that don’t need to be observed.

And where queries do need to be observed I’ve moved the useQuery call up to a context.

This seems to be working for us for now, but it would be great if you are able to look into this.

Many thanks  
John

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [June 1, 2023, 8:37pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/6 "2023-06-01T20:37:04Z")

</div>

Thanks @John_Davison!

> [@John\_Davison](#):
>
> a hook that we use to make several useQuery calls (relying on cache hits to avoid network requests after the first one).

Can I ask for a few more details about this? Are you getting 100% cache hits on the subsequent `useQuery` calls?

---

<div class="post-metadata">

**Author:** ![John\_Davison](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/john_davison/32/810_2.png) [@John\_Davison](https://community.apollographql.com/u/John_Davison)\
**Post date:** [June 4, 2023, 5:14pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/7 "2023-06-04T17:14:44Z")

</div>

Hi @JeffAuriemma yes we are getting 100% cache hits for subsequent useQuery calls

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [June 6, 2023, 4:17pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/8 "2023-06-06T16:17:22Z")

</div>

I have an update for you @John_Davison - we’re not sure what might be going on under the hood here with the information given but we are interested in getting a more robust picture of general scalability challenges associated with `useQuery`. We’ll be taking a deeper look at this in the foreseeable future and will be sharing our findings in the `apollo-client` repo. Glad you found a workaround for the time being, stay tuned!

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:48pm UTC](https://community.apollographql.com/t/does-apollo-client-perform-any-garbage-collection-on-watches/6282/9 "2025-02-06T20:48:44Z")

</div>


