electron-userland / electron-json-storage

:package: Easily write and read user settings in Electron apps
1.43k stars 80 forks source link

High CPU and Memory Usage sporadically, but can't find the pattern #206

Open Nantris opened 1 year ago

Nantris commented 1 year ago

You can can see in the image below that the memory usage varied wildly by an entire gigabyte up and down in the course of a few seconds, repeating in a pattern. I confirmed the memory usage is being caused by electron-json-storage, though I'm not exactly sure how. You can also see CPU usage was very high, causing freezing and stuttering, and that the offending calls are from electron-json-storage:

image

The CPU usage and retained memory both appear to occur due to this particular part of the code:

https://github.com/electron-userland/electron-json-storage/blob/034b58beb26909cb56b98c13791b634494551f3a/lib/storage.js#L384-L396

I've seen this issue on rare occasions but when it occurs usually the app crashes. Today I finally was able to diagnose which code was behind the issue at least. 5-10 minutes later, the memory usage and CPU usage resolved (snapshot 1 vs 2) below

image

Specifically the retained memory is a bunch of identical strings. The retainers graphs vary slightly for some of them but the most common one looks like below. In the image below storage.js is the electron-json-storage module and index.js is from mkdirp.

image

@jviotti do you have any thoughts on what might cause this? Perhaps it could be resolved by replacing mkdirp with the now available Node built-in alternative? Or maybe the issue doesn't truly arise from mkdirp but from the way it's called. I'm not that good at interpreting these heap snapshots and they're tough to revisit later since the codepaths they reference are lost even if you save the heap snapshot to review again later.

Nantris commented 1 year ago

One more screenshot before I restart the app and lose some of the debugging data (the code paths.) You can see the blocking time is almost the entire length of the recorded time in the performance tab (at the bottom of the screenshot)

image