-
I have a bot program that uses rust-skia to generate images. However, over time, I've noticed that the program's memory usage gradually increases from around 70MB(with some static resources) at the beginning to about 200MB after a few days. I've only been learning Rust for less than two months, and I'd like to know if this is normal. Here is a code example: let mut surface = surfaces::raster_n32_premul(ISize::new(1080, img_height)).expect("Cannot create surface");
let canvas = surface.canvas();
let mut paint = Paint::default();
paint.set_anti_alias(true);
// Some drawing code, such as:
let img = Image::from_encoded(Data::new_copy(&bytes));
canvas.draw_image(&img, Point::new(100.0, 100.0), Some(&paint));
paint.reset();
let image = surface.image_snapshot();
return match image.encode(None, EncodedImageFormat::JPEG, 50) {
Some(data) => Some(data.as_bytes().to_vec()),
None => {
error!("Image generation failed");
None
}
};
// repeat this almost every 10 minutes Translated by ChatGPT because my English is really bad😂 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
The symptom you’re describing doesn’t look normal to me. Do you have a standalone sample that reproduces the issue? |
Beta Was this translation helpful? Give feedback.
-
@tronical This sample seems to have the same problem use skia_safe::*;
use sysinfo::{System, SystemExt, ProcessExt};
use walkdir::WalkDir;
fn main() {
const MB: f32 = 1048576.0;
let mut sys = System::new();
let pid = sysinfo::get_current_pid().unwrap();
{
sys.refresh_system();
sys.refresh_process(pid);
let process = sys.process(pid.clone()).unwrap();
let prog = process.memory() as f32;
println!("Initial memory: {:.2}MB", prog / MB);
for entry in WalkDir::new("./imgs") { // example imgset
let entry = entry.unwrap();
if (&entry).file_type().is_dir() { continue; }
let bytes = std::fs::read(&entry.path()).unwrap();
println!("{:?}", entry);
let img = Image::from_encoded(Data::new_copy(&bytes)).unwrap();
let mut surface =
surfaces::raster_n32_premul(ISize::new(1080, img.height())).expect("Cannot create surface");
let canvas = surface.canvas();
canvas.draw_image(
&img,
Point::new(0.0, 0.0),
None,
);
let _output = surface.image_snapshot();
}
}
sys.refresh_process(pid);
let process = sys.process(pid.clone()).unwrap();
let prog = process.memory() as f32;
println!("Current memory: {:.2}MB", prog / MB);
} It outputs
|
Beta Was this translation helpful? Give feedback.
-
Images are cached. I didn't see this. 😂 |
Beta Was this translation helpful? Give feedback.
from_encoded_with_alpha_type
Images are cached. I didn't see this. 😂