UNPKG

@remotion/renderer

Version:

Render Remotion videos using Node.js or Bun

59 lines (58 loc) 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAvailableMemory = exports.maxLambdaMemory = void 0; const node_fs_1 = require("node:fs"); const node_os_1 = require("node:os"); const logger_1 = require("./logger"); const getFreeMemoryFromProcMeminfo = () => { const data = (0, node_fs_1.readFileSync)('/proc/meminfo', 'utf-8'); // Split the file by lines and find the line with MemFree const lines = data.split('\n'); const memAvailableLine = lines.find((line) => line.startsWith('MemAvailable')); // If we couldn't find MemAvailable, return an error if (!memAvailableLine) { throw new Error('MemAvailable not found in /proc/meminfo'); } // Extract the value and unit from the line const matches = memAvailableLine.match(/(\d+)\s+(\w+)/); if (!matches || matches.length !== 3) { throw new Error('Failed to parse MemAvailable value'); } const value = parseInt(matches[1], 10); const unit = matches[2].toLowerCase(); // Convert the value to bytes based on its unit switch (unit) { case 'kb': return value * 1024; case 'mb': return value * 1024 * 1024; case 'gb': return value * 1024 * 1024 * 1024; default: throw new Error(`Unknown unit: ${unit}`); } }; const maxLambdaMemory = () => { if (process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE) { return (parseInt(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE, 10) * 1024 * 1024); } return Infinity; }; exports.maxLambdaMemory = maxLambdaMemory; const getAvailableMemory = (logLevel) => { const maxMemory = (0, exports.maxLambdaMemory)(); if ((0, node_fs_1.existsSync)('/proc/meminfo')) { try { const val = getFreeMemoryFromProcMeminfo(); if (val !== null) { return Math.min(val, maxMemory); } } catch (err) { logger_1.Log.warn({ indent: false, logLevel }, 'Tried to get available memory from /proc/meminfo but failed. Falling back to os.freemem(). Error:'); logger_1.Log.warn({ indent: false, logLevel }, err); } } return Math.min((0, node_os_1.freemem)(), maxMemory); }; exports.getAvailableMemory = getAvailableMemory;