为什么在 Bash 上使用 Groovy shell 脚本(当内存占用很重要时)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6704197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use a Groovy shell script over Bash (when memory footprint is important)?
提问by David Parks
I've got a Bash script (50 lines of code) that requires a bit of hacking due to limitations of bash. So someone said: "hey, use a better language than bash for that!"
我有一个 Bash 脚本(50 行代码),由于 bash 的限制,它需要一些黑客攻击。所以有人说:“嘿,为此使用比 bash 更好的语言!”
So, I looked at Groovy (Groovy/Grails is next up on my to-learn list, so maybe this is a good time). I can do more complex shell scripts, so this might be a perfect fit.
所以,我研究了 Groovy(Groovy/Grails 是我学习列表中的下一个,所以也许这是一个好时机)。我可以做更复杂的 shell 脚本,所以这可能是一个完美的选择。
But when I run even a simple Groovy script (while(true){...}) the memory consumption of the Java process is 123M, ouch, I've got about 10 such scripts to run (all bash based now) on a box with 650M of memory. The equivalent stand-alone bash script runs in around 1.5M of memory.
但是当我运行一个简单的 Groovy 脚本 ( while(true){...}) 时,Java 进程的内存消耗是 123M,哎哟,我有大约 10 个这样的脚本要在一个具有 650M 内存的盒子上运行(现在全部基于 bash)。等效的独立 bash 脚本在大约 1.5M 的内存中运行。
So is this a case of:
所以这是一个案例:
- I'm a newbie doing something wrong, 123M memory footprint is not necessary?
- Groovy isn't a good fit here? Perhaps Python or Perl are more memory conscious?
- bash is a good option, even for its shortcomings, when a small footprint is important?
- 我是新手做错了,123M的内存占用不是必须的吗?
- Groovy 不适合这里?也许 Python 或 Perl 更注重内存?
- bash 是一个不错的选择,即使它有缺点,当占用空间小很重要时?
回答by Joachim Sauer
If memory is limited like that, then any language running on the JVM will be at a disadvantage. And Groovy is such a language.
如果内存如此有限,那么任何在 JVM 上运行的语言都会处于劣势。Groovy 就是这样一种语言。
Other languages like Python or Perl have a leaner runtime and require less memory for simple scripts (my guess is that Python is still a bit leaner than Perl, but I can't back that up with numbers).
其他语言如 Python 或 Perl 具有更精简的运行时,并且对于简单脚本需要更少的内存(我的猜测是 Python 仍然比 Perl 精简一点,但我无法用数字来证明这一点)。
In my opinion Python is a nice step-up from bash scripts, providing much nicer features while still being lean enough to be used in common scripts.
在我看来,Python 是 bash 脚本的一个很好的升级,它提供了更好的功能,同时仍然足够精简以用于普通脚本。
bash itself is a nice scripting language with a reasonable memory requirement. If memory footprint is reallyimportant, than another POSIX-compliant shell (such as dash) might be a reasonable replacement. Note that many, but not all features of bashare present in modern POSIX-compliant Shells.
bash 本身是一种不错的脚本语言,具有合理的内存要求。如果内存占用真的很重要,那么另一个符合 POSIX 的 shell(例如dash)可能是一个合理的替代品。请注意,bash现代兼容 POSIX 的 Shell 中存在许多(但不是全部)功能。
回答by Joachim Sauer
Here's a formula:
这是一个公式:
- Use Bash for scripts that merely poke and tickle other programs.
- Avoid Perl unless you fancy yourself as an archaic die-hard hacker.
- Use Python or Ruby for scripts that automate lightweight tasks.
- Use Groovy or Scala for scripts that automate heavy-duty tasks.
- 将 Bash 用于仅戳和逗弄其他程序的脚本。
- 除非你认为自己是一个过时的铁杆黑客,否则不要使用 Perl。
- 将 Python 或 Ruby 用于自动执行轻量级任务的脚本。
- 将 Groovy 或 Scala 用于自动化繁重任务的脚本。
Groovy and Scala are overkill for trivial tasks, but a godsend for bulldozer situations -- serious scripts make the JVM look pretty thrifty.
Groovy 和 Scala 对于琐碎的任务来说太过分了,但对于推土机情况来说是天赐之物——严肃的脚本使 JVM 看起来非常节俭。
回答by Ray Toal
When you launch and application that runs on a JVM you should be able to configure such things as the heap size and permgen space which should affect the memory footprint. If you need to write in Java or Groovy or Jython or JRuby or Scala, then look for how this can be set for your application.
当您启动并运行在 JVM 上的应用程序时,您应该能够配置诸如堆大小和 permgen 空间之类的东西,这应该会影响内存占用。如果您需要使用 Java 或 Groovy 或 Jython 或 JRuby 或 Scala 进行编写,请查看如何为您的应用程序设置。
Yes, the JVM-based languages are known for memory consumption, such as the 8-byte overhead for objects and the need for padding that makes Integer objects require a lot more memory than you might think (look this up somewhere :-)). However the JVM offers a lot of advantages: most JVMs have been highly tuned and optimized over the last 15 years by really smart people (can you write a garbage collector thatgood?). And the Java platform is just huge. It has everything.
是的,基于 JVM 的语言以内存消耗而闻名,例如对象的 8 字节开销和填充的需要,这使得 Integer 对象需要比您想象的更多的内存(在某处查找:-))。然而,JVM 提供了很多优势:在过去的 15 年中,大多数 JVM 都经过非常聪明的人的高度调整和优化(你能写出一个这么好的垃圾收集器吗?)。Java 平台非常庞大。它拥有一切。
Are your 10 scripts long-running and need to be memory-resident all at the same time? Or are they quick-running? Since you have already written them in Bash, it sounds like you have no need for the rich Java platform. Are you just moving files around, and cutting and grepping and awking and sedding?
您的 10 个脚本是否长时间运行并且需要同时驻留在内存中?或者他们跑得很快?由于您已经用 Bash 编写了它们,听起来您不需要丰富的 Java 平台。您是否只是在四处移动文件、剪切、grepping、awking 和 sedding?
Aas a rule of thumb I like Bash for small scripts, but once I get to the point where I have a loops or two and a couple of variables (basically anything involving logic other than an if statement), Ruby and Python start looking so much easier to read and maintain. Groovy isn't necessarily a bad choice (although the other languages are more popular). But as far as memory footprint issues go, you should take the time to try and tune your JVM-based applications and make some measurements. In your situation, it might not matter much. Perhaps JVM startup time plays a factor? A lot depends on your specific situation.
作为一个经验法则,我喜欢 Bash 用于小脚本,但是一旦我到了有一个或两个循环和几个变量(基本上涉及除 if 语句之外的逻辑的任何内容)的地步,Ruby 和 Python 开始看起来如此之多更易于阅读和维护。Groovy 不一定是一个糟糕的选择(尽管其他语言更受欢迎)。但就内存占用问题而言,您应该花时间尝试调整基于 JVM 的应用程序并进行一些测量。在您的情况下,这可能无关紧要。也许 JVM 启动时间是一个因素?很大程度上取决于您的具体情况。
回答by Nicholas
Java in general has issues when you want to run a quickie on the command line. I have had a lot of success using (and extending) a tool called Nailgun. It basically runs one instance of a JVM and uses a native client to pipe scripts or basic commands to the server for evaluation, and the output streams are converted to command line standard output. The results are fairly impressive.
当您想在命令行上运行 quickie 时,Java 通常会出现问题。我在使用(和扩展)一个名为Nailgun的工具方面取得了很大的成功。它基本上运行一个 JVM 实例,并使用本机客户端将脚本或基本命令通过管道传输到服务器进行评估,并将输出流转换为命令行标准输出。结果相当令人印象深刻。
Also see this mail post. To quote:
另请参阅此邮件帖子。报价:
Here's a sample of JRuby startup time on OS X Java 6, first normal and then with ng:
normal:
~/NetBeansProjects/jruby $ time jruby -e "puts 'hello'" hello
real 0m1.944s user 0m1.511s sys 0m0.138s
nailgun:
~/NetBeansProjects/jruby $ time jruby -e "puts 'hello'" hello
real 0m0.103s user 0m0.006s sys 0m0.009s
这是 OS X Java 6 上 JRuby 启动时间的示例,首先是正常的,然后是 ng:
普通的:
~/NetBeansProjects/jruby $ time jruby -e "puts 'hello'" hello
真实 0m1.944s 用户 0m1.511s 系统 0m0.138s
钉枪:
~/NetBeansProjects/jruby $ time jruby -e "puts 'hello'" hello
真实 0m0.103s 用户 0m0.006s 系统 0m0.009s
回答by Angel O'Sphere
Keep in mind that "groovy" is just a wrapper script that starts "java -cp groovy.jar:$CLASSPATH" or something similar. That means you should check if there are JVM flags that affect the preallocated memory and change them accordingly. Also do those scritps call each other? If so try to make them groovy classes instead and try to run them all in the same VM.
请记住,“groovy”只是一个以“java -cp groovy.jar:$CLASSPATH”或类似内容开头的包装脚本。这意味着您应该检查是否存在影响预分配内存的 JVM 标志并相应地更改它们。那些脚本也会互相调用吗?如果是这样,请尝试使它们成为 groovy 类,并尝试在同一个 VM 中运行它们。
回答by Leonard Brünings
As you said use python or perl, for such things they have a much lower memory footprint.
正如您所说,使用 python 或 perl,对于这些事情,它们的内存占用要低得多。

