# What is the best way to trace a memory leak in my custom rpcs in the typescript nakama runtime?

**URL:** <https://forum.heroiclabs.com/t/what-is-the-best-way-to-trace-a-memory-leak-in-my-custom-rpcs-in-the-typescript-nakama-runtime/5350>\
**Category:** Runtime Framework\
**Tags:** server-framework, typescript\
**Created:** [June 12, 2024, 2:32pm UTC](https://forum.heroiclabs.com/t/what-is-the-best-way-to-trace-a-memory-leak-in-my-custom-rpcs-in-the-typescript-nakama-runtime/5350 "2024-06-12T14:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukep](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@lukep](https://forum.heroiclabs.com/u/lukep)\
**Post date:** [June 12, 2024, 2:32pm UTC](https://forum.heroiclabs.com/t/what-is-the-best-way-to-trace-a-memory-leak-in-my-custom-rpcs-in-the-typescript-nakama-runtime/5350/1 "2024-06-12T14:32:12Z")

</div>

I have a series of custom RPC’s in my nakama typescript multiplayer server. I’ve noticed that when starting and completing a match my memory usage on my machine that is running nakama increases by ~1% and doesn’t go back down after the match finishes.

I’m wondering where my leak is and how to debug. I started using the pprof endpoint for memory (/pprof/heap) but the flame graph doesn’t provide particularly useful information on which functions are using the most (top says [github.com/dop251/goja.(\*baseObject).put](http://github.com/dop251/goja.(*baseObject).put)).

Do you have any suggestions to get more specific information to find the leak?

Let me know - thanks!

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [June 12, 2024, 4:36pm UTC](https://forum.heroiclabs.com/t/what-is-the-best-way-to-trace-a-memory-leak-in-my-custom-rpcs-in-the-typescript-nakama-runtime/5350/2 "2024-06-12T16:36:02Z")

</div>

Hello @lukep,

Unfortunately it is not possible to take insightful pprofs of memory usage in the JS runtime, but I can say that we have many customers using the JS runtime in production with complex games without issues and we’re not aware of any memory leaks.

Please consider the following points:

- The JS VM pool for RPC requests may instantiate new VMs as needed, depending on these configs: [Configuration - Heroic Labs Documentation](https://heroiclabs.com/docs/nakama/getting-started/configuration/#runtime.js_max_count)

- Avoid modifying JS global state - the VMs are reused which means that changes to global state will only reflect in the current VM and not others, which can cause correctness issues if you rely on that state.

- The Go GC may not release all memory back to the OS immediately, whether the memory is released back to the OS depends on a number of variables, so this may play a role on what you’re observing too.

---

<div class="post-metadata">

**Author:** ![lukep](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@lukep](https://forum.heroiclabs.com/u/lukep)\
**Post date:** [June 13, 2024, 5:02pm UTC](https://forum.heroiclabs.com/t/what-is-the-best-way-to-trace-a-memory-leak-in-my-custom-rpcs-in-the-typescript-nakama-runtime/5350/3 "2024-06-13T17:02:14Z")

</div>

Thanks for the quick response @sesposito! I’ll try to review what we have and see if we should adapt anything.
