Skip to content

Conversation

@dunglas
Copy link
Collaborator

@dunglas dunglas commented Jan 25, 2025

Setting the Go runtime memory limit can improve significantly the performance of Caddy (and FrankenPHP and Mercure, according to our tests), especially on low-resources machines and containers.

This patch uses github.com/KimMachineGun/automemlimit to automatically find a good limit using the currently configured groups. This is especially useful when setting limits to containers using Docker, Kubernetes, LXC, and the like.

This PR is similar to #6047, which does the same things go the max number of processors.

Thanks to @flovntp for the idea and for finding this library!

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Kevin! Do you think we could add a comment to this code (and maybe it could be shared with the gomaxprocs code above it) since I don't think it's obvious why it's useful? Then we will merge!

@mholt mholt added the optimization 📉 Performance or cost improvements label Jan 26, 2025
@dunglas
Copy link
Collaborator Author

dunglas commented Jan 26, 2025

@mholt, done.

I also added logs for GOMAXPROCS to improve the overall experience. Commands now start with something like this:

2025/01/26 21:11:59.838 INFO    maxprocs: Leaving GOMAXPROCS=10: CPU quota undefined
2025/01/26 21:11:59.838 INFO    GOMEMLIMIT is updated   {"package": "github.com/KimMachineGun/automemlimit/memlimit", "GOMEMLIMIT": 15461882265, "previous": 9223372036854775807}

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great - thanks Kevin!

@mholt mholt merged commit 066d770 into caddyserver:master Jan 27, 2025
33 checks passed
@dunglas dunglas deleted the automemlimit branch January 27, 2025 22:05
mohammed90 pushed a commit to cedricziel/caddy that referenced this pull request Aug 29, 2025
* feat: automatically set GOMEMLIMIT

* add system support

* comments

* add logs
@mohammed90 mohammed90 mentioned this pull request Oct 25, 2025
46 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimization 📉 Performance or cost improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants