Java, JIT and garbage collector performance

I want to know about the effectiveness of Java and the advantages and disadvantages of the Java and Android virtual machine. Efficiency - low memory usage, low processor usage and fast execution.

Mobile devices are easier than PCs, then applications should be more efficient. Servers receive many connections, and they must be very efficient. Many mobile devices use Android and Java applications, 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 benefits (just in time):

  • It can be optimized better because it knows the meaning of some variables and where it is used or modified.
  • He knows the processor and can optimize it with specific processor instructions.
  • Easier to convert functions to built-in function.
  • It can remove known conditional tests and remove blocks that will not be executed.

Java disadvantages:

  • When the application is launched for the first time, the application will be very slow because the bytecodes will be interpreted and the JIT compiler will do a lot of analysis to find good optimizations. Applications cannot use maximum hardware power. If the application is a game or application in real time, if it starts for the first one successfully and without delay, but it uses maximum hardware power, then the next time the application starts, it will not use maximum hardware power due to optimization. The problem is that the application cannot be designed to use the maximum power of the equipment after optimization, because in the first run it will be too slow and will not continue to work.
  • Java checks to see if the index of the array is out of bounds, and checks to see if the pointers are specified. It will add some internal ifs to the generated code.
  • All objects use the garbage collector, including objects that are very easy to remove manually.
  • All instances of objects are created with dynamic memory allocation, including objects that can easily use the stack. If the iteration of the loop begins to instantiate the class and finishes deleting the created object, the allocation of dynamic memory will be inefficient.
  • The garbage collector must stop the application while it clears the memory, and this is very undesirable for games, graphics applications and real-time applications. The reference count is slow, and it cannot handle circular references. A multi-threaded garbage collector is slower and needs to use more CPU.
+4
source share
5 answers

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

It is very difficult to achieve greater efficiency than the best C and C ++ programs. There are many C and C ++ programs that are nowhere nearly as effective as this, and beating them with (modern) Java code is quite practical if you are doing well. I also heard good things about modern best-in-class Javascript systems, but I never studied them in detail.

With Python and PHP (and besides, many other languages) this is a little different. These languages ​​are written in C, so it is obvious that they cannot be more efficient than C (follows by design). Nevertheless, it’s much easier to write efficient code in them (i.e. uses what is actually a very well-written C library) than to start from scratch. In particular, this reduces the number of defects for each program. This is a very important indicator in practice; anyone can create quick code if he makes a mistake.

In general, I advise you not to worry about maximum efficiency. You are faced with the law of profit reduction. Instead, use reasonable general algorithms (or, as my friend once told me, “look at the big O () s and let the constant factors take care of yourself”), and focus on the question of whether the program is enough in practice. As soon as this happens, stop messing around and send it!

+5
source

Drop your claimed flaws:

  • When the application is launched for the first time, the application will be very slow because the bytecodes will be interpreted and the JIT compiler will do a lot of analysis to find good optimizations. Applications cannot use maximum hardware power.

Compiling JIT is an implementation issue. These are not all platforms. Indeed, the Android platform could be modified to: 1) compile in advance, or 2) cache the native code created by JIT to speed up launch the next time the application starts.

Interestingly, various Java developers tried to use these strategies at different times, and yet empirical evidence suggests that a simple JIT is the best strategy.

  • Java checks to see if the index of the array is out of bounds, and checks to see if the pointers are specified. It will add some internal ifs to the generated code.

The JIT compiler can optimize many of these tests. Otherwise, the overhead is generally relatively small; for example, a difference of several percent ... not 2 times.

Please note that an alternative to checking is the risk that typical application errors will cause the Android platform to crash. Of course, garbage collection becomes problematic if applications can spoof memory.

  • All objects use the garbage collector, including objects that are very easy to remove manually.

The downside is that it is easy to forget to delete objects, delete objects twice, use them after they are deleted, and so on. These errors lead to errors that are usually difficult to track.

  • All instances of objects are created with dynamic memory allocation, including objects that can easily use the stack. If the iteration of the loop begins to instantiate the class and finishes deleting the created object, the allocation of dynamic memory will be inefficient.

