Open RezaAb opened 1 year ago
Try increasing the heap memory available to your app by using the --max-old-space-size flag, like node --max-old-space-size=4096 yourScript.js
. The value (4096) is in megabytes.
@miktam Thanks for your response.
My current heap size is big enough.
heap_size_limit: 4345298944,
My object size was not around this value. (Maybe around 100Mb)
I didn’t have any problems like this until I used the package.
@RezaAb, thank you for the update!
Heavily nested arrays will cause the module to throw, as the engine can not allocate memory for a huge data structure with the nested arrays.
This code crashes tests:
describe('Testing with Large Objects - Arrays of Arrays with Huge Strings', () => {
it.only('should not crash the app when handling large nested arrays with huge strings', function () {
// Optional: Increase the timeout for this test as it will likely take a while.timeout(30000)
// Create large nested arrays with huge strings
const largeNestedArray = []
const hugeString = 'x'.repeat(1000) // A string with 10,000 characters
for (let i = 0; i < 500; i++) {
let currentArray = largeNestedArray
for (let j = 0; j < 500; j++) {
const newArray = [hugeString]
currentArray.push(newArray)
currentArray = newArray
}
}
// Test that this doesn't crash the app
// Note: Depending on the machine this test is run on, this might still crash
sizeof(largeNestedArray).should.be.equal(-1)
})
})
Regarding remediation, the only way I see at the moment is to look at the object first and nicely return -1 when the object's depth exceeds a certain threshold. At least it will not crash the invoking app.
This does not feel like an ideal solution. Are there any better ways?
I got this error in my app with a large object that includes arrays of arrays. The error closed the app even by error handling.
nodejs version: 16.20