@@ -86,6 +86,8 @@ class State {
8686}
8787let state = new State ( ) ;
8888
89+ const reloadMemoryThresholdMb = getEnvironmentVariable ( "SEMMLE_TYPESCRIPT_MEMORY_THRESHOLD" , Number , 1000 ) ;
90+
8991/**
9092 * Debugging method for finding cycles in the TypeScript AST. Should not be used in production.
9193 *
@@ -529,26 +531,36 @@ function getEnvironmentVariable<T>(name: string, parse: (x: string) => T, defaul
529531 return value != null ? parse ( value ) : defaultValue ;
530532}
531533
534+ /**
535+ * Whether the memory usage was last observed to be above the threshold for restarting the TypeScript compiler.
536+ */
537+ let isAboveReloadThreshold = false ;
538+
539+ /**
540+ * If memory usage has moved above a the threshold, reboot the TypeScript compiler instance.
541+ */
542+ function checkMemoryUsage ( ) {
543+ let bytesUsed = process . memoryUsage ( ) . heapUsed ;
544+ let megabytesUsed = bytesUsed / 1000000 ;
545+ if ( ! isAboveReloadThreshold && megabytesUsed > reloadMemoryThresholdMb && state . project != null ) {
546+ console . warn ( 'Restarting TypeScript compiler due to memory usage' ) ;
547+ state . project . reload ( ) ;
548+ isAboveReloadThreshold = true ;
549+ }
550+ else if ( isAboveReloadThreshold && megabytesUsed < reloadMemoryThresholdMb ) {
551+ isAboveReloadThreshold = false ;
552+ }
553+ }
554+
532555function runReadLineInterface ( ) {
533556 reset ( ) ;
534- let reloadMemoryThresholdMb = getEnvironmentVariable ( "SEMMLE_TYPESCRIPT_MEMORY_THRESHOLD" , Number , 1000 ) ;
535- let isAboveReloadThreshold = false ;
536557 let rl = readline . createInterface ( { input : process . stdin , output : process . stdout } ) ;
537558 rl . on ( "line" , ( line : string ) => {
538559 let req : Command = JSON . parse ( line ) ;
539560 switch ( req . command ) {
540561 case "parse" :
541562 handleParseCommand ( req ) ;
542- // If memory usage has moved above the threshold, reboot the TypeScript compiler instance.
543- let bytesUsed = process . memoryUsage ( ) . heapUsed ;
544- let megabytesUsed = bytesUsed / 1000000 ;
545- if ( ! isAboveReloadThreshold && megabytesUsed > reloadMemoryThresholdMb && state . project != null ) {
546- console . warn ( 'Restarting TypeScript compiler due to memory usage' ) ;
547- state . project . reload ( ) ;
548- isAboveReloadThreshold = true ;
549- } else if ( isAboveReloadThreshold && megabytesUsed < reloadMemoryThresholdMb ) {
550- isAboveReloadThreshold = false ;
551- }
563+ checkMemoryUsage ( ) ;
552564 break ;
553565 case "open-project" :
554566 handleOpenProjectCommand ( req ) ;
0 commit comments