Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory not being returned correctly #1246

Open
sanvit opened this issue Feb 18, 2024 · 3 comments
Open

Memory not being returned correctly #1246

sanvit opened this issue Feb 18, 2024 · 3 comments

Comments

@sanvit
Copy link

sanvit commented Feb 18, 2024

image

I'm trying to host imgproxy in railway.app. As you can see, after the spike in memory usage due to processing, the memory usage doesn't go down (the drop at the end is due to a forced restart of the container). Is there a way to force-flush the memory after each image processing or in an interval?
Below is my current env file just in case

IMGPROXY_DOWNLOAD_TIMEOUT=30
IMGPROXY_WORKERS=16
IMGPROXY_ENABLE_DEBUG_HEADERS=true
IMGPROXY_JPEG_PROGRESSIVE=true
IMGPROXY_PNG_INTERLACED=true
IMGPROXY_ENABLE_WEBP_DETECTION=true
IMGPROXY_ENABLE_AVIF_DETECTION=true
IMGPROXY_MAX_SRC_RESOLUTION=200
@DarthSim
Copy link
Member

Hi @sanvit!

imgproxy forcefully runs garbage collection and malloc_trim(0) every 10 seconds by default. However, imgproxy uses cached memory buffers when downloading images. This way imgproxy can reuse memory and minify memory allocations. However, this leads to some memory not being freed right after usage. Some imgproxy's dependencies may also keep some memory for reuse.

However, even considering all these things memory graph is usually not this flat. It seems like your graph shows the container's memory usage. Containers tend to reserve some memory, and this is a normal behavior.

@sanvit
Copy link
Author

sanvit commented Feb 19, 2024

Hello @DarthSim,

First of all, thank you so much for your reply and your insight. I really appreciate your contribution to this project.

imgproxy uses cached memory buffers when downloading images

Is there a way to force-evict the cache? I'm trying to do everything I can in order to reduce memory usage inside the container if possible, as railway charges me for the exact memory the container is using.

It seems like your graph shows the container's memory usage. Containers tend to reserve some memory, and this is a normal behavior.

As per this, I am not quite sure as I'm new to railway. However, my simeple django app didn't have this kind of issue (although it didn't really require that much more ram), so I think I'll need to dig a bit deeper.

Anyways, thank you so much for your input, and I'll update this issue when I find something new.

@DarthSim
Copy link
Member

Is there a way to force-evict the cache? I'm trying to do everything I can in order to reduce memory usage inside the container if possible, as railway charges me for the exact memory the container is using.

There's no way to do so. And I would not recommend doing it. Without cached buffers, imgproxy will have to allocate a new buffer for every image, and that buffer will use memory until the next garbage collection run.

I would recommend you to try an alternative memory allocator. Try setting IMGPROXY_MALLOC=tcmalloc or IMGPROXY_MALLOC=jemalloc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants