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

How to understand unsafe

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What this article shares with you is about how to understand unsafe. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Unsafe originally means unsafe and dangerous in English. Roles in java are also unsafe and dangerous. It is the sun.msic in the java package, and it is not recommended to use it because it can directly manipulate the underlying JVM and modify memory. If it is not used properly, it may directly cause the server to crash. However, it has been well used in open source frameworks such as netty and hadoop, such as using unsafe for unlocked operations in netty. In addition, there is also a good use of unsafe in java's NIO, using unsafe in memory and directly opening up large chunks of out-of-heap memory for caching and communication, but it is easy to cause problems if you do not operate properly, because it is not in jvm, so there is no boundary check.

1.unsafe is singleton and globally unique. We can understand it through the source code.

You can also see here that only classes loaded by the main class loader can get a unsafe instance, and a normal call throws a SecurityException exception.

two。 If you want to use unsafe, you must first get the unsafe instance. There are many ways to get this instance on the Internet. I am used to using reflection, as shown in the following figure:

The efficacy of 3.unsafe

3.1 object instantiation

As far as I know, object instantiation is usually done with new or reflection. Here we use unsafe to create an object instance, and how to create the following figure

Here, an instance is created through unsafe, so that the constructor is skipped during creation, and unsafe can be considered if there is a need to skip the constructor.

3.2 modify jvm memory

Here you can find the physical address of a field in jvm memory in jvm, and then replace it, as shown in the figure below.

The above is how to understand unsafe, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report