After inserting 1000w pieces of data into the ArrayList, I suspected jvm...

"Brother Wolf, I have discovered the new world, I will post your code later" "What's wrong, so excited" "Wait..."

 
 
  1. List<Integer> list0 = new ArrayList<Integer>();


  2. long start0 = System.currentTimeMillis();

  3. for (int i = 0; i < 10000000; i++) {

  4.    list0.add(i);

  5. }

  6. System.out.println(System.currentTimeMillis() - start0);


  7. long start1 = System.currentTimeMillis();

  8. List<Integer> list1 = new ArrayList<Integer>();

  9. for (int i = 10000000; i < 20000000; i++) {

  10.    list1.add(i);

  11. }

  12. System.out.println(System.currentTimeMillis() - start1);

"I continuously inserted 10 million pieces of data into an ArrayList, and the results were different in time. They were 2346 797. I didn't understand."

I took a look and knew that the guy's chassis was unstable.

"You add -XX:+PrintGCDetails -XX:+PrintGCDateStamps to see if there is a Full GC for the first time"

"Understood, I will try again"

A few minutes later...

2019-09-28T09:49:07.519-0800: [GC (Allocation Failure) [PSYoungGen: 54888K->10738K(76288K)] 54888K->36180K(251392K), 0.0520111 secs] [Times: user=0.24 sys=0.03, real=0.06 secs] 2019-09-28T09:49:07.590-0800: [GC (Allocation Failure) [PSYoungGen: 74092K->10736K(141824K)] 99534K->80803K(316928K), 0.0693607 secs] [Times: user=0.39 sys=0.03, real=0.06 secs] 2019-09-28T09:49:07.751-0800: [GC (Allocation Failure) [PSYoungGen: 141808K->10736K(141824K)] 211875K->188026K(320512K), 0.1829926 secs] [Times: user=1.02 sys=0.10, real=0.18 secs] 2019-09-28T09:49:07.934-0800: [Full GC (Ergonomics) [PSYoungGen: 10736K->0K(141824K)] [ParOldGen: 177290K->171620K(402432K)] 188026K->171620K(544256K), [Metaspace: 3062K->3062K(1056768K)], 1.8672996 secs] [Times: user=5.96 sys=0.03, real=1.87 secs] 23652019-09-28T09:49:09.832-0800: [GC (Allocation Failure) [PSYoungGen: 129254K->10738K(196608K)] 300875K->282609K(599040K), 0.1039307 secs] [Times: user=0.74 sys=0.07, real=0.10 secs] 2019-09-28T09:49:09.936-0800: [Full GC (Ergonomics) [PSYoungGen: 10738K->0K(196608K)] [ParOldGen: 271871K->36047K(372736K)] 282609K->36047K(569344K), [Metaspace: 3067K->3067K(1056768K)], 0.4510440 secs] [Times: user=1.82 sys=0.01, real=0.45 secs] 2019-09-28T09:49:10.440-0800: [GC (Allocation Failure) [PSYoungGen: 185856K->10752K(264704K)] 221903K->171359K(637440K), 0.1292143 secs] [Times: user=0.97 sys=0.01, real=0.12 secs] 772

"Brother Wolf, the first Full GC really took 1.87s, then I will increase the heap to avoid Full GC"

A few minutes later...

"There is no GC this time, but every time it runs, the former one takes a little longer than the latter. What's the matter?"

"You try to run in a different thread?"

"好"

After a few minutes...

"Executed in different threads, the two take almost the same time, why is this?"

"Do you know OSR?"

"do not know."

"Then I will tell you about it."

OSR (On-Stack Replacement) is a technology that replaces the stack frame of a running function/method at runtime.

In the modern mainstream JVM, all have the ability of multi-layer compilation. At the beginning, the execution is performed in an interpreted manner. This performance is relatively slow (compared with C++), but once it is found that a certain function is executed very frequently At that time, JIT compilation is used to improve function execution performance (mostly faster than C++).

However, if JIT compilation is performed in the unit of function, then it cannot cope with the situation where the main function contains the loop body. At this time, OSR comes in handy.

Instead of compiling the entire method, we can choose to compile only a certain loop in the method when we find that a loop in a certain method is very hot. When the loop body executes to i = 5000, the loop counter reaches the threshold that triggers OSR compilation , After the compilation is complete, you can execute the generated code after compilation. So in the above example, when we execute the loop body for the second time, we are already executing the OSR compiled code, then the performance will be a little faster than the previous one.

The more specific implementation principles of OSR, this article will not go deep into this article. Interested students can read the knowledge of R. https://tinyurl.com/y3yxu8fc


Guess you like

Origin blog.51cto.com/15009384/2562916