journald memory usage on CentOS 7

Mik*_*Do- 6 centos memory-usage systemd journald

I have an AWS EC2 instance running a CentOS 7.7.1908 (systemd 219) with a server application. The server logs quite a lot of information to the system logs (using syslog).

I have recently enabled persistent storage of the system logs using this answer. Since then, the memory consumption of systemd-journald is constantly growing.

After a full day, systemd-journald ends up using more than 250M of RAM.

我做了一个快速测试来确认 Journald RAM 使用量实际上在增长。

测试 :

  • 获取systemd-journald内存使用情况
  • 使用以下命令将一堆数据记录到系统日志中logger
  • 获取systemd-journald内存使用情况
  • 使用以下命令将内存中的日志(如果有)刷新到 /var/log/journaljournalctl --flush
  • 重新启动systemd-journald服务
  • 获取systemd-journald内存使用情况

结果 :

# ps aux | grep journald | grep -v grep
root     23963  0.0  0.1  61320  2500 ?        Ss   15:03   0:00 /usr/lib/systemd/systemd-journald
# for i in {0..7000}; do logger -t TEST -p err `python -c 'print "A"*1000'`; done;
# ps aux | grep journald | grep -v grep
root     23963  0.1  0.2  69512 11964 ?        Ss   15:08   0:00 /usr/lib/systemd/systemd-journald
# journalctl --flush
# ps aux | grep journald | grep -v grep
root     23963  0.1  0.2  69512 11964 ?        Ss   15:08   0:00 /usr/lib/systemd/systemd-journald
# systemctl restart systemd-journald
# ps aux | grep journald | grep -v grep
root     24237  0.0  0.1  55416  2492 ?        Ss   15:08   0:00 /usr/lib/systemd/systemd-journald
Run Code Online (Sandbox Code Playgroud)

记录大约 7M 的数据后,内存使用量从 2.5M 增加到近 12M。重新启动守护进程会使内存使用量恢复到约 2.5M。

我的解释:

  • systemd-journald内存使用量与其处理的数据量成正比。
  • 该参数RuntimeMaxUse不限制此内存使用。
  • 重新启动systemd-journald会清除缓冲区,但我无法配置其大小。
  • 可以使用的内存量没有可配置的限制systemd-journald
  • 内存使用不是由于日志等待刷新到磁盘造成的。

我的journald.conf:

[Journal]
#Storage=auto # /var/log/journal is created and used, so this is equivalent to 'Persistent'
SyncIntervalSec=1m
RateLimitInterval=0
RateLimitBurst=0
SystemMaxUse=4G
RuntimeMaxUse=1M
Run Code Online (Sandbox Code Playgroud)

如何解决这个“泄漏”而不需要每天重新启动服务呢?