限制程序的 RAM 使用量

限制程序的 RAM 使用量

有没有简单的方法可以限制特定程序的内存消耗?我有一个程序会生成一个非常大的文件,最大可达 1 GB,这会导致我的整个笔记本电脑死机。我想限制该程序的内存使用量,这样它就不会死机。

答案1

您可以添加交换空间,man mkswap;man swapon让系统认为它有更多的内存。

要限制内存使用量,请阅读man bash并搜索ulimit

   ulimit [-HSabcdefiklmnpqrstuvxPT [limit]]
          Provides control over the resources available to the shell and to processes started
          by  it, on systems that allow such control.  The -H and -S options specify that the
          hard or soft limit is set for the given resource.  A hard limit cannot be increased
          by a non-root user once it is set; a soft limit may be increased up to the value of
          the hard limit.  If neither -H nor -S is specified, both the soft and  hard  limits
          are set.  The value of limit can be a number in the unit specified for the resource
          or one of the special values hard, soft, or unlimited, which stand for the  current
          hard  limit,  the  current  soft  limit,  and  no limit, respectively.  If limit is
          omitted, the current value of the soft limit of the resource is printed, unless the
          -H  option  is given.  When more than one resource is specified, the limit name and
          unit are printed before the value.  Other options are interpreted as follows:
 ...
          -d     The maximum size of a process's data segment
 ...
          -l     The maximum size that may be locked into memory
 ...
         -v     The  maximum  amount  of  virtual memory available to the shell and, on some
                 systems, to its children

相关内容