Skip to content

Calling cache.gc() might cause memory usage increase and performance impact since v3.9 #12360

Closed
@tgohn

Description

@tgohn

Issue Description

Descriptions:

Since @apollo/client v3.9, calling client.cache.gc() increase memory usage in various cache subsequently.
Some affected cache are:

  • inMemoryCache. executeSelectionSet
  • inMemoryCache. executeSubSelectedArray
  • client.cache.addTypenameTransform.resultCache

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:

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:

  • Click on "Toggle Show Location & cache.GC()" multiple times
    • this button mounts/unmounts a simple React component that use useQuery() hooks (Code copied from get-started doc)
    • this button also triggers apolloClient.cache.gc() at the same time
  • Observe the various cache size keeps increasing. For example:inMemoryCache.executeSelectionSet.size
    • This behaviour started since v3.9.0
    • in v3.8, theinMemoryCache.executeSelectionSet.size stays at 6

Reproduction screenshot:

Image

@apollo/client version

3.12.11

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions