diff --git a/packages/pyright-internal/src/analyzer/program.ts b/packages/pyright-internal/src/analyzer/program.ts index 7a57db7f533..5df2c96949f 100644 --- a/packages/pyright-internal/src/analyzer/program.ts +++ b/packages/pyright-internal/src/analyzer/program.ts @@ -8,6 +8,7 @@ * and all of their recursive imports. */ +import { getHeapStatistics } from 'v8'; import { CancellationToken, CompletionItem, DocumentSymbol } from 'vscode-languageserver'; import { TextDocumentContentChangeEvent } from 'vscode-languageserver-textdocument'; import { @@ -2176,13 +2177,13 @@ export class Program { // Don't bother doing this until we hit this point because the heap usage may not // drop immediately after we empty the cache due to garbage collection timing. if (typeCacheSize > 750000 || this._parsedFileCount > 1000) { - const memoryUsage = process.memoryUsage(); + const memoryUsage = getHeapStatistics(); // If we use more than 90% of the available heap size, avoid a crash // by emptying the type cache. - if (memoryUsage.heapUsed > memoryUsage.rss * 0.9) { - const heapSizeInMb = Math.round(memoryUsage.rss / (1024 * 1024)); - const heapUsageInMb = Math.round(memoryUsage.heapUsed / (1024 * 1024)); + if (memoryUsage.used_heap_size > memoryUsage.heap_size_limit * 0.9) { + const heapSizeInMb = Math.round(memoryUsage.heap_size_limit / (1024 * 1024)); + const heapUsageInMb = Math.round(memoryUsage.used_heap_size / (1024 * 1024)); this._console.info( `Emptying type cache to avoid heap overflow. Used ${heapUsageInMb}MB out of ${heapSizeInMb}MB`