├── .gitignore ├── package.json ├── README.md ├── LICENSE.md └── app.mjs /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "max-mem", 3 | "version": "1.0.0", 4 | "description": "Measure maximum memory usage of a command", 5 | "main": "app.mjs", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "bin": { 10 | "max-mem": "./app.mjs" 11 | }, 12 | "type": "module", 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/apostrophecms/max-mem.git" 16 | }, 17 | "author": "Apostrophe Technologies", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/apostrophecms/max-mem/issues" 21 | }, 22 | "homepage": "https://github.com/apostrophecms/max-mem#readme" 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # max-mem 2 | 3 | ## Purpose 4 | 5 | Measures the maximum memory usage of any command. `max-mem` checks the memory usage of the command every 100 milliseconds and reports the peak "resident set size" of the command when it exits, including any child processes. This is helpful in tracking down the cause of out-of-memory errors in Webpack builds and other expensive operations. 6 | 7 | `max-mem` does for memory what `time` does for execution time. 8 | 9 | ## Install 10 | 11 | ``` 12 | npm install -g max-mem 13 | ``` 14 | 15 | ## Usage 16 | 17 | ``` 18 | max-mem npm run build 19 | 20 | [Regular output appears here] 21 | 22 | Max memory usage: 800MB 23 | ``` 24 | 25 | ## Credits 26 | 27 | `max-mem` was created to facilitate our work on [ApostropheCMS](https://apostrophecms.com). 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2022 Apostrophe Technologies, Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /app.mjs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import { spawn, execSync } from 'child_process'; 4 | 5 | const argv = process.argv.slice(2); 6 | 7 | if (!argv[0]) { 8 | console.error(`Usage: max-mem `); 9 | process.exit(1); 10 | } 11 | 12 | const processes = new Map(); 13 | 14 | go().then(() => { 15 | process.exit(0); 16 | }).catch((e) => { 17 | console.error(e); 18 | process.exit(1); 19 | }); 20 | 21 | async function go() { 22 | let max = 0; 23 | const child = spawn(argv[0], argv.slice(1), { 24 | shell: true, 25 | stdio: 'inherit' 26 | }); 27 | const pid = child.pid; 28 | 29 | child.on('close', (status) => { 30 | console.log(`Max memory usage: ${Math.ceil(max / 1024)}MB`); 31 | process.exit(status); 32 | }); 33 | child.on('error', e => { 34 | // Spawn failed 35 | console.error(e); 36 | process.exit(1); 37 | }); 38 | 39 | process.on('SIGINT', function() { 40 | child.kill('SIGINT'); 41 | }); 42 | process.on('SIGQUIT', function() { 43 | child.kill('SIGQUIT'); 44 | }); 45 | process.on('SIGTERM', function() { 46 | child.kill('SIGTERM'); 47 | }); 48 | 49 | while (true) { 50 | const output = execSync('ps -o pid,ppid,rss', { encoding: 'utf8' }); 51 | const lines = output.split('\n'); 52 | for (const line of lines) { 53 | const fields = line.trim().split(/\s+/); 54 | if (fields[0] === 'PID') { 55 | continue; 56 | } 57 | const childPid = parseInt(fields[0], 10); 58 | if (isNaN(childPid)) { 59 | continue; 60 | } 61 | const ppid = parseInt(fields[1], 10); 62 | processes.set(childPid, { ppid, rss: parseInt(fields[2], 10) }); 63 | } 64 | for (const [ pid, info ] of processes.entries()) { 65 | const parent = processes.get(info.ppid); 66 | if (parent) { 67 | parent.children = parent.children || []; 68 | parent.children.push(pid); 69 | } 70 | } 71 | for (const [ pid, info ] of processes.entries()) { 72 | recursiveRss(pid, info); 73 | } 74 | max = Math.max(max, processes.get(pid).recursiveRss); 75 | await sleep(100); 76 | } 77 | } 78 | 79 | function recursiveRss(pid, info) { 80 | if (info.recursiveRss !== undefined) { 81 | return info.recursiveRss; 82 | } 83 | if (info.children) { 84 | info.recursiveRss = info.rss + info.children.reduce((sum, childPid) => { 85 | return sum + recursiveRss(childPid, processes.get(childPid)); 86 | }, 0); 87 | } else { 88 | info.recursiveRss = info.rss; 89 | } 90 | return info.recursiveRss; 91 | } 92 | 93 | function sleep(ms) { 94 | return new Promise((resolve) => { 95 | setTimeout(resolve, ms); 96 | }); 97 | } 98 | --------------------------------------------------------------------------------