Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the process cache to reduce the memory for asar file #36600

Merged
merged 2 commits into from Dec 14, 2022

Conversation

xuwanghu
Copy link
Contributor

@xuwanghu xuwanghu commented Dec 7, 2022

Description of Change

The memory leaks at /lib/asar/fs-wrapper.ts, const cachedArchives = new Map<string, NodeJS.AsarArchive>() Whenever a new worker is created, it will have an instance of this map. The contents of this map will not be cleaned up immediately.
And there is a process-level cache at /shell/common/asar/asar_util.cc, static base::NoDestructor s_archive_map
In summary, I decide to use this process-level cache to save the asar cache instead of creating a new one for each worker

Resolves #36597

Checklist

  • PR description included and stakeholders cc'd
  • npm test passes

Release Notes

Notes: use the process cache to reduce the memory for asar file

@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Dec 7, 2022
@BlackHole1
Copy link
Member

It appears the relevant PR is #33216
/cc @indutny

@RaisinTen
Copy link
Member

The change makes sense to me. Is there any chance you could add a test? Maybe by comparing the value of process.memoryUsage().heapUsed before and after the memory leak?

@RaisinTen
Copy link
Member

@zcbenz could you also take a look?

@indutny
Copy link
Contributor

indutny commented Dec 8, 2022

Nice catch!

@xuwanghu
Copy link
Contributor Author

xuwanghu commented Dec 8, 2022

The change makes sense to me. Is there any chance you could add a test? Maybe by comparing the value of process.memoryUsage().heapUsed before and after the memory leak?

I have test it in our app which is build with electron, the memory usage gap is determined by the size of the ASAR file and the number of workers used in the program. The orange line is on electron 17.0.0 and the blue line is after my repair.
image

@RaisinTen
Copy link
Member

Right, I mean adding a test to spec to make sure that future changes don't regress on this test case.

@MarshallOfSound
Copy link
Member

Haven't looked into it but are we sure that Archive and GetOrCreateAsarArchive are thread safe? Maybe we should look into cleaning up archives on thread destruction rather than re-using archives across threads if not.

@xuwanghu
Copy link
Contributor Author

xuwanghu commented Dec 8, 2022

Haven't looked into it but are we sure that Archive and GetOrCreateAsarArchive are thread safe? Maybe we should look into cleaning up archives on thread destruction rather than re-using archives across threads if not.

I refer to #29292 , and my conclusion is that Archive and GetOrCreateAsarArchive are thread-safe

@xuwanghu
Copy link
Contributor Author

xuwanghu commented Dec 8, 2022

Right, I mean adding a test to spec to make sure that future changes don't regress on this test case.

I will learn and try to add a test to the electron, but what about the data that determines a failed test? 1kb or 1mb? It is not easy to confirm the criteria for determining test failure. It is also difficult to confirm the time when the memory reclamation is completed when the worker exits

@RaisinTen
Copy link
Member

Hmm, I'm not sure how this can be reproduced consistently. Maybe you would need to experiment with the values a bit? If it's hard to consistently reproduce, I guess it's okay to land this without a test too.

shell/common/api/electron_api_asar.cc Outdated Show resolved Hide resolved
@nornagon
Copy link
Member

asar::GetOrCreateAsarArchive is guarded by a lock, so I think this is safe:

std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
base::AutoLock auto_lock(GetArchiveCacheLock());

@nornagon nornagon added target/21-x-y PR should also be added to the "21-x-y" branch. target/22-x-y PR should also be added to the "22-x-y" branch. target/23-x-y PR should also be added to the "23-x-y" branch. semver/patch backwards-compatible bug fixes labels Dec 13, 2022
@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Dec 13, 2022
Copy link
Member

@RaisinTen RaisinTen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@jkleinsc jkleinsc merged commit f72e655 into electron:main Dec 14, 2022
@welcome
Copy link

welcome bot commented Dec 14, 2022

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk
Copy link

release-clerk bot commented Dec 14, 2022

Release Notes Persisted

use the process cache to reduce the memory for asar file

@trop
Copy link
Contributor

trop bot commented Dec 14, 2022

I have automatically backported this PR to "21-x-y", please check out #36663

@trop trop bot added in-flight/21-x-y and removed target/21-x-y PR should also be added to the "21-x-y" branch. labels Dec 14, 2022
@trop
Copy link
Contributor

trop bot commented Dec 14, 2022

I have automatically backported this PR to "22-x-y", please check out #36664

@trop
Copy link
Contributor

trop bot commented Dec 14, 2022

I have automatically backported this PR to "23-x-y", please check out #36665

@trop trop bot added in-flight/22-x-y merged/23-x-y PR was merged to the "23-x-y" branch. and removed target/22-x-y PR should also be added to the "22-x-y" branch. target/23-x-y PR should also be added to the "23-x-y" branch. in-flight/23-x-y labels Dec 14, 2022
@trop trop bot added merged/21-x-y PR was merged to the "21-x-y" branch. merged/22-x-y PR was merged to the "22-x-y" branch. and removed in-flight/21-x-y labels Jan 13, 2023
khalwa pushed a commit to solarwindscloud/electron that referenced this pull request Feb 22, 2023
…on#36600)

* fix: use the process cache to reduce the memory for asar file

* Update shell/common/api/electron_api_asar.cc

Co-authored-by: webster.xu <webster.xu@ringcentral.com>
Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged/21-x-y PR was merged to the "21-x-y" branch. merged/22-x-y PR was merged to the "22-x-y" branch. merged/23-x-y PR was merged to the "23-x-y" branch. semver/patch backwards-compatible bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: memory leak when a worker accesses a file in the asar archive package for the first time
8 participants