In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about Java direct memory access skills, the editor thinks it is very practical, so share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
Skills of Java Direct memory access
Java is designed to be a secure, manageable environment, while Java HotSpot has a back door that provides low-level, direct memory and thread operations. The back door is-- sun.misc.Unsafe. This class has a wide range of applications in JDK, such as java.nio and java.util.concurrent. It's hard to imagine using these dangerous, non-portable, and unverified API in daily development. However, Unsafe provides an easy way to look at some of the techniques within HotSpot JVM.
Get Unsafe
Access to the class sun.misc.Unsafe is restricted, its constructors are private, and the corresponding factory methods must be loaded by Bootloader before they can be used, that is, only parts of the JDK can use this factory method to construct Unsafe objects.
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
Public final class Unsafe {
...
Private Unsafe () {}
Private static final Unsafe theUnsafe = new Unsafe ()
...
Public static Unsafe getUnsafe () {
Class cc = sun.reflect.Reflection.getCallerClass (2)
If (cc.getClassLoader ()! = null)
Throw new SecurityException ("Unsafe")
Return theUnsafe
}
...
}
Fortunately, there is a theUnsafe property that can be used to retrieve the Unsafe instance, and we can see that we can write a reflection method to get the Unsafe instance:
one
two
three
four
five
six
seven
Public static Unsafe getUnsafe () {
Try {
Field f = Unsafe.class.getDeclaredField ("theUnsafe")
F.setAccessible (true)
Return (Unsafe) f.get (null)
} catch (Exception e) {/ *... * /}
}
Now we will learn some methods of Unsafe.
1.long getAddress (long address) and void putAddress (long address, long x)
Read and write to direct memory.
2.int getInt (Object o, long offset), void putInt (Object o, long offset, int x)
Another similar method reads and writes direct memory, converting C language structures and Java objects.
3.long allocateMemory (long bytes)
This can be seen as a wrapper for the C language's malloc () function.
Sizeof () function
The structure of the Java object is shown in the following figure:
The first trick is to simulate the C language's sizefo () function, which returns the byte size of the object. We can implement the sizeof () function with the following code:
one
two
three
four
five
six
seven
eight
nine
Public static long sizeOf (Object object) {
Unsafe unsafe = getUnsafe ()
Return unsafe.getAddress (normalize (unsafe.getInt (object, 4L)) + 12L)
}
Public static long normalize (int value) {
If (value > = 0) return value
Return (~ 0L > 32) & value
}
We need to use the normalize () function, because if the memory address is between 2 ^ 31 and 2 ^ 32, the adjacent integers will be automatically overwritten, that is, they will be stored as complements. Let's test it in 32-bit JVM (JDK6 or 7):
one
two
three
four
five
/ / execute sizeOf (new MyStructure ()) to get the following result:
Class MyStructure {} / / 8: 4 (start tag) + 4 (pointer to class)
Class MyStructure {int x;} / 16: 4 (start tag) + 4 (pointer to class) + 4 (int) + 4 padding bytes to align 64-bit blocks
Class MyStructure {int x; int y;} / / 16: 4 (start tag) + 4 (pointer to class) + 2x4
Direct memory management
Unsafe allows memory to be allocated and reclaimed for display through allcateMemory and freeMemory methods. The memory allocated directly is not under the control of GC and is not limited to the size of the JVM heap. In general, these methods are safe and effective through NIO's out-of-heap constraint buffering, but interestingly, this makes it possible for standard Java references to map non-heap memory:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
MyStructure structure = new MyStructure (); / / create a test object
Structure.x = 777
Long size = sizeOf (structure)
Long offheapPointer = getUnsafe () .allocateMemory (size)
GetUnsafe (). CopyMemory
Structure, / / source object
0, / / source offset is zero-copy an entire object
Null, / / destination is specified by absolute address, so destination object is null
OffheapPointer, / / destination address
Size
) / / test object was copied to off-heap
Pointer p = new Pointer (); / / Pointer is just a handler that stores address of some object
Long pointerOffset = getUnsafe () .objectFieldOffset (Pointer.class.getDeclaredField ("pointer"))
GetUnsafe () .putLong (p, pointerOffset, offheapPointer); / / set pointer to off-heap copy of the test object
Structure.x = 222; / / rewrite x value in the original object
System.out.println ((MyStructure) p.pointer) .x); / / prints 777
....
Class Pointer {
Object pointer
}
So, in fact, real objects can be allocated and reclaimed, not just byte buffering in NIO. Of course, a big problem is that GC will happen after such memory spoofing.
Inherits from the Final class and void*
Imagine having a method that takes String as a parameter, but it needs to be overloaded externally. The specific code is as follows:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
Carrier carrier = new Carrier ()
Carrier.secret = 777
String message = (String) (Object) carrier; / / ClassCastException
Handler (message)
...
Void handler (String message) {
System.out.println ((Carrier) (Object) message) .secret)
}
...
Class Carrier {
Int secret
}
In order for this code to work, you first need to change the Carrier class to pretend to be a subclass of String. The superclasses list is stored in the location of the Carrier class structure 28, as shown in the previous figure. In principle, add the following code to convert Carrer to String:
one
two
three
Long carrierClassAddress = normalize (unsafe.getInt (carrier, 4L))
Long stringClassAddress = normalize (unsafe.getInt (", 4L))
Unsafe.putAddress (carrierClassAddress + 32, stringClassAddress); / / insert pointer to String class to the list of Carrier's superclasses
In this way, the type conversion works properly. However, this kind of conversion is inappropriate and violates the virtual machine specification. A more detailed approach will include the following steps:
1. The position of 32 in the Carrier class actually contains a pointer to the Carrier class itself, so the pointer will be moved to the position 36, not just rewritten to the String class.
two。 When Carrier inherits from String, the final tag of the String class is removed.
Sun.misc.Unsafe provides almost unlimited ability to monitor and modify virtual machine runtime data structures. Although these capabilities are almost irrelevant to Java development itself, Unsafe is a great tool for people who want to learn about HotSpot virtual machines but don't have C++ code debugging, or need to create special analysis tools.
These are the skills of Java direct memory access, and 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.