There are many factors that may lead to slow performance of Java code. This presentation helps identify and overcome them.
This presentation by Andrii Antilikatorov (Lead Software Engineer, GlobalLogic) was delivered at Java.io 3.0 conference in Kharkiv on March 23, 2016, and GlobalLogic Mykolaiv Java Conference on June 11, 2016.
12. 12
String Deduplication
Available from Java 8 update 20
Requires G1 collector to be
enabled. Optional phase of G1.
Unlikely to run on the loaded systems
Processes only strings that survived
after few garbage collections
Doesn’t mean you shouldn’t use
internation
String
String
String
String
String
String
String
String
String
String
StringString
String
13. 13
String Deduplication
Available from Java 8 update 20
Requires G1 collector to be
enabled. Optional phase of G1.
Unlikely to run on the loaded systems
Processes only strings that survived
after few garbage collections
Doesn’t mean you shouldn’t use
internation
String
String
String
String
String
String
String
String
String
String
StringString
String
String before Java 7 update 6
• char [] symbols;
• Offset
• Count
• Hash code
14. 14
String Deduplication
Available from Java 8 update 20
Requires G1 collector to be
enabled. Optional phase of G1.
Unlikely to run on the loaded systems
Processes only strings that survived
after few garbage collections
Doesn’t mean you shouldn’t use
internation
String
String
String
String
String
String
String
String
String
String
StringString
String
String before Java 7 update 6
• char [] symbols;
• Offset
• Count
• Hash code
This is very very long string which consumes a lot of memory…
This is very very long string which consumes a lot of memory…
15. 15
String Deduplication
Available from Java 8 update 20
Requires G1 collector to be
enabled. Optional phase of G1.
Unlikely to run on the loaded systems
Processes only strings that survived
after few garbage collections
Doesn’t mean you shouldn’t use
internation
String
String
String
String
String
String
String
String
String
String
StringString
String
String after Java 7 update 6
• char [] symbols;
• Hash code
16. 16
String Deduplication
Available from Java 8 update 20
Requires G1 collector to be
enabled. Optional phase of G1.
Unlikely to run on the loaded systems
Processes only strings that survived
after few garbage collections
Doesn’t mean you shouldn’t use
internation
String
String
String
String
String
String
String
String
String
String
StringString
String
• Serial
• Parallel
• Concurrent Mark Sweep (CMS)
• Garbage-First (G1)
25. 25
Maps & Sets - Hints
• emptyList/emptyMap/emptySet - for empty
collections
• singletonList/singletonMap/singleton
Set - for collections containing a single
element/pair
32. 32
Thread-safe variants of List and Set
Manual synchronization of
operations with collections
Use CopyOnWrite*
collections
33. 33
Synchronized or volatile variables?
Volatile variables are less expensive than synchronized
Volatile are thread-safe for atomic operations
Volatile acquires lock on variable, therefore not thread- safe
for non-atomic operations
Synchronized blocks have no performance impact if no
synchronization required