From ca4fe2ced7e9c6717c1274da8a74abfeb54f8203 Mon Sep 17 00:00:00 2001 From: ze0987 Date: Fri, 11 Apr 2025 07:03:45 +0200 Subject: [PATCH] feat: limit memory usage for previewing large images (#2602) Co-authored-by: sxyazi --- yazi-config/preset/yazi-default.toml | 2 +- yazi-plugin/preset/plugins/magick.lua | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/yazi-config/preset/yazi-default.toml b/yazi-config/preset/yazi-default.toml index 3324928a..294e8e1f 100644 --- a/yazi-config/preset/yazi-default.toml +++ b/yazi-config/preset/yazi-default.toml @@ -84,7 +84,7 @@ micro_workers = 10 macro_workers = 10 bizarre_retry = 3 image_alloc = 536870912 # 512MB -image_bound = [ 0, 0 ] +image_bound = [ 5000, 5000 ] suppress_preload = false [plugin] diff --git a/yazi-plugin/preset/plugins/magick.lua b/yazi-plugin/preset/plugins/magick.lua index a9496cdd..b976f13d 100644 --- a/yazi-plugin/preset/plugins/magick.lua +++ b/yazi-plugin/preset/plugins/magick.lua @@ -24,7 +24,7 @@ function M:preload(job) return true end - local cmd = M.with_env() + local cmd = M.with_limit() if job.args.flatten then cmd = cmd:arg("-flatten") end @@ -46,10 +46,16 @@ end function M:spot(job) require("file"):spot(job) end -function M.with_env() - local cmd = Command("magick"):env("MAGICK_THREAD_LIMIT", 1) +function M.with_limit() + local cmd = Command("magick"):args { "-limit", "thread", 1 } if rt.tasks.image_alloc > 0 then - cmd = cmd:env("MAGICK_MEMORY_LIMIT", rt.tasks.image_alloc) + cmd = cmd:args({ "-limit", "memory", rt.tasks.image_alloc }):args { "-limit", "disk", "1MiB" } + end + if rt.tasks.image_bound[1] > 0 then + cmd = cmd:args { "-limit", "width", rt.tasks.image_bound[1] } + end + if rt.tasks.image_bound[2] > 0 then + cmd = cmd:args { "-limit", "height", rt.tasks.image_bound[2] } end return cmd end