src: assert memory calc for max-old-space-size-percentage · nodejs/node@44a8ecf

@@ -119,14 +119,19 @@ assert(

119119120120

// Validate heap sizes against system memory

121121

const totalMemoryMB = Math.floor(os.totalmem() / 1024 / 1024);

122-

const margin = 10; // 5% margin

122+

const uint64Max = 2 ** 64 - 1;

123+

const constrainedMemory = process.constrainedMemory();

124+

const constrainedMemoryMB = Math.floor(constrainedMemory / 1024 / 1024);

125+

const effectiveMemoryMB =

126+

constrainedMemory > 0 && constrainedMemory !== uint64Max ? constrainedMemoryMB : totalMemoryMB;

127+

const margin = 10; // 10% margin

123128

testPercentages.forEach((percentage) => {

124-

const upperLimit = totalMemoryMB * ((percentage + margin) / 100);

129+

const upperLimit = effectiveMemoryMB * ((percentage + margin) / 100);

125130

assert(

126131

heapSizes[percentage] <= upperLimit,

127132

`Heap size for ${percentage}% (${heapSizes[percentage]} MB) should not exceed upper limit (${upperLimit} MB)`

128133

);

129-

const lowerLimit = totalMemoryMB * ((percentage - margin) / 100);

134+

const lowerLimit = effectiveMemoryMB * ((percentage - margin) / 100);

130135

assert(

131136

heapSizes[percentage] >= lowerLimit,

132137

`Heap size for ${percentage}% (${heapSizes[percentage]} MB) should not be less than lower limit (${lowerLimit} MB)`