Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, JIT and Garbage Collector efficiency

I want to know about the efficiency of Java and the advantages and disadvantages of Java Virtual Machine and Android. Efficiency is the low use of memory, low use of the processor and fast execution.

Mobile devices are simpler than PC, then the apps need to be more efficient. Servers receive many connections and they need to be very efficient. Many mobile devices use Android and Java apps, and many servers use PHP.

Can Java and interpreted languages, such as Java Script, Python and PHP, be more efficient than C and C++?

JIT (just in time) advantages:

  • It can optimize better, because it knows the value of some variables and where it is used or changed.
  • It knows the processor and can optimize with processor specific instructions.
  • It is easier to transform functions into inline function.
  • It can remove known conditional tests and remove blocks that will not be run.

Java disadvantages:

  • When the app run for the first time, the app will be very slow, because the bytecodes will be interpreted and JIT compiler will do many analysis to find good optimizations. The apps cannot use the maximum of the hardware power. If an app is a game or a real-time app, if it be run for the first successfully and with no delay, but it uses the maximum of the hardware power, then the next time the app be run, it will not use the maximum of the hardware power due to optimizations. The problem is the app cannot be designed to use the maximum of the hardware power after the optimization, because it will be too slow on the first run, and will not continue to run.
  • Java checks if the array index is not out of bounds, and it checks if the pointers are not null. It will add several internal "if"s to generated code.
  • All objects use garbage collector, including objects that are very easy to manually delete.
  • All instances of objects are created with dynamic memory allocation, including objects that can easily use the stack. If a loop iteration begins creating an instance of a class and ends deleting the created object, dynamic memory allocation will be inefficient.
  • The garbage collector needs to stop the app while it cleans the memory and it is very undesired for games, GUI apps and real-time apps. Reference counting is slow and it cannot handle circular references. Multi-threaded garbage collector is slower and it needs more use of the CPU.
like image 360
Squall Avatar asked Dec 17 '22 15:12

Squall


1 Answers

Can Java and interpreted languages, such as Java Script, Python and PHP, be more efficient than C and C++?

It's very difficult to get more efficient than the best C and C++ programs. There's a lot of C and C++ programs that are nowhere near as efficient as that though, and beating them with (modern) Java code is quite practical if you're any good. I've also heard good things about the current best-of-breed Javascript engines, but I've never studied them in detail.

With Python and PHP (and many other languages besides) it's a bit different. Those languages are written in C, so it's obvious they cannot be more efficient than C (follows by construction). Yet it's much easier to write efficient code in them (i.e., that uses what is in-effect a very well-written C library) than it is to start from scratch. In particular, it reduces the number of defects per program. That's a very important metric in practice; anyone can produce fast code if it's allowed to be wrong.

In general, I advise not worrying about getting maximal efficiency. You run up against the law of diminishing returns. Instead, use sensible overall algorithms (or, as a friend of mine once said to me, “look after the big O()s and let the constant factors look after themselves”) and focus on the question of whether the program is good enough in practice. Once it is, stop fiddling around and ship it!

like image 116
Donal Fellows Avatar answered Jan 04 '23 12:01

Donal Fellows