Java Performance Boost
-
Upload
globallogic-ukraine -
Category
Software
-
view
1.540 -
download
0
Transcript of Java Performance Boost
![Page 1: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/1.jpg)
©2015 GlobalLogic Inc.
![Page 2: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/2.jpg)
2
Java Performance Boost
By Andrii Antilikatorov
![Page 3: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/3.jpg)
3
![Page 4: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/4.jpg)
4
Optimize or not optimize?
![Page 5: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/5.jpg)
5
Rule
![Page 6: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/6.jpg)
6
![Page 7: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/7.jpg)
7
![Page 8: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/8.jpg)
8
Avoid usage byte[] to String constructor (Java 6)
![Page 9: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/9.jpg)
9
Avoid usage byte[] to String constructor (Java 6)
• Update to Java 7 or later• Minimize usage of constructor
![Page 10: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/10.jpg)
10
Understanding of “switch on String”
![Page 11: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/11.jpg)
11
String Internation
String
String
StringString
String
String
String
String
String
String
StringString
Do not use String.intern() on Java 6
Internation is efficient on Java 7 and above
Works great in multithreaded code
JVM string pool is NOT thread localString
![Page 12: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/12.jpg)
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
StringString
String
String
String
String
String
String
StringString
String
![Page 13: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/13.jpg)
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
StringString
String
String
String
String
String
String
StringString
String
String before Java 7 update 6
• char [] symbols;• Offset• Count• Hash code
![Page 14: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/14.jpg)
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
StringString
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…
![Page 15: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/15.jpg)
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
StringString
String
String
String
String
String
String
StringString
String
String after Java 7 update 6
• char [] symbols;• Hash code
![Page 16: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/16.jpg)
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
StringString
String
String
String
String
String
String
StringString
String• Serial• Parallel • Concurrent Mark Sweep (CMS) • Garbage-First (G1)
![Page 17: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/17.jpg)
17
Avoid heavy logic in static constructors
![Page 18: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/18.jpg)
18
Avoid heavy logic in static constructors
![Page 19: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/19.jpg)
19
Avoid heavy logic in static constructors
![Page 20: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/20.jpg)
20
Problem with inner classes
![Page 21: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/21.jpg)
21
Problem with inner classes
![Page 22: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/22.jpg)
22
Cost of method calls
invokestatic
invokevirtual
invokeinterface
invokespecial
![Page 23: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/23.jpg)
23
Cost of method calls
invokestatic
invokevirtual
invokeinterface
invokespecial
Dependency Injection
![Page 24: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/24.jpg)
24
Maps & Sets - Hints
Set:
Map:
![Page 25: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/25.jpg)
25
Maps & Sets - Hints
• emptyList/emptyMap/emptySet - for empty collections
• singletonList/singletonMap/singletonSet - for collections containing a single element/pair
![Page 26: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/26.jpg)
26
Performance of Date/Time entities
java.util.Date java.util.GregorianCalendar
![Page 27: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/27.jpg)
27
Performance of Date/Time entities
long
Int[]
InternationalizationArithmetic
operation
support
24 bytes 448 bytes
java.util.Date java.util.GregorianCalendar
![Page 28: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/28.jpg)
28
Performance of Date/Time entities
long
Int[]
InternationalizationArithmetic
operation
support
24 bytes 448 bytes
java.util.Date java.util.GregorianCalendar
![Page 29: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/29.jpg)
29
Synchronized block or synchronized method?
![Page 30: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/30.jpg)
30
Synchronized block or synchronized method?
0: aload_0 1: getfield 2: nop 3: iconst_m1 4: ireturn
![Page 31: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/31.jpg)
31
Synchronized block or synchronized method?
0: aload_0 1: dup 2: astore_1 3: monitorenter 4: aload_0 5: getfield 6: nop 7: iconst_m1 8: aload_1 9: monitorexit 10: ireturn 11: astore_2 12: aload_1 13: monitorexit 14: aload_2 15: athrow
![Page 32: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/32.jpg)
32
Thread-safe variants of List and Set
Manual synchronization of operations with collections
Use CopyOnWrite* collections
![Page 33: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/33.jpg)
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
![Page 34: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/34.jpg)
34
Synchronized or volatile variables?
MESI Protocol• Modified• Exclusive• Shared• Invalid
![Page 35: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/35.jpg)
35
Summary
Architecture first
Remember rule 80/20
Don’t try to write ideal code from the beginning
Don’t create bicycles
Don’t optimize for particular platform
![Page 36: Java Performance Boost](https://reader036.fdocuments.net/reader036/viewer/2022062412/58e7dfb41a28ab3a578b60e7/html5/thumbnails/36.jpg)
©2015 GlobalLogic Inc.