当前位置:网站首页>Use of unsafe class
Use of unsafe class
2022-07-03 18:03:00 【Spring comes again】
Unsafe The use of the class
Unsafe It can be used to directly access the system memory resources and manage them independently , In the promotion Java Operational efficiency 、 enhance Java The language bottom level operation ability aspect has played the very big role —— It can be said that ,Unsafe Class is Java The back door left in , Provides some low-level operations , Such as direct memory access 、 Thread scheduling, etc .
Unsafe Do not belong to Java standard . The official does not recommend the use of Unsafe, And from JDK 9 Start , Official start Unsafe!
relevant Issue:https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6852936
therefore ,Unsafe Class for project practice , It doesn't mean much . However, at present, there are many easy-to-use class libraries in the industry Unsafe class , for example java.util.concurrent.atomic
A bunch of classes in the bag 、Netty、Hadoop、Kafka etc. . So it's good to know .
Different JDK In the version ,Unsafe Classes are also different , for example :
- stay JDK 8 Belong to sun.misc It's a bag ;
- stay JDK 11 Belong to sun.misc Package or jdk.internal.misc Next , among jdk.internal.misc Under the Unsafe Class is more powerful .( Should be from JDK 9 At the beginning , The author did not test it personally )
Quick start
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class DirectMemoryTest1 {
private static final int _1MB = 1024 * 1024;
public static void main(String[] args) throws IllegalAccessException {
// Get by reflection Unsafe Class and allocate direct memory through it
Field unsafeField = Unsafe.class.getDeclaredFields()[0];
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
int i = 0;
while (true) {
unsafe.allocateMemory(_1MB);
System.out.println(++i);
}
}
}
Unsafe The use of the class
See :
- https://www.jb51.net/article/140726.htm
- https://blog.csdn.net/ahilll/article/details/81628215
- https://www.jianshu.com/p/dd2be4d3b88e
JDK 11 How to use Unsafe class
TIPS
- Again , In actual projects , Unless it is necessary , Try not to use Unsafe class , The official does not recommend the use of
- from JDK 10 Start ,Unsafe Some functions of class have been VarHandle replace , Suggest using VarHandle
Create a JDK 11 Project
Create under the project source code path
module-info.java
, The contents are as follows :module unsafe { requires jdk.unsupported; }
After creation , The code structure is as follows :
|____src | |____main | | |____java | | | |____module-info.java | | | |____com | | | | |____example | | | | | |____demo | | | | | | |____UnsafePlayer.java | | | | | | |____DirectMemoryTest1.java
Test code :
import sun.misc.Unsafe; import java.lang.reflect.Field; public class DirectMemoryTest1 { private static final int _1MB = 1024 * 1024; public static void main(String[] args) throws IllegalAccessException { // Get by reflection Unsafe Class and allocate direct memory through it Field unsafeField = Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); Unsafe unsafe = (Unsafe) unsafeField.get(null); int i = 0; while (true) { unsafe.allocateMemory(_1MB); System.out.println(++i); } } }
Reference documents
边栏推荐
- [combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
- SDNUOJ1015
- SQL injection database operation foundation
- Embedded-c language-7
- [combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
- Records of long objects and long judgments in the stream of list
- 面试官:值为 nil 为什么不等于 nil ?
- [combinatorics] generating function (commutative property | derivative property | integral property)
- Closure and closure function
- Managing multiple selections with MVVM - managing multiple selections with MVVM
猜你喜欢
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
Win32: dump file analysis of heap corruption
MySQL has been stopped in the configuration interface during installation
Computer graduation design PHP sports goods online sales system website
Module 9 operation
How to expand the capacity of golang slice slice
聊聊支付流程的設計與實現邏輯
Leetcode Valentine's Day Special - looking for a single dog
聊聊支付流程的设计与实现逻辑
Five problems of database operation in commodity supermarket system
随机推荐
Redis core technology and practice - learning notes (IX): slicing cluster
Embedded-c language-7
c# . Net tool ecosystem
Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
远程办公工具分享|社区征文
Kotlin's collaboration: Context
Redis core technology and practice - learning notes (VII) sentinel mechanism
How to draw non overlapping bubble chart in MATLAB
小程序 多tab 多swiper + 每个tab分页
PHP MySQL order by keyword
Golang string (string) and byte array ([]byte) are converted to each other
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
Leetcode 108 converts an ordered array into a binary search tree -- recursive method
Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
win32:堆破壞的dump文件分析
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
圖像24比特深度轉8比特深度
Introduction to PHP MySQL
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition