Home  >  Q&A  >  body text

ubuntu - 在linux怎么限制一个进程运行内存大小

在运行一个命令的时候可以限制其消耗内存大小,我找到一个命令ulimit,但是他如果发现进程超过最大内存的话,会kill掉。这并不是我想要的,我希望只是限制使用最大内存,但不能kill掉。

PHP中文网PHP中文网2742 days ago774

reply all(1)I'll reply

  • 阿神

    阿神2017-04-17 12:04:40

    Using ulimit is the right way to solve this problem.

    If the memory is exceeded and you still don’t kill it, what should you do with the program? Let it continue to use memory? Then why limit the total memory~

    If the command is written by yourself, and the memory can be reclaimed as long as it is notified externally, then you can consider making this command respond to a special message, such as SIGUSR1, and reclaim the memory after receiving it. Then write a monitor program to continuously check the memory usage through /proc/$PID, and send it a message once it exceeds the limit. However, it seems that the request of the questioner is an arbitrary command, so this method obviously does not work.

    reply
    0
  • Cancelreply