-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Calling cache.gc()
might cause memory usage increase and performance impact since v3.9
#12360
Comments
Hmm. I'll have to dig deeper into this, but also appreciate any further insights you might come up with. |
I would need to dig deeper into the code as well, to understand where the But this could be related to timing issue where:
Screenshot: |
Okay, I've dug into this - this is actually not an unbounded memory leak, but expected behaviour. First thing, if this is disturbing you, you can reset everything by calling client.cache.gc({ resetResultCache: true }); which will also reset/recreate the But that said, this is kinda expected behaviour given what the You can see this cache like cache management in an operating system like Linux - when you look at memory usage, it will be constantly growing, but a lot of it is labelled as "cache", not "memory usage" - if memory usage reaches a certain threshold, that memory is collected, but before that it's held in case it might be useful in the future. We don't actively remove data from it before it grows full. If you want to limit how big this cache can grow, I recommend you set a smaller cache size. PS: I'm kinda curious - in what environment are you using this that memory size is of a concern to this level? Note that on servers you shouldn't have ApolloClient instances that exist for longer than one user request. |
Thank you for the quick turn around.
For context:
When we tried to upgrade the client to v3.9, we saw increase in browser's Also, historically, we has been configuring the
I will get back to the team with your suggestions and see how best to proceed forward in our case. |
Issue Description
Descriptions:
Since
@apollo/client
v3.9, callingclient.cache.gc()
increase memory usage in various cache subsequently.Some affected cache are:
This behaviour did NOT occur in v3.8 .
Also, I think there is performance impact here as well when trying to re-read previous gql query document, after calling
cache.gc()
This happens because subsequent usage of cache.storeReader.executeSelectionSet (optimism.wrap) will result in cache miss.
Some other notes:
When doing git bisect on apollo-client repo, the first commit introducing this behaviour is bd26676
From reading the code, I think by reseting the cache in
inMemoryCache.addTypenameTransform
(source), it produces different cache key for the same gql query subsequently.The chain is something like this:
selectionSet
as part of its keyselectionSet
is returned from QueryManager.transform()Querymanager.transform()
makes use ofinMemoryCache.addTypenameToDocument
(source)inMemoryCache.addTypenameToDocument()
returns different value for same documentNode afterinMemoryCache.addTypenameTransform.resetCache()
ininMemoryCache.gc()
Hope this make sense.
Link to Reproduction
https://codesandbox.io/p/devbox/apollo-3-9-memory-leak-public-xknp7y?file=%2Fsrc%2FApp.tsx&workspaceId=ws_UB3koDt7PAYGsj7fRJouXc
Reproduction Steps
Reproduction steps:
useQuery()
hooks (Code copied from get-started doc)apolloClient.cache.gc()
at the same timeinMemoryCache.executeSelectionSet.size
inMemoryCache.executeSelectionSet.size
stays at6
Reproduction screenshot:
@apollo/client
version3.12.11
The text was updated successfully, but these errors were encountered: