Why use the Groovy script shell on top of Bash (with significant memory impact)?

I have a Bash script (50 lines of code) that requires a bit of hacking due to bash restrictions. So someone said, “Hey, use a better language than Bash!”

So, I looked at Groovy (Groovy / Grails in the following list of my activities, so maybe this is a good time). I can make more complex shell scripts, so this might be perfect.

But when I run even a simple Groovy script ( while(true){...} ), the memory consumption for the Java process is 123 M, oh, I have about 10 such scripts to run (now all Bash) on a box with 650 MB of memory. The equivalent standalone Bash script runs in approximately 1.5 MB of memory.

So this is the case:

  • Am I a newbie doing something wrong, 123M memory not needed?
  • Does Groovy not fit here? Perhaps Python or Perl are more aware of memory?
  • Is bash a good option, even for its shortcomings, when a small footprint is important?
+6
source share
6 answers

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

Other languages, such as Python or Perl, have a more compact runtime and require less memory for simple scripts (I believe Python is still a little more compact than Perl, but I can't support this with numbers).

In my opinion, Python is a good step by step from bash scripts, providing much more nice features, but it is thin enough to be used in ordinary scripts.

bash itself is a good scripting language with a reasonable memory requirement. If memory size is really important, then it might be a reasonable substitute for another POSIX-compatible shell (such as

+9
source

Here is the formula:

  • Use Bash for scripts that just poke and tickle other programs.
  • Avoid Perl if you can't imagine yourself being an archaic heavy hacker.
  • Use Python or Ruby for scripts that automate lightweight tasks.
  • Use Groovy or Scala for scenarios that automate heavy-duty tasks.

Groovy and Scala are superfluous for trivial tasks, but a find for situations with bulldozers - serious scenarios make the JVM very attractive.

+8
source

When you start and run the application running on the JVM, you should be able to configure things like heap size and shift space, which should affect the amount of memory. If you need to write in Java or Groovy or Jython or JRuby or Scala, see how this can be configured for your application.

Yes, JVM languages ​​are known for memory consumption, such as 8-byte overhead for objects and the need for padding, which cause Integer objects to require much more memory than you might think (see somewhere :)). However, the JVM offers many advantages: most JVMs have been tuned and optimized by really smart people over the past 15 years (can you write a garbage collector, which is good?). And the Java platform is just huge. He has it all.

Are your 10 scripts long-term and should be permanent in memory at the same time? Or do they work fast? Since you already wrote them in Bash, it looks like you don't need a rich Java platform. Are you just moving files around and carving and grepping and awking and sedding?

I usually like Bash for small scripts, but as soon as I get to the point where I have loops or two and a few variables (basically something related to logic other than the if statement), Ruby and Python start searching is much easier to read and maintain. Groovy is not necessarily a bad choice (although other languages ​​are more popular). But as far as memory issues are concerned, you should take the time to try to configure JVM-based applications and take some measurements. In your situation, this may not matter much. Perhaps the start time of the JVM plays a factor? Much depends on your specific situation.

+7
source

Java generally has problems when you want to run quickie on the command line. I have had great success using (and expanding) a tool called Nailgun . It basically starts one instance of the JVM and uses its own client to send scripts or basic commands to the server for evaluation, and the output streams are converted to standard command line output. The results are pretty impressive.

Also see this post . Quote:

Here's an example of JRuby's startup time on OS X Java 6, the first normal and then with ng:

fine

~ / NetBeansProjects / jruby $ time jruby -e "puts 'hello'" hello

real 0m1.944s user 0m1.511s sys 0m0.138s

Nail gun:

~ / NetBeansProjects / jruby $ time jruby -e "puts 'hello'" hello

real 0m0.103s user 0m0.006s sys 0m0.009s

+4
source

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

+1
source

Keep in mind that "groovy" is just a shell script that runs "java -cp groovy.jar: $ CLASSPATH" or something similar. This means that you must check if JVM flags exist that affect the pre-allocated memory and modify them accordingly. Also, do these scripts call each other? If so, try to make them groovy classes and try to run them all in one virtual machine.

+1
source

Source: https://habr.com/ru/post/892813/


All Articles