Skip to content

Instantly share code, notes, and snippets.

@l2ysho
Last active January 25, 2021 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l2ysho/e9af327d431a2986ed2ab0ddd5f7c6bd to your computer and use it in GitHub Desktop.
Save l2ysho/e9af327d431a2986ed2ab0ddd5f7c6bd to your computer and use it in GitHub Desktop.
Inspect Memory Usage in Node.js
import Joi from '@hapi/joi'
import { Request, Response, NextFunction } from 'express'
export const schema = Joi.object().keys({
body: Joi.object(),
query: Joi.object(),
params: Joi.object()
})
const formatMemmoryUsage = (data: any) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`
export const workflow = async (req: Request, res: Response, next: NextFunction) => {
try {
const memoryData = process.memoryUsage()
const result = {
name: process.env.npm_package_name,
version: process.env.npm_package_version,
env: process.env.NODE_ENV,
host: process.env.HOST,
memmoryUsage: {
rss: `${formatMemmoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
heapTotal: `${formatMemmoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
heapUsed: `${formatMemmoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
external: `${formatMemmoryUsage(memoryData.external)} -> V8 external memory`,
}
}
return res.json(result)
} catch (err) {
return next(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment