Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the safe point of SafePoint?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is the SafePoint security point". In the daily operation, I believe many people have doubts about what the SafePoint security point is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the SafePoint security point?" Next, please follow the editor to study!

Summary of premises

Safepoint, also known as safe point, is an important concept in JVM such as hotspot. Let's take a closer look at what safepoint is, what safepoint does, how safepoint is implemented, and what you need to pay attention to as a developer.

What is the safe point of SafePoint?

The main task of JVM is to execute the Java program, and the JVM runtime itself is also a program, but in order to execute the Java program JVM, there are many auxiliary work, such as GC, JIT compilation and so on. Generally speaking, a user Java program running on JVM is called mutator.

GC as an example, the general GC in JVM uses reachability analysis, that is, starting from some GC Root of the application (such as local variable table in the method stack frame in the running thread stack, references in the Operand table, static variable references, etc.), and if mutator is also running during JVM traversal, mutator may modify the reference relationship of the object graph. If JVM does not handle this kind of concurrent modification, it may cause some non-recyclable objects not to be traversed, thus being marked as junk objects and being reclaimed incorrectly. Remember to mark objects that are alive, not to be cleared, and that they will be immediately recycled before they are traversed, and you need to consider the finalize method for further judgment.

The cost of implementing a fully concurrent GC,JVM will be high, and in many cases the overall throughput will be reduced. (Lenovo's use of cas atomic operations and locking in concurrent programming is more efficient if the competition is fierce, because it can reduce the cpu consumption of the cas loop.)

So there are some StopTheWorld phases in many GC collectors, and this StopTheWorld is safepoint. There are no mutator operands in safepoint, and the data type of each location in the thread stack and heap is determined (such as whether the data of a 8bit is long or object reference).

A thread is either in safepoint or not in safepoint. The StopTheWorld mentioned above refers to the global safepoint (for hotspot), which requires all threads to be in the safepoint state. If not specified later, safepoint also refers to the global safepoint.

How safepoint is implemented and how JVM notifies threads to enter the safepoint

In the hotspot implementation, safepoint is collaborative. When JVM needs mutator to enter safepoint, it sets a status flag to indicate that it is about to enter safepoint. Each mutator thread checks this status flag at the appropriate time, and pauses itself if it finds that it needs to enter safepoint.

The appropriate timing here should be chosen less frequently, avoid increasing runtime overhead (you can't look at it every step), avoid not checking for too long, and avoid entering the safepoint too slowly (you need threads to enter the safepoint state. If one thread has been working all the time, not checking safepoint will affect other threads, after all, other threads are waiting).

In the case of compiled code (JIT compiled code), JIT inserts check code somewhere, such as where the method call returns and where the loop jumps back.

If it is interpreted code (interpretive execution), JVM has two bytecode partitions, and if you need to enter safepoint, JVM will switch to the sub-publication with safepoint status check.

How do each thread check whether to enter the safepoint

To minimize overhead, the state check of safepoint in hotspot is achieved by reading a memory value, and if you need to enter safepoint, set the memory page to be protected, so that a page fault is triggered, and then you can enter safepoint through exception handling. This approach is lighter than accurately reading a memory value (such as an boolean data) (because memory synchronization is required)

You may think that if a thread is in sleep, the thread is also in safepoint when executing JNI code, and other "blocking" states are also in safepoint, such as Thread.sleep. If you want to exit a thread, you need JVM permission to exit safepoint, so that threads in sleep state will not suddenly run after other threads enter safepoint.

What are the situations that will cause safepoint

Except for some GC phases that need to be performed within the safepoint, other common operations are.

Revoke bias

(bias lock undo, frequently see bias lock revocation can consider turning off bias lock-XX:-UseBiasedLocking)

Dump thread stack

Whether through jstack or jmx, ThreadMXBean.getThreadInfo (in the case of maxDepth > 0) or Thread.getAllStackTraces, or other methods, safepoint will be triggered, which can lead to a long pause if there are too many threads.

Class redefinition

(retransform or redefine the class through the Instrument object)

Code deoptimization how to troubleshoot safepoint related problems add some parameters to the JVM startup parameters to print out the application pause and safepoint related information.

If the version of jdk8

-Xlog:gc*=info::time,tags,tid-Xlog:safepoint=info::time,tags An example of tid [2020-05-18T09:18:55.978-0800] [19459] [safepoint] Application time: 1.0038747 seconds [2020-05-18T09:18:55.978-0800] [19459] [safepoint] Entering safepoint region: ThreadDump [2020-05-18T09:18:55.980-0800] [19459] [safepoint] Leaving safepoint region [- 05-18T09:18:55.980-0800] [19459] [safepoint] Total time for which application threads were stopped: 0.0017502 seconds Stopping threads took: 0.0000312 seconds

This means that after the application thread runs for 1.0038747 seconds, the application thread is paused for 0.0017502 seconds because the ThreadDump starts to enter the safepoint, and it takes 0.0000312 seconds to suspend these threads.

At this point, the study on "what is the security point of SafePoint" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report