编写一个 bash shell 脚本,在用户定义的时间内消耗恒定数量的 RAM

发布于 2024-10-16 19:13:30 字数 77 浏览 3 评论 0原文

我正在尝试编写一个 bash shell 脚本,该脚本在用户定义的时间内在嵌入式设备上消耗大量 RAM。在不使用数组的情况下如何做到这一点?

I am trying to write a bash shell script that consumes a high amount of RAM on an embedded device for a user defined time. How do I do it without using arrays ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

太傻旳人生 2024-10-23 19:13:30

即使不支持传统的 Bash 数组,仍然可以创建数组- 类似于使用特定 shell 中内置的 eval 命令的变量。

以下示例脚本基于我在嵌入式 Linux 项目中使用 BusyBox 时编写的一些脚本。 BusyBox 使用 Almquist shell(也称为 A Shell、ash 和 sh),不支持数组。

#!/bin/ash

for index in 1 2 3 4 5; do
    value=$(($index * 1024))
    eval array$index=\"array[$index]: $value\"
done

for i in 1 3 5; do
    eval echo \$array$i
done

使用eval时请小心引用!

输出:

array[1]: 1024
array[3]: 3072
array[5]: 5120

根据您的特定场景,类似于以下内容的脚本可能就足够了。

#!/bin/ash

echo "Provide sleep time in the form of NUMBER[SUFFIX]"
echo "   SUFFIX may be 's' for seconds (default), 'm' for minutes,"
echo "   'h' for hours, or 'd' for days."
read -p "> " delay

echo "begin allocating memory..."
for index in $(seq 1000); do
    value=$(seq -w -s '' $index $(($index + 100000)))
    eval array$index=$value
done
echo "...end allocating memory"

echo "sleeping for $delay"
sleep $delay

在我的简短测试中,该脚本在指定的 5 分钟时间段内消耗了约 570M 到约 575M 物理内存*

* 在单独的测试中使用 top 和 memprof 程序进行监控

Even if traditional Bash arrays are not supported, it may still be possible to create array-like variables using the eval command built into the particular shell.

The following example script is based on some scripting I did when using BusyBox in an embedded Linux project. BusyBox uses the Almquist shell (also known as A Shell, ash, and sh), which does not support arrays.

#!/bin/ash

for index in 1 2 3 4 5; do
    value=$(($index * 1024))
    eval array$index=\"array[$index]: $value\"
done

for i in 1 3 5; do
    eval echo \$array$i
done

Be careful with quoting when using eval!

Output:

array[1]: 1024
array[3]: 3072
array[5]: 5120

Depending on your particular scenario, a script similar to the following may suffice.

#!/bin/ash

echo "Provide sleep time in the form of NUMBER[SUFFIX]"
echo "   SUFFIX may be 's' for seconds (default), 'm' for minutes,"
echo "   'h' for hours, or 'd' for days."
read -p "> " delay

echo "begin allocating memory..."
for index in $(seq 1000); do
    value=$(seq -w -s '' $index $(($index + 100000)))
    eval array$index=$value
done
echo "...end allocating memory"

echo "sleeping for $delay"
sleep $delay

In my brief testing, this script consumed ~570M to ~575M physical memory* for the specified time period of 5 minutes.

* Monitored using top and memprof programs in separate tests

Spring初心 2024-10-23 19:13:30

如果您有 /dev/shm 设备,您可以写入位于那里的文件,因为默认情况下它是 tmpfs。

If you have a /dev/shm device, you can write to file located there, since it's a tmpfs by default.

一梦等七年七年为一梦 2024-10-23 19:13:30

就我个人而言,我会同意 Nick 的答案,因为用 C 语言来做这件事确实会容易得多。

但是...如果你真的想避免编写一个超级简单的 C 程序来做到这一点,那么(如果系统运行的是 Linux,并且内置了正确的东西)你应该能够通过安装一个大小为 tmpfs 的 tmpfs 来做到这一点限制您想要使用的内存量,然后将数据喷射到该 tmpfs 中的文件中以填充它(例如,通过从无限源(例如,/dev/zero)复制数据)。

不过,只要您可以针对该平台进行编译,C 程序确实更容易。

Personally I would go with Nick's answer, since doing it in C is going to be much easier really.

But... if you really want to avoid writing a super-simple C program to do it, then (if the system is running Linux with the right stuff built in) you should be able to do it by mounting a tmpfs with a size limit of however much memory you want to use, then spewing data into a file in that tmpfs to fill it up (by, e.g., copying data from an infinite source (e.g., /dev/zero).

The C program is really easier though, as long as you can compile for the platform.

对你而言 2024-10-23 19:13:30

您需要区分已分配 RAM 和工作集 RAM。在 bash 中很容易耗尽内存:

A="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
for power in $(seq 8); do
  A="${A}${A}"
done

但除非脚本频繁地搅动数据,否则这些内存页很适合被换出。

You need to distinguish between allocated and working-set RAM. It's easy to eat up memory in bash:

A="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
for power in $(seq 8); do
  A="${A}${A}"
done

but unless the script churns through the data frequently then those pages of memory are good candidates to be swapped out.

相权↑美人 2024-10-23 19:13:30

@JohnBartholomew

你关于 tmpfs 挂载的想法也不是那么难,你可以更确定它实际上正在消耗 RAM,对吗? (请参阅 Chris Dodd 在 Nick 的回答中的评论)

mount -t tmpfs none /new/path/for/temp -o size=32m
dd if=/dev/zero of=/new/path/for/temp/zero.txt bs=32m count=1

可能 dd 会抱怨没有空间留在设备上。另外,我不知道具体会使用多少 RAM,但如果你谈论的是 MB,那么这应该没问题。

@JohnBartholomew

Your idea about a tmpfs mount is also not that hard and you can be more sure that it's actually consuming RAM, right? (see Chris Dodd's comment at Nick's answer)

mount -t tmpfs none /new/path/for/temp -o size=32m
dd if=/dev/zero of=/new/path/for/temp/zero.txt bs=32m count=1

Probably dd will complain that there is no space left on the device. Also, I don't know how much RAM will be used exactly, but if you're talking about MB's than this should be fine.

初见终念 2024-10-23 19:13:30

我想出了这个。 /dev 是一个 tmpfs

 #!/bin/sh

 mntroot rw
 cd /dev
 while : 
 do 
        dd > /dev/null 2>&1 if=/dev/zero of=myfile1 count=25000 bs=1024 # eat up 25 MB of RAM 
        usleep 1 
        rm myfile1

 done

I came up with this. /dev is a tmpfs

 #!/bin/sh

 mntroot rw
 cd /dev
 while : 
 do 
        dd > /dev/null 2>&1 if=/dev/zero of=myfile1 count=25000 bs=1024 # eat up 25 MB of RAM 
        usleep 1 
        rm myfile1

 done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文