Skip to content

Commit

Permalink
Fixed bug in logic that determines whether to empty the in-memory typ…
Browse files Browse the repository at this point in the history
…e cache if it has the potential to overflow the heap.
  • Loading branch information
msfterictraut committed Mar 27, 2022
1 parent 368dc5e commit 43b7459
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/pyright-internal/src/analyzer/program.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 43b7459

Please sign in to comment.