Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Unsafe Java

9,341 views

Published on

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

Published in: Technology
  • Follow the link, new dating source: ♥♥♥ http://bit.ly/2F4cEJi ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sex in your area is here: ♥♥♥ http://bit.ly/2F4cEJi ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Unsafe Java

  1. 1. Unsafe Java Misha Kozik @mishadoff
  2. 2. Average C developer
  3. 3. Java is Safe! ● No pointers ● No direct memory allocation ● Compile-time checking ● Platform-independent types ● Checked Exceptions ● Garbage collection ● etc.
  4. 4. Not really...
  5. 5. Not really... sun.misc.Unsafe
  6. 6. “API” OverviewInfo Synchronization ● addressSize ● monitorEnter ● pageSize ● tryMonitorEnter ● monitorExitObjects ● compareAndSwapInt ● allocateInstance ● putOrderedInt ● objectFieldOffset ● park ● unparkClasses ● staticFieldOffset Memory ● defineClass ● allocateMemory ● defineAnonymousClass ● copyMemory ● ensureClassInitialized ● freeMemory ● getAddressArrays ● getInt ● arrayBaseOffset ● putInt ● arrayIndexScale TOTAL: 105
  7. 7. Instantiation
  8. 8. “Trusted” code with bootloaderjava -Xbootclasspath:/usr/jdk1.7.0/jre/lib/rt.jar:.com.mishadoff.magic.UnsafeClient
  9. 9. Reflection
  10. 10. Usage
  11. 11. Avoid Initialization
  12. 12. Memory Corruption
  13. 13. #define TRUE FALSE
  14. 14. Very Unchecked Cast
  15. 15. Dynamic Classes
  16. 16. sizeOf
  17. 17. sizeOf
  18. 18. Shallow Copy
  19. 19. Hide Password
  20. 20. Throw Exception
  21. 21. Fast Serialization● Serializable (so slooooow)● Externalizable (requires schema)● protobuf (dependency)● kryo (dependency)● Unsafe
  22. 22. Fast Serialization: Write
  23. 23. Fast Serialization: Read
  24. 24. Fast Serialization: PerformanceBenchmarks: https://github.com/eishay/jvm-serializers/wiki
  25. 25. Super Array
  26. 26. 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
  27. 27. Concurrency
  28. 28. Concurrency: Client
  29. 29. Concurrency: Stupid Counter
  30. 30. Concurrency: Stupid Counter Counter result: 99542945 Time passed in ms: 679
  31. 31. Concurrency: Sync Counter
  32. 32. Concurrency: Sync Counter Counter result: 100000000 Time passed in ms: 10136
  33. 33. Concurrency: Lock Counter
  34. 34. Concurrency: Lock Counter Counter result: 100000000 Time passed in ms: 8065
  35. 35. Concurrency: Atomic Counter
  36. 36. Concurrency: Atomic Counter Counter result: 100000000 Time passed in ms: 6552
  37. 37. Concurrency: CAS Counter CAS = Compare And Swap
  38. 38. Concurrency: CAS Counter CAS = Compare And Swap Counter result: 100000000 Time passed in ms: 6454
  39. 39. Lock-free Data Structures Intuition ● Create a copy of current state ● Modify it ● CAS ● Repeat if it fails Problems ● Hard to implement. ● Problem ABA (DCAS, LL/SC) ● Reordering (Memory Barriers)
  40. 40. And finally...
  41. 41. Unsafe.park()“Block current thread, returning when a balancingunpark occurs, or a balancing unpark has alreadyoccurred, or the thread is interrupted, or, if notabsolute and time is not zero, the given timenanoseconds have elapsed, or if absolute, the givendeadline in milliseconds since Epoch has passed, orspuriously (i.e., returning for no "reason"). Note:This operation is in the Unsafe class only becauseunpark is, so it would be strange to place itelsewhere.” – Javadoc
  42. 42. 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
  43. 43. ProjectsProject LambdaProject Jigsaw
  44. 44. 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 Access http://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
  45. 45. Thanks! Q?

×