Replies: 1 comment
-
Thanks everyone — I’ve found the root cause and wanted to share it here before closing this discussion. I had missed an important point in how In my case, I was doing: const [queryClient] = useState(getQueryClient()) // ❌ Instead, as the [React Query docs](https://tanstack.com/query/v4/docs/framework/react/guides/ssr) show for hydration setups, it should be: const [queryClient] = useState(() => new QueryClient()) // ✅ Passing the result of the function was causing a new Thanks again for the discussion — closing this now! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Memory leak when instantiating
QueryClient
per call after upgrading to React 19 / Next.js 15 ❓Hi team,
We recently upgraded our project from React 17 to React 19 and Next.js 12 to 15, while still using the Pages Router, not the App Router.
After this upgrade, we encountered a severe memory leak issue in our containerized production environment. The memory usage kept increasing over time, leading to OOMKills.
After investigation, we found that the issue was related to how we were initializing the
QueryClient
from@tanstack/react-query
.🧨 Memory leak version (per-call instantiation)
Used like this in
_app.tsx
:This worked fine before the upgrade, but after moving to React 19 and Next.js 15, it caused memory to grow uncontrollably in production.
✅ Fixed version (singleton instance)
This change resolved the memory issue completely.
📌 My question
useState(getQueryClient())
being called more frequently than expected in newer versions?QueryClient
internal structures (observers, cache, etc)?We’re still using the Pages Router, so this is not related to the App Router’s
cache()
oruse()
behavior.Would appreciate any insights or pointers. Thanks!
🧾 Additional information
React version
17.0.1
19.0.0
Next.js version
12.1.6
15.1.6
React Query version
@tanstack/[email protected]
Routing mode
Still using Pages Router, not App Router.
SSR context
QueryClient is used across both client and SSR (in
_app.tsx
) viauseState(getQueryClient())
.Production environment
22.4.0
Beta Was this translation helpful? Give feedback.
All reactions