SIGKILL 程序被終止錯誤
說明 Remotion 渲染時出現 SIGKILL 訊號導致程序被強制終止的原因及解決方法,通常與記憶體不足有關
SIGKILL 程序被終止錯誤
錯誤訊息變體
此錯誤有多種形式:
Remotion Rust 程序被終止:
Compositor quit with signal SIGKILL: [...]
FFmpeg 程序被終止:
FFmpeg quit with code null (SIGKILL)
發生了什麼事?
作業系統正在終止 Remotion 程序或 FFmpeg 程序。這最可能是因為這些程序佔用了過多記憶體,導致 OOM Killer(記憶體不足殺手)介入強制終止程序。
Remotion 的記憶體管理機制
Remotion 為視訊幀擷取開啟了快取,預設情況下允許自己填滿渲染開始時可用記憶體的 50%。
當 Remotion 發現系統記憶體不足時,它會將快取大小縮小一半並釋放記憶體。然而,如果其他程序正在填滿記憶體,Remotion 程序可能在嘗試配置任何記憶體時就被終止。FFmpeg 程序也面臨同樣的問題。
解決方案
1. 降低 Remotion 的記憶體使用量
使用 offthreadVideoCacheSizeInBytes 參數減少 Remotion 的快取大小:
import { renderMedia } from "@remotion/renderer";
await renderMedia({
composition,
serveUrl,
codec: "h264",
outputLocation,
offthreadVideoCacheSizeInBytes: 1024 * 1024 * 512, // 512MB
});在 CLI 中使用:
npx remotion render --offthread-video-cache-size-in-bytes=5368709122. 確保 Remotion 版本為最新
Remotion 持續改進記憶體管理。最後一個帶有改進的版本是 v4.0.171。升級指令:
npm install remotion@latest @remotion/cli@latest @remotion/renderer@latest3. 降低並發數量
設定較低的並發數以同時開啟較少的瀏覽器標籤頁,從而減少記憶體使用:
npx remotion render --concurrency=2或在程式碼中:
await renderMedia({
composition,
serveUrl,
codec: "h264",
outputLocation,
concurrency: 2,
});4. 增加系統記憶體
作為最後手段,您可以為系統分配更多記憶體來緩解此問題:
- 在雲端環境(AWS、GCP 等)中升級到更大的機器類型
- 在本機環境中關閉其他佔用大量記憶體的程式
- 在 Lambda 環境中增加函數的記憶體配置
在 Lambda 環境中的注意事項
如果在 Remotion Lambda 上遇到此問題:
import { renderMediaOnLambda } from "@remotion/lambda";
await renderMediaOnLambda({
functionName,
region,
serveUrl,
composition,
codec: "h264",
memorySizeInMb: 3009, // 增加記憶體
});監控記憶體使用量
可使用以下方式在渲染期間監控記憶體:
# macOS/Linux
watch -n 1 free -h
# 或使用 top/htop 觀察程序記憶體
htop