Methods of Java Operating system hardware unsafe

Source: Internet
Author: User
Tags cas pack reflection shallow copy

Often Java code is run in the JVM without direct access to the system hardware such as memory allocation release, but if there is a need to skip the JVM directly with Java access to the system hardware, such as the C language pointer operation, you can invoke the unsafe object-related methods.

1, unsafe description

The unsafe class is under the Sun.misc package and is not part of the Java standard. However, many Java base class libraries, including some high-performance development libraries, are based on the unsafe class, such as Netty, Hadoop, Kafka, Juc, and the underlying AQS-dependent CAS operations and Locksupport classes. Unsafe plays a significant role in improving the efficiency of Java operations and enhancing Java's underlying operational capabilities. However, most of its operations are bypassed by the JVM through JNI, so it allocates memory needs manual free, excessive use of the unsafe class will make the chance of error increased, so it is very dangerous, Java officials do not recommend direct use.

2, the use of unsafe class

The unsafe class is a singleton, which can be obtained through the Getunsafe method, but internal checks the limit and only the classes loaded by the system ClassLoader can be called, or an exception will be thrown. You can use the JVM startup parameter-xbootclasspath to specify that the class you want to start is a startup class or an instance of it obtained through reflection "the unsafe class has a private member variable Theunsafe, and we can set its instance's Access property to true with reflection. It is then obtained through the Get method of the file.

Field f = Unsafe. class. Getdeclaredfield ("Theunsafe"); F.setaccessible (true= (Unsafe) f.get ( null);

After obtaining an instance of unsafe, we can implement the following functions

    1. Memory Management: Memory allocation, release, copy, etc., using CopyMemory method can realize a general object shallow copy method.
    2. Instantiation of unconventional objects: Using the Allocateinstance method, you can directly generate an instance object without calling the construction method and its initialization method, which is useful when deserializing objects, rebuilding and setting the final field instead of invoking a constructor method.
    3. Manipulate classes, objects, variables: Gets the object pointer, modifies the data that the pointer points to.
    4. Array manipulation: Use the unsafe class's memory allocation method to implement a very large array, but be careful to free up memory yourself.
    5. Multi-threaded synchronization: CAS operation, lock mechanism, locksupport pack, Unpark method is the final call to the unsafe pack, unpack method.
    6. Memory Barrier: Newly introduced in Java8, used to define memory barriers and avoid code rearrangement.

3. Change of unsafe in Java9

    1. The unsafe packet path is changed and moved to the Jdk.internal.misc package.
    2. Added a very detailed comment, add a static method, you can get the Theunsafe object directly.

Talk about the unsafe class of Java

79097682

Methods of Java Operating system hardware unsafe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.