开发者

Why use a Groovy shell script over Bash (when memory footprint is important)? [closed]

开发者 https://www.devze.com 2023-03-20 07:04 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing th
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 9 years ago.

Improve this question

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!"

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.

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.

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?


If memory is limited like that, then any language running on the JVM will be at a disadvantage. And Groovy is such a language.

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).

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.

bash itself is a nice scripting language with a reasonable memory requirement. If memory footprint is really important, than another POSIX-compliant shell (such as dash) might be a reasonable replacement. Note that many, but not all features of bash are present in modern POSIX-compliant Shells.


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.

Groovy and Scala are overkill for trivial tasks, but a godsend for bulldozer situations -- serious scripts make the JVM look pretty thrifty.


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.

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 that good?). And the Java platform is just huge. It has everything.

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?

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.


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.

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


As you said use python or perl, for such things they have a much lower memory footprint.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消