Unsafe Java

Post on 10-May-2015

6.441 views 1 download

Tags:

description

Overview of sun.misc.Unsafe class. Fun, dirty and maybe some practical examples.

Transcript of Unsafe Java

Unsafe Java

Misha Kozik@mishadoff

Average C developer

Java is Safe!

● No pointers● No direct memory allocation● Compile-time checking● Platform-independent types● Checked Exceptions● Garbage collection● etc.

Not really...

Not really...

sun.misc.Unsafe

“API” Overview

● allocateInstance● objectFieldOffset

Objects

Classes● staticFieldOffset● defineClass● defineAnonymousClass● ensureClassInitialized

Info● addressSize● pageSize

Arrays● arrayBaseOffset● arrayIndexScale

Memory

Synchronization● monitorEnter● tryMonitorEnter● monitorExit● compareAndSwapInt● putOrderedInt● park● unpark

● allocateMemory● copyMemory● freeMemory● getAddress● getInt● putInt

TOTAL: 105

Instantiation

“Trusted” code with bootloader

java -Xbootclasspath:/usr/jdk1.7.0/jre/lib/rt.jar:. com.mishadoff.magic.UnsafeClient

Reflection

Usage

Avoid Initialization

Memory Corruption

#define TRUE FALSE

Very Unchecked Cast

Dynamic Classes

sizeOf

sizeOf

Shallow Copy

Hide Password

Throw Exception

Fast Serialization

● Serializable (so slooooow) ● Externalizable (requires schema)● protobuf (dependency)● kryo (dependency)● Unsafe

Fast Serialization: Write

Fast Serialization: Read

Fast Serialization: Performance

Benchmarks: https://github.com/eishay/jvm-serializers/wiki

Super Array

Off-heap Collections

● Not limited to Heap capacity● Not limited to Integer.MAX_VALUE size● No boundary checks● Not under GC management● Partially available at java.nio.*● Useful for math computations● Realtime programming

“The price of greatness is responsibility.”– Winston Churchill

Concurrency

Concurrency: Client

Concurrency: Stupid Counter

Concurrency: Stupid Counter

Counter result: 99542945Time passed in ms: 679

Concurrency: Sync Counter

Concurrency: Sync Counter

Counter result: 100000000Time passed in ms: 10136

Concurrency: Lock Counter

Concurrency: Lock Counter

Counter result: 100000000Time passed in ms: 8065

Concurrency: Atomic Counter

Concurrency: Atomic Counter

Counter result: 100000000Time passed in ms: 6552

Concurrency: CAS CounterCAS = Compare And Swap

Concurrency: CAS CounterCAS = Compare And Swap

Counter result: 100000000Time passed in ms: 6454

Lock-free Data Structures

● Create a copy of current state● Modify it● CAS● Repeat if it fails

● Hard to implement.● Problem ABA (DCAS, LL/SC)● Reordering (Memory Barriers)

Problems

Intuition

And finally...

Unsafe.park()

“Block current thread, returning when a balancing unpark occurs, or a balancing unpark has already occurred, or the thread is interrupted, or, if not absolute and time is not zero, the given time nanoseconds have elapsed, or if absolute, the given deadline in milliseconds since Epoch has passed, or spuriously (i.e., returning for no "reason"). Note: This operation is in the Unsafe class only because unpark is, so it would be strange to place it elsewhere.”

– Javadoc

Disclaimer

● May be removed/changed● Not portable● Depends on architecture● Depends on JVM implementation● Depends on major/minor JVM● Never use it in production

Gentle reminder:

One error may cause JVM crash

Projects

Project LambdaProject Jigsaw

Links

● Javadoc http://www.docjar.com/docs/api/sun/misc/Unsafe.html

● My Blog http://mishadoff.github.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/

● Tricks with Direct Memory Accesshttp://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/

● StackOverflow http://stackoverflow.com/questions/5574241/interesting-uses-of-sun-misc-unsafe

● Native C/C++ Like Performance For Java Object Serialisation http://mechanical-sympathy.blogspot.de/2012/07/native-cc-like-performance-for-java.html

● Big Arrays in Java http://www.omsn.de/blog/big-arrays-in-java

● Lock-Free Wait-Free Hash Table http://www.azulsystems.com/about_us/presentations/lock-free-hash

● Java theory and practice: Going atomic www.ibm.com/developerworks/java/library/j-jtp11234/

● Fast multipurpose serialization (RU) http://www.javaspecialist.ru/2012/07/blog-post.html

Thanks!

Q?