3

Here is some question about cache area in JVM: (for hotspot Java8)

  • Does all machine code that jvm could run stored in this area, or only some hot code is stored here?
  • From some book, it says client compiler (C1) is more likely to run out of memory in cache area, while server compiler (C2) don't. I am a little confused about that. Is that because the server compiler only compile the hot part, and interrupt the other part? In that case, shouldn't the server compiler be slow?
  • How many times the code is run before C1 & C2 would compile & cache them respectively?
Eric
  • 22,183
  • 20
  • 145
  • 196
  • 1
    By the way, if you're interested in performance, [this book](http://shop.oreilly.com/product/0636920028499.do) gives a good overview of many aspects of it. – biziclop Mar 07 '16 at 10:59
  • @biziclop Nice suggestion, thanks. – Eric Mar 07 '16 at 11:30

1 Answers1

3

Note that the answer to this could vary from implementation to implementation :

Does all machine code that jvm could run stored in this area, or only some hot code is stored here?

Code cache will almost always be used to cache hot methods (or methods that were once considered hot by C1 + C2 (tiered compilation)

From some book, it says client compiler (C1) is more likely to run out of memory in cache area, while server compiler (C2) don't. I am a little confused about that. Is that because the server compiler only compile the hot part, and interrupt the other part? In that case, shouldn't the server compiler be slow?

C1 compiler compiles everything. C2 waits until it finds some optimizations that could be applied and then applies those optimizations and compiles the methods (not always true, but mostly).

Note that C1 can compile the same method multiple times.

How many times the code is run before C1 & C2 would compile them respectively?

A method can be compiled even if it is called only once if C1 / C2 find out that it is taking a long time to run.

You can use JITWatch to see C1 and C2 in action.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Thx, but the answer to first question is a little blur to me, I am mainly focusing on java8 hotspot, can u make it more clear base on that. – Eric Mar 07 '16 at 10:50
  • 2
    `A method can be compiled even if it is called only once if C1 / C2 find out that it is taking a long time to run.` Not directly, but for each loop there is a branching count, which tells Hotspot how many iterations the loop has done. If that counter reaches the threshold, the loop itself is compiled, even if the method itself is still running. – biziclop Mar 07 '16 at 10:54
  • @biziclop About the first question, when using c1, if a method only runs once and it's very quick to the jvm, the method will still be compiled, right? And will it still be cached in the code cache area? – Eric Mar 07 '16 at 11:08
  • @biziclop - But eventually the whole method will be replaced right?. – TheLostMind Mar 07 '16 at 11:15
  • @EricWang - If the method size is less than 325 bytes, it will be inlined. Yes, even if a method runs only once and if C1 / C2 have optimized something in / for it, then it will be added to the code cache – TheLostMind Mar 07 '16 at 11:17
  • @TheLostMind I guess you mean, in C1, if the code runs only once very quickly, and the compiler didn't do any optimization to it, then it won't be cached. I think I get the key philosophy, some of the details doesn't matter that much. – Eric Mar 07 '16 at 11:24
  • @EricWang - I am not entirely sure if non jit-compiled methods will be cached. Frankly it could be dependent on the JVM – TheLostMind Mar 07 '16 at 11:29
  • 1
    @EricWang No. The default compilation threshold is 1500 for C1 and 10000 for C2 (or something similar). If the combined branch + invocation count exceeds that threshold, the method is declared hot and is compiled. However the count is also decreased periodically, so even a method that is invoked a million times over the lifetime of the application may not be compiled, if the invocations are fairly evenly spaced, – biziclop Mar 07 '16 at 11:31
  • @biziclop - You could write an answer here :P. Also can you provide references for *1500 for C1 and 10000 for C2* ? – TheLostMind Mar 07 '16 at 11:33
  • 2
    @biziclop The thresholds 1500 and 10000 don't play much role in Tiered compilation (default in JDK 8). The compilation policy is much more complex, see [this answer](http://stackoverflow.com/questions/35601841/how-does-the-jvm-decided-to-jit-compile-a-method-categorize-a-method-as-hot/35614237#35614237). – apangin Mar 07 '16 at 11:35
  • @apangin Thanks, that is indeed more relevant in Java 8. Also in Java 8 the default code cache is bigger because of this, IIRC. – biziclop Mar 07 '16 at 13:31