DavidWells / DavidWells/analytics

trackEvent's state.history grows forever, takes up memory

Open
#358 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
2.7k
Forks
267
PR merge metrics
No merged PRs in 30d

Description

Hi, thanks for analyticsjs!

When using it on in a long-running server calling trackEvent after every request, the memory usage of the process grows forever. This shows the process's growing memory use until OOM killed. (On ~1/31 we deployed the fix below).

Screenshot 2023-02-02 at 12 47 15 PM

We found that the state.history variable of the trackReducer keeps a log of every single trackEvent sent. Adding this line after our trackEvent() call made the memory issue go away:

  analytics.getState("track").history = [];

When reading the code, I can't seem to find where state.history is used, or if it's for external code like plugins to read.
In our use case we never read from it, so disabling it would be best, or keeping it capped at a small N if it's used for retries.

I noticed the trackEvent history code has the comment //Todo prevent LARGE arrays. Could we add some kind of config to set a max history length, or to turn it off entirely? Or hard code a limit like 10k?

This might also help memory usage for browser users of analyticsjs with long running sessions.

Would you take a PR to fix this? Which approach would you prefer?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/analytics-core/src/modules/track.js, at the trackReducer and the history code linked in the issue. Check whether state.history is used elsewhere or exposed to plugins, then define the supported behavior for limiting or disabling it. Done means long-running trackEvent usage no longer grows history without bound while any required history behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.