@@ -299,21 +299,34 @@ export class Snapshotter {
299299 protected _snapshotMemory ( systemInfo : SystemInfo ) {
300300 const createdAt = systemInfo . createdAt ? new Date ( systemInfo . createdAt ) : new Date ( ) ;
301301 this . _pruneSnapshots ( this . memorySnapshots , createdAt ) ;
302- const { memCurrentBytes } = systemInfo ;
302+ const { memCurrentBytes, memTotalBytes } = systemInfo ;
303+
304+ let maxMemoryBytes ;
305+ if ( this . maxMemoryBytes > 0 && this . maxMemoryBytes <= 1 ) {
306+ // Treat it as ratio of the total actual memory
307+ if ( ! memTotalBytes )
308+ throw new Error (
309+ 'Incorrect memory info provided by the systemInfo event, total memory is required to be able to use maxMemoryBytes as a ratio.' ,
310+ ) ;
311+ maxMemoryBytes = this . maxMemoryBytes ! * memTotalBytes ;
312+ } else {
313+ maxMemoryBytes = this . maxMemoryBytes ! ;
314+ }
315+
303316 const snapshot : MemorySnapshot = {
304317 createdAt,
305- isOverloaded : memCurrentBytes ! / this . maxMemoryBytes ! > this . maxUsedMemoryRatio ,
318+ isOverloaded : memCurrentBytes ! / maxMemoryBytes > this . maxUsedMemoryRatio ,
306319 usedBytes : memCurrentBytes ,
307320 } ;
308321
309322 this . memorySnapshots . push ( snapshot ) ;
310- this . _memoryOverloadWarning ( systemInfo ) ;
323+ this . _memoryOverloadWarning ( systemInfo , maxMemoryBytes ) ;
311324 }
312325
313326 /**
314327 * Checks for critical memory overload and logs it to the console.
315328 */
316- protected _memoryOverloadWarning ( systemInfo : SystemInfo ) {
329+ protected _memoryOverloadWarning ( systemInfo : SystemInfo , maxMemoryBytes : number ) {
317330 const { memCurrentBytes } = systemInfo ;
318331 const createdAt = systemInfo . createdAt ? new Date ( systemInfo . createdAt ) : new Date ( ) ;
319332 if (
@@ -322,18 +335,18 @@ export class Snapshotter {
322335 )
323336 return ;
324337
325- const maxDesiredMemoryBytes = this . maxUsedMemoryRatio * this . maxMemoryBytes ! ;
326- const reserveMemory = this . maxMemoryBytes ! * ( 1 - this . maxUsedMemoryRatio ) * RESERVE_MEMORY_RATIO ;
338+ const maxDesiredMemoryBytes = this . maxUsedMemoryRatio * maxMemoryBytes ;
339+ const reserveMemory = maxMemoryBytes * ( 1 - this . maxUsedMemoryRatio ) * RESERVE_MEMORY_RATIO ;
327340 const criticalOverloadBytes = maxDesiredMemoryBytes + reserveMemory ;
328341 const isCriticalOverload = memCurrentBytes ! > criticalOverloadBytes ;
329342
330343 if ( isCriticalOverload ) {
331- const usedPercentage = Math . round ( ( memCurrentBytes ! / this . maxMemoryBytes ! ) * 100 ) ;
344+ const usedPercentage = Math . round ( ( memCurrentBytes ! / maxMemoryBytes ) * 100 ) ;
332345 const toMb = ( bytes : number ) => Math . round ( bytes / 1024 ** 2 ) ;
333346 this . log . warning (
334347 'Memory is critically overloaded. ' +
335348 `Using ${ toMb ( memCurrentBytes ! ) } MB of ${ toMb (
336- this . maxMemoryBytes ! ,
349+ maxMemoryBytes ,
337350 ) } MB (${ usedPercentage } %). Consider increasing available memory.`,
338351 ) ;
339352 this . lastLoggedCriticalMemoryOverloadAt = createdAt ;
0 commit comments