systemd-run MemoryMax / MemoryLimitが機能していないようです。

systemd-run MemoryMax / MemoryLimitが機能していないようです。

メモリを占有するようにsystemdを設定します。

$ grep Memory /etc/systemd/system.conf
DefaultMemoryAccounting=yes

100Mのスペースを割り当てる小さな実行可能ファイルを作成します。

$ cat test.c
#include <stdlib.h>
#include <stdio.h>

int main() {
  void *ptr = malloc(100 * 1024 * 1024); // Allocating 100M
  if (ptr == NULL) {
    printf("ptr NULL\n");
  } else {
    printf("ptr allocated\n");
    free(ptr);
  }
}

しかし、10Mの制限で実行するのは大丈夫です。

$ systemd-run --scope -p MemoryMax=10M -p MemorySwapMax=10M -p MemoryLimit=10M --user ./test
Running scope as unit: run-r9e69896461b94a5f8ab68df8e34bee73.scope
ptr allocated

これが予想される動作ですか?または、設定で何かを逃した可能性があります。

ベストアンサー1

メモリ制限は、割り当てだけでなく物理メモリ使用量にも適用されます。努力する

#include <stdlib.h>
#include <stdio.h>

int main() {
  char * ptr = malloc(100 * 1024 * 1024); // Allocating 100M
  if (ptr == NULL) {
    printf("ptr NULL\n");
  } else {
    printf("ptr allocated\n");
    for (int i = 0; i < 100 * 1024 * 1024; i++) ptr[i] = ' ';
    free(ptr);
  }
}

プログラムが終了するのを見ることができます。

おすすめ記事