如何用随机数填充一定量的RAM?

问题描述 投票:0回答:1

以下命令行可用于分配任意数量的内存,例如1GB。内存区域充满了零。

</dev/zero head -c $((1024**3)) | tail

但是,如何才能类似地填充给定量的内存,例如1GB,随机数?

与预期相反,以下两个变体不会导致工作内存的可选择分配大小,而是在终端中输出几百个随机字符:

</dev/random head -c $((1024**3)) | tail
</dev/urandom head -c $((1024**3)) | tail

也许关于以下不起作用,人们可以提供解决方案的想法,但会给出一条错误消息,例如“您没有权利”

output=$(sudo bash -c 'memory_to_reserve=$((1024**3)); /dev/random head -c $memory_to_reserve | tail')

下面这个看起来不像是一个解决方案,因为它操作了数据(remove/0)并且只保存了几百个字符的变量,但不是1GB:

output=$(sudo head -c $((1024**3)) /dev/random | tr -d '\0' | tail)

关于以下一个可能是一种解决方案的想法,但看起来 var 上只有几百个字符而不是 1GB:

output=""
while IFS= read -r -d '' substring; do
 output+="$substring"
done < <(sudo head -c $((1024**3)) /dev/random | tail)
bash shell random ram
1个回答
0
投票

如何类似地填充给定量的内存,例如1GB,随机数?

tail
lines 上输出。所以删除换行符。

</dev/random tr -d '\n' | head -c $((1024**3)) | tail >/dev/null
© www.soinside.com 2019 - 2024. All rights reserved.