Skip to content

Commit eeaf8df

Browse files
committed
Tided up and tested the code.
1 parent e1c784c commit eeaf8df

File tree

4 files changed

+90
-181
lines changed

4 files changed

+90
-181
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
# nodejs-memory-test
2-
Code to test how much memory is available with Node.js
2+
3+
A test program to estimate how much heap memory is available to a Node.js application.
4+
5+
This example code accompanies the book [Data Wrangling with JavaScript](http://bit.ly/2t2cJu2).
6+
7+
If you're a JavaScript developer, you already know that working with data is a big deal. Why let the Python and R coders get all the glory? JavaScript isn't just good at data visualization, you can move your entire data wrangling pipeline to JavaScript and work more effectively. [Data Wrangling with JavaScript](http://bit.ly/2t2cJu2) teaches you core data munging techniques in JavaScript, along with many libraries and tools that will make your data tasks even easier.
8+
9+
## Setup
10+
11+
Clone the repo to your local hard drive and change directory to the repo.
12+
13+
No dependencies are required.
14+
15+
## Run
16+
17+
To determine the largest amount of memory that can be allocated in Node.js run:
18+
19+
node index.js
20+
21+
Let this run for a while and it will eventually abort with a fatal out of memory error like this:
22+
23+
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
24+
25+
The last line of text printed tells you the total amount of heap memory available to your application under Node.js.
26+
27+
Now try increasing heap memory, add the '--max-old-space-size' command line parameter as follows and run it again:
28+
29+
node --max-old-space-size=6000 index.js
30+
31+
You should now have much more heap memory to play with!

index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Small program to test the maximum amount of allocations in multiple blocks.
3+
// This script searches for the largest allocation amount.
4+
//
5+
6+
//
7+
// Allocate a certain size to test if it can be done.
8+
//
9+
function alloc (size) {
10+
const numbers = size / 8;
11+
const arr = []
12+
arr.length = numbers; // Simulate allocation of 'size' bytes.
13+
for (let i = 0; i < numbers; i++) {
14+
arr[i] = i;
15+
}
16+
return arr;
17+
};
18+
19+
//
20+
// Keep allocations referenced so they aren't garbage collected.
21+
//
22+
const allocations = [];
23+
24+
//
25+
// Allocate successively larger sizes, doubling each time until we hit the limit.
26+
//
27+
function allocToMax () {
28+
29+
console.log("Start");
30+
31+
const field = 'heapUsed';
32+
const mu = process.memoryUsage();
33+
console.log(mu);
34+
const gbStart = mu[field] / 1024 / 1024 / 1024;
35+
console.log(`Start ${Math.round(gbStart * 100) / 100} GB`);
36+
37+
let allocationStep = 100 * 1024;
38+
39+
while (true) {
40+
// Allocate memory.
41+
const allocation = alloc(allocationStep);
42+
43+
// Allocate and keep a reference so the allocated memory isn't garbage collected.
44+
allocations.push(allocation);
45+
46+
// Check how much memory is now allocated.
47+
const mu = process.memoryUsage();
48+
const mbNow = mu[field] / 1024 / 1024 / 1024;
49+
//console.log(`Total allocated ${Math.round(mbNow * 100) / 100} GB`);
50+
console.log(`Allocated since start ${Math.round((mbNow - gbStart) * 100) / 100} GB`);
51+
52+
// Infinite loop, never get here.
53+
}
54+
55+
// Infinite loop, never get here.
56+
};
57+
58+
allocToMax();
59+
60+
// Infinite loop, never get here.

test-max-alloc.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

test-single-alloc-limit.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)