编写一个 bash shell 脚本,在用户定义的时间内消耗恒定数量的 RAM
我正在尝试编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
即使不支持传统的 Bash 数组,仍然可以创建数组- 类似于使用特定 shell 中内置的
eval
命令的变量。以下示例脚本基于我在嵌入式 Linux 项目中使用 BusyBox 时编写的一些脚本。 BusyBox 使用 Almquist shell(也称为 A Shell、ash 和 sh),不支持数组。
使用
eval
时请小心引用!输出:
根据您的特定场景,类似于以下内容的脚本可能就足够了。
在我的简短测试中,该脚本在指定的 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.
Be careful with quoting when using
eval
!Output:
Depending on your particular scenario, a script similar to the following may suffice.
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
如果您有
/dev/shm
设备,您可以写入位于那里的文件,因为默认情况下它是 tmpfs。If you have a
/dev/shm
device, you can write to file located there, since it's a tmpfs by default.就我个人而言,我会同意 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.
您需要区分已分配 RAM 和工作集 RAM。在 bash 中很容易耗尽内存:
但除非脚本频繁地搅动数据,否则这些内存页很适合被换出。
You need to distinguish between allocated and working-set RAM. It's easy to eat up memory in bash:
but unless the script churns through the data frequently then those pages of memory are good candidates to be swapped out.
@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.我想出了这个。 /dev 是一个 tmpfs
I came up with this. /dev is a tmpfs