如何限制 WINE 所看到的 RAM 数量?

如何限制 WINE 所看到的 RAM 数量?

我有一个想使用的程序,但它非常老旧,并且以千字节为单位测量 RAM。它无法启动,因为它抱怨它无法在少于 5000K RAM 的情况下运行(尽管我的 RAM 大得多)。我如何限制 WINE 看到/告诉程序的 RAM 数量?

答案1

我不确定这是否可行,但我找到了一个脚本这里限制进程的内存。

脚本如下:

#!/bin/sh

set -eu

if [ "$#" -lt 2 ]
then
        echo Usage: `basename $0` "<limit> <command>..."
        exit 1
fi

limit="$1"
shift

cgname="limitmem_$$"
echo "limiting memory to $limit (cgroup $cgname) for command $@" >&2

cgm create memory "$cgname" >/dev/null
cgm setvalue memory "$cgname" memory.limit_in_bytes "$limit" >/dev/null
# try also limiting swap usage, but this fails if the system has no swap
cgm setvalue memory "$cgname" memory.memsw.limit_in_bytes "$limit" >/dev/null 2>&1 || true
bytes_limit=`cgm getvalue memory "$cgname" memory.limit_in_bytes | tail -1 | cut -f2 -d\"`

# spawn subshell to run in the cgroup
# set +e so a failing child does not prevent us from removing the cgroup
set +e
(
set -e
cgm movepid memory "$cgname" `sh -c 'echo $PPID'` > /dev/null
exec "$@"
)

# grab exit code 
exitcode=`echo $?`

set -e

peak_mem=`cgm getvalue memory "$cgname" memory.max_usage_in_bytes | tail -1 | cut -f2 -d\"`
failcount=`cgm getvalue memory "$cgname" memory.failcnt | tail -1 | cut -f2 -d\"`
percent=`expr "$peak_mem" / \( "$bytes_limit" / 100 \)`
echo "peak memory used: $peak_mem ($percent%); exceeded limit $failcount times" >&2

cgm remove memory "$cgname" >/dev/null

exit $exitcode

您必须安装cgmanagersudo apt-get install cgmanager),但它只占半兆多一点。

尽管有限制,WINE(或可执行文件)可能会检测到您拥有的所有 RAM,在这种情况下,此操作很可能会失败。如果发生这种情况,您可以随时删除脚本,然后cgmanager使用快速卸载sudo apt-get autoremove --purge cgmanager

相关内容