DEV Community

Discussion on: How to see and limit memory consumption of an application ?

Collapse
 
maximilianxu profile image
ZhiyingXu

Hi, thanks for the script.

I want to limit the RAM usage of python script with the systemd style (the limitmem.sh works for me though). I tried the following systemd service:

pylimitmem.service

[Unit]
Description=limit_python_memory

[Service]
User=zyx
Group=zyx
ExecStart=/usr/bin/python3
#Restart=on-failure
KillMode=process
MemoryAccounting=true
MemoryMax=10M

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

After this, I ran the following comand:

systemctl daemon-reload                        
systemctl enable pylimitmem
systemctl start pylimitmem
Enter fullscreen mode Exit fullscreen mode

With the following script to test whether it works:

import numpy as np
import time
zeros = []
while True:
  print([np.zeros((10000, 10000)) for _ in range(100000)])
  z = np.zeros((100000, 100000))
  time.sleep(1)
  zeros.append(z)
  print("allocated zeros")
print(zeros)
Enter fullscreen mode Exit fullscreen mode

Then the os still crash immediately...

Could you please give me some suggestions? Thanks.