Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE: Using node-memwatch to find a memory leak #2253

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 51 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"prepare": "husky install"
},
"dependencies": {
"@airbnb/node-memwatch": "^2.0.0",
"@hashgraph/hedera-local": "^2.19.1",
"@open-rpc/schema-utils-js": "^1.16.1",
"@types/find-config": "^1.0.4",
Expand Down
26 changes: 26 additions & 0 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import path from 'path';
import fs from 'fs';
import { v4 as uuid } from 'uuid';
import { formatRequestIdMessage } from './formatters';
import memwatch from '@airbnb/node-memwatch';

let HeapDiff = memwatch.HeapDiff;

const mainLogger = pino({
name: 'hedera-json-rpc-relay',
Expand Down Expand Up @@ -64,6 +67,29 @@ const methodResponseHistogram = new Histogram({
// set cors
app.getKoaApp().use(cors());

// leak detection middleware

memwatch.on('leak', function (info) {
logger.error('Memory leak detected: --> ' + JSON.stringify(info, null, 2));
});

memwatch.on('stats', function (stats) {
logger.error('Memory stats: --> ' + JSON.stringify(stats, null, 2));
});

app.getKoaApp().use(async (ctx, next) => {
// force garbage collection
memwatch.gc();
logger.info('GC forced');
let hd = new HeapDiff();
await next();
// force garbage collection
memwatch.gc();
logger.info('GC forced');
const diff = hd.end();
logger.info('Heap diff: -->' + JSON.stringify(diff, null, 2));
});

/**
* middleware for non POST request timing
*/
Expand Down