Skip to content

Commit 492ebd5

Browse files
committed
BC-5586 Investigate possibilities to detect OutOfMemory errors on production
- Fix heap size calculation, at is was off a few percentage. Now use process API and only use gc-stats for knowing when a full GC was done
1 parent 8d1802f commit 492ebd5

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
03-10-2017 - Paul Rütter
2+
- 1.0.5 - Change heap calculation.
3+
14
03-10-2017 - Paul Rütter
25
- 1.0.4 - Add error handler in case the calling process is not running anymore. Also, block execution a while to allow heapdump to be created.
36

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Just add the following snippet to your node process.
2929

3030
```javascript
3131
require("node-oom-heapdump")({
32-
threshold: 75,
32+
threshold: 90,
3333
path: "./my_heapdump"
3434
});
3535
```
@@ -46,7 +46,7 @@ These might impact performance though.
4646

4747
# Options
4848
* heapdumpOnOOM - boolean whether to create a heapdump when an out of memory occurs. Default true.
49-
* threshold - integer between 0 and 100 (%) which determines when to make the heapdump. When the used heapSize exceeds the threshold, a heapdump is made.
49+
* threshold - integer between 0 and 100 (%) which determines when to make the heapdump. When the used heapSize exceeds the threshold, a heapdump is made. This value can be tuned depending on your configuration; if memory usage is very volatile, a lower value might make more sense. Default is 90.
5050
* path - the path where the heapdump ends up when an out of memory error occurs.
5151
* port - optionally, the alternative DevTools protocol port. Defaults to 9229. Should map on the port given to the --inspect arg.
5252

lib/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class NodeOomHeapDumpImpl {
1818
gcStats.on('stats', (stats) => {
1919
// gctype 2 is a Full GC (Mark/Sweep/Compact)
2020
if (stats.gctype === 2 && !this._busy) {
21-
let heapSizeUsedPercentage = (parseInt(stats.after.totalHeapSize) / parseInt(stats.after.heapSizeLimit)) * 100;
22-
if (heapSizeUsedPercentage > this._opts.threshold) {
23-
24-
// this is a full GC and the used heap size is using more than 80% of the assigned heap space limit
25-
console.warn('OoM is imminent: Full GC (Mark/Sweep/Compact) complete and still more than %s% (%s%) of the heap is used. Creating heapdump now. GC stats: ', this._opts.threshold, Math.round(heapSizeUsedPercentage), stats);
21+
let memoryUsage = process.memoryUsage();
22+
var memoryUsagePercentage = Math.round((memoryUsage.heapUsed / memoryUsage.heapTotal) * 100);
23+
if (memoryUsagePercentage > this._opts.threshold) {
24+
// this is a full GC and the used heap size is using more than x% of the assigned heap space limit
25+
console.warn('OoM is imminent: Full GC (Mark/Sweep/Compact) complete and still more than %s% (%s%) of the heap is used. Creating heapdump now. GC stats: ', this._opts.threshold, Math.round(memoryUsagePercentage), stats);
2626

2727
this.createHeapSnapshot(path.resolve('./' + this._opts.path + '.heapsnapshot'), "OoM");
2828

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-oom-heapdump",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Create a V8 heap snapshot right before an \"Out of Memory\" error occurs, or create a heap snapshot on request.",
55
"main": "index.js",
66
"scripts": {

tests/oom_app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let oom = require("../index.js")({
2-
threshold: 75,
2+
threshold: 90,
33
path: "../my_heapdump",
44
heapdumpOnOOM: true
55
});

0 commit comments

Comments
 (0)