unsafe java

45
Unsafe Java Misha Kozik @mishadoff

Upload: misha-kozik

Post on 10-May-2015

6.443 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Unsafe Java

Unsafe Java

Misha Kozik@mishadoff

Page 2: Unsafe Java

Average C developer

Page 3: Unsafe Java

Java is Safe!

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

Page 4: Unsafe Java

Not really...

Page 5: Unsafe Java

Not really...

sun.misc.Unsafe

Page 6: Unsafe Java

“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

Page 7: Unsafe Java

Instantiation

Page 8: Unsafe Java

“Trusted” code with bootloader

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

Page 9: Unsafe Java

Reflection

Page 10: Unsafe Java

Usage

Page 11: Unsafe Java

Avoid Initialization

Page 12: Unsafe Java

Memory Corruption

Page 13: Unsafe Java

#define TRUE FALSE

Page 14: Unsafe Java

Very Unchecked Cast

Page 15: Unsafe Java

Dynamic Classes

Page 16: Unsafe Java

sizeOf

Page 17: Unsafe Java

sizeOf

Page 18: Unsafe Java

Shallow Copy

Page 19: Unsafe Java

Hide Password

Page 20: Unsafe Java

Throw Exception

Page 21: Unsafe Java

Fast Serialization

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

Page 22: Unsafe Java

Fast Serialization: Write

Page 23: Unsafe Java

Fast Serialization: Read

Page 24: Unsafe Java

Fast Serialization: Performance

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

Page 25: Unsafe Java

Super Array

Page 26: Unsafe Java

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

Page 27: Unsafe Java

Concurrency

Page 28: Unsafe Java

Concurrency: Client

Page 29: Unsafe Java

Concurrency: Stupid Counter

Page 30: Unsafe Java

Concurrency: Stupid Counter

Counter result: 99542945Time passed in ms: 679

Page 31: Unsafe Java

Concurrency: Sync Counter

Page 32: Unsafe Java

Concurrency: Sync Counter

Counter result: 100000000Time passed in ms: 10136

Page 33: Unsafe Java

Concurrency: Lock Counter

Page 34: Unsafe Java

Concurrency: Lock Counter

Counter result: 100000000Time passed in ms: 8065

Page 35: Unsafe Java

Concurrency: Atomic Counter

Page 36: Unsafe Java

Concurrency: Atomic Counter

Counter result: 100000000Time passed in ms: 6552

Page 37: Unsafe Java

Concurrency: CAS CounterCAS = Compare And Swap

Page 38: Unsafe Java

Concurrency: CAS CounterCAS = Compare And Swap

Counter result: 100000000Time passed in ms: 6454

Page 39: Unsafe Java

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

Page 40: Unsafe Java

And finally...

Page 41: Unsafe Java

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

Page 42: Unsafe Java

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

Page 43: Unsafe Java

Projects

Project LambdaProject Jigsaw

Page 44: Unsafe Java

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

Page 45: Unsafe Java

Thanks!

Q?