Skip to content

Commit

Permalink
avoid leaking memory after the compiler has been closed
Browse files Browse the repository at this point in the history
clear cache on shutdown
  • Loading branch information
sokra committed Apr 1, 2021
1 parent 87b67a9 commit 8074127
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ ${other}`);
close(callback) {
this.hooks.shutdown.callAsync(err => {
if (err) return callback(err);
// Get rid of reference to last compilation to avoid leaking memory
// We can't run this._cleanupLastCompilation() as the Stats to this compilation
// might be still in use. We try to get rid for the reference to the cache instead.
this._lastCompilation = undefined;
this.cache.shutdown(callback);
});
}
Expand Down
44 changes: 44 additions & 0 deletions lib/FileSystemInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ class SnapshotOptimization {
} times referenced)`;
}

clear() {
this._map.clear();
this._statItemsShared = 0;
this._statItemsUnshared = 0;
this._statSharedSnapshots = 0;
this._statReusedSharedSnapshots = 0;
}

storeUnsharedSnapshot(snapshot, locations) {
if (locations === undefined) return;
const optimizationEntry = {
Expand Down Expand Up @@ -984,6 +992,42 @@ class FileSystemInfo {
}
}

clear() {
this._remainingLogs = this.logger ? 40 : 0;
if (this._loggedPaths !== undefined) this._loggedPaths.clear();

this._snapshotCache = new WeakMap();
this._fileTimestampsOptimization.clear();
this._fileHashesOptimization.clear();
this._fileTshsOptimization.clear();
this._contextTimestampsOptimization.clear();
this._contextHashesOptimization.clear();
this._contextTshsOptimization.clear();
this._missingExistenceOptimization.clear();
this._managedItemInfoOptimization.clear();
this._managedFilesOptimization.clear();
this._managedContextsOptimization.clear();
this._managedMissingOptimization.clear();
this._fileTimestamps.clear();
this._fileHashes.clear();
this._fileTshs.clear();
this._contextTimestamps.clear();
this._contextHashes.clear();
this._contextTshs.clear();
this._managedItems.clear();
this._managedItems.clear();

this._cachedDeprecatedFileTimestamps = undefined;
this._cachedDeprecatedContextTimestamps = undefined;

this._statCreatedSnapshots = 0;
this._statTestedSnapshotsCached = 0;
this._statTestedSnapshotsNotCached = 0;
this._statTestedChildrenCached = 0;
this._statTestedChildrenNotCached = 0;
this._statTestedEntries = 0;
}

/**
* @param {Map<string, FileSystemInfoEntry | "ignore" | null>} map timestamps
* @returns {void}
Expand Down
7 changes: 5 additions & 2 deletions lib/cache/IdleFileCachePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class IdleFileCachePlugin {
* @returns {void}
*/
apply(compiler) {
const strategy = this.strategy;
let strategy = this.strategy;
const idleTimeout = this.idleTimeout;
const idleTimeoutForInitialStore = Math.min(
idleTimeout,
Expand Down Expand Up @@ -101,7 +101,10 @@ class IdleFileCachePlugin {
reportProgress(1, `stored`);
});
}
return currentIdlePromise;
return currentIdlePromise.then(() => {
// Reset strategy
if (strategy.clear) strategy.clear();
});
}
);

Expand Down
6 changes: 6 additions & 0 deletions lib/cache/MemoryCachePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class MemoryCachePlugin {
});
}
);
compiler.cache.hooks.shutdown.tap(
{ name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY },
() => {
cache.clear();
}
);
}
}
module.exports = MemoryCachePlugin;
20 changes: 18 additions & 2 deletions lib/cache/PackFileCacheStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,11 @@ class PackFileCacheStrategy {
this.packPromise = this._openPack();
}

_getPack() {
if (this.packPromise === undefined) this.packPromise = this._openPack();
return this.packPromise;
}

/**
* @returns {Promise<Pack>} the pack
*/
Expand Down Expand Up @@ -925,7 +930,7 @@ class PackFileCacheStrategy {
* @returns {Promise<void>} promise
*/
store(identifier, etag, data) {
return this.packPromise.then(pack => {
return this._getPack().then(pack => {
pack.set(identifier, etag === null ? null : etag.toString(), data);
});
}
Expand All @@ -936,7 +941,7 @@ class PackFileCacheStrategy {
* @returns {Promise<any>} promise to the cached content
*/
restore(identifier, etag) {
return this.packPromise
return this._getPack()
.then(pack =>
pack.get(identifier, etag === null ? null : etag.toString())
)
Expand All @@ -955,6 +960,7 @@ class PackFileCacheStrategy {
}

afterAllStored() {
if (this.packPromise === undefined) return Promise.resolve();
const reportProgress = ProgressPlugin.getReporter(this.compiler);
return this.packPromise
.then(pack => {
Expand Down Expand Up @@ -1114,6 +1120,16 @@ class PackFileCacheStrategy {
this.logger.debug(err.stack);
});
}

clear() {
this.fileSystemInfo.clear();
this.buildDependencies.clear();
this.newBuildDependencies.clear();
this.resolveBuildDependenciesSnapshot = undefined;
this.resolveResults = undefined;
this.buildSnapshot = undefined;
this.packPromise = undefined;
}
}

module.exports = PackFileCacheStrategy;
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3760,6 +3760,7 @@ declare abstract class FileSystemInfo {
immutablePaths: string[];
immutablePathsWithSlash: string[];
logStatistics(): void;
clear(): void;
addFileTimestamps(
map: Map<string, null | FileSystemInfoEntry | "ignore">
): void;
Expand Down

0 comments on commit 8074127

Please sign in to comment.