Java dynamic memory allocation and FAST object creation. Faster than in C ++, for example.

  • The garbage collector must stop the application while it clears the memory, and this is very undesirable for games, graphics applications and real-time applications.

Then use a garbage collector with a simultaneous / low pause. Another approach is to implement your application so as not to generate a lot of garbage ... and rarely run garbage collection.

  • The link count is slow and it cannot handle circular links.

There is no suitable Java GC uses reference counting. (On the other hand, there are many C / C ++ manual control schemes. For example, the so-called C ++ smart pointer schemes.)

  • A multi-threaded garbage collector is slower and needs to use more CPU.

In fact, you mean assembly at the same time. Yes, but that means you pay for the extra responsiveness you require for real-time interactive games / applications.

+3
source

What you call "effective," I would call "ideal." An application that requires a small amount of memory, a small amount of processor time and starts quickly sets a different path - it’s good, fast and cheap at the same time. It doesn’t matter if he does something useful or interesting.

The only comparison that I would consider reasonable if all three goals are required is for applications that give a common result. In this case, it is unlikely that a competing group of programmers with uniform potential that any one implementation would exceed all three accounts for the others.

However, your question does not contain a key criterion for the mobile market: the speed of application development. Mobile applications also bring much greater benefits from a positive user experience than optimization in the background. Without this limitation, the issue of efficiency, as you put it, seems to me more complicated than practical.

But to the real question: can a language like Java generate more efficient code than one that is statically placed in the instruction set of the target machine? Probably no. Could it be just as effective or quite effective? Absolutely. If we were to consider a performance platform with fixed, very limited resources that changed infrequently, that would be a different matter.

+2
source

In any language, a quick execution method is to do the job with as little execution as possible, and as little garbage collection as possible.

It sounds like an empty community, but what it means in practice, regardless of language,

  • To structure the data structure, keep it as simple as possible. Stay away from fancy collection classes full of bells and whistles. Especially avoid notifications as a way to save data. If your data is normalized, it can never be inconsistent. If you cannot normalize it, it is better to tolerate temporary inconsistency than to try to keep it in check with notifications.

  • Performance problems creep into even the best code. You should try not to make them, but you will make them anyway. The most important thing is to know how to find them as soon as they are done, and delete them. Here is a purge example. If at the same time you find that you need a better algorithm for large output, then enter it. Insert it, not sure if it needs a recipe for slowness.

No language can save a program from unused performance problems. Language and its compiler, JITter, etc. Looks like a racing horse. It is good to want a good horse, but it is a waste if the jockey is not as thin as possible. Your program is a jockey, and it is your job to take it on weight loss.

+1
source

I will put in an interesting answer given by James Goslin himself in the book "Programming Skill".

Well, I heard that he said that effectively you have two compilers in the Java world. You have a compiler for Java bytecode, and then you have your JIT, which basically recompiles everything again. All your scary optimizations are in JIT .

James: Right. These days beating up really good C and C ++ compilers almost always. When you switch to a dynamic compiler, you will get two advantages when compilers are running right at the last moment. One you know exactly which chipset you are running next. So many times, when people compile a piece of C code, they have to compile it to run according to the general x86 architecture. Almost none of the binaries that you get are especially well tuned for any of them. You download the latest copy of Mozilla, and itll work in almost all Intel CPU architecture. Pretty much a single linux file. Its pretty general, and its compiled using GCC, which is not a very good C compiler.

When HotSpot works, it knows exactly which chipset you are running. This knows exactly how the cache works. It knows exactly how the memory hierarchy works. He knows exactly how all inter-block locks work in the CPU. He knows what set of instructions for expanding this chip. This optimizes what kind of machine you are on. Then the other half is that he actually sees the application as its launch. His ability to have statistics, which is all important. Its ability is inline stuff that the C compiler could never do. What gets embedded in the Java world is pretty amazing. Then you, as storage management, work with modern garbage collectors. With a modern garbage collector, storage distribution is very fast.

Masterminds of programming

+1
source

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


All Articles