Sperasoft talks j point 2015
-
Upload
sperasoft -
Category
Technology
-
view
60 -
download
3
Transcript of Sperasoft talks j point 2015
![Page 1: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/1.jpg)
JPoint 2015 Conference
overview
![Page 2: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/2.jpg)
Memory leaks profiling basics
Some notes about java.lang.String class
What technical debt is. How to measure it and how to sell.
Regular expressions under hood
Conclusion
Agenda
![Page 3: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/3.jpg)
Memory Leaks
![Page 4: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/4.jpg)
Different regions of memory
Default sizes
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal –version
Non-default sizes
-Xmx2g -Xms2g -XX:MaxPermSize=128m
-XX:MaxPermSize=128m
Garbage Collector
JVM subsystem responsible for object allocation and removing
OutOfMemoryError
JVM and Memory
![Page 5: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/5.jpg)
Unused object - object with no reference on it
References:
From an object to the value of one of its instance fields
From an array to one of its elements
From an object to its class
From a class to its class loader
From a class to the value of one of its static fields
From a class to its superclass
GC roots - special objects, always considered to be alive
Reachability
Memory Leaks: Unused Objects
![Page 6: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/6.jpg)
Memory Leaks
Caches without look-ups and eviction
Immortal threads
Unclosed IO streams
String.substring()
NOT Memory Leaks
Too high allocation rate
Cache with wrong size
Trying to load too much data at once
Fat data structures
Memory Leaks: Examples
![Page 7: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/7.jpg)
JVM Options
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-Xloggc:file.log
-XX:+UseGCLogFileRotation
-XX:NumberOfGClogFiles=N
GC Logs Analyzers
GCViewer
Fasterj
Memory Leaks: GC Logs
![Page 8: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/8.jpg)
Binary representation of objects graph written to a file
One of the best ways to find out what consumes memory
Getting memory dump:
jmap -dump:format=b,file=heap.hprof
(!) Stop The World, 1 Thread, 4Gb ~4 sec
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./java_pid<pid>.hprof
Get as late as possible
Memory dump analyze tool:
MAT
Memory Leaks: Memory Dump
![Page 9: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/9.jpg)
Shallow – size of the object itself with headers and fields but without
fields values
Deep – size of the object itself with headers, fields and fields values
Retained – the sub-graph size that will be unreached if we remove the
object
Memory Leaks: Object Size
![Page 10: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/10.jpg)
Memory Leaks: MAT
![Page 11: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/11.jpg)
Some Notes about java.lang.String
![Page 12: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/12.jpg)
Immutable
25% of objects are java.lang.String
String concat vs StringBuilder (3250 op/sec vs 125000 op/sec)
-XX:±OptimizeStringConcat
String
OFFSET SIZE TYPE DESCRIPTION
0 12 object header
12 4 char[] value
16 4 int hash
20 4 alligmeent loss
![Page 13: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/13.jpg)
(“” + x) is faster then Integer.toString(x) (!) -XX:+OptimizeStringConcat
with side effects optimization is not working
string in JDK8 have no seek and count properties
do not use .intern() method as cache
StringTable is overflowed and can not be resized
-XX :+ PrintStringTableStatistics
equals implementation is intrinsic function (chose by compiler)
-XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=::_equals
String: Notes
![Page 14: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/14.jpg)
String: HashCode Example
String str1 , str2 ; public void setup () { str1 = “superinstrument␣neglected"; // same length str2 = " neglected␣ superinstrument "; // same length } int test1 () { return str1 . hashCode (); } int test2 () { return str2 . hashCode (); }
![Page 15: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/15.jpg)
Uses polynomial function
Collisions (the same result for different values)
String: HashCode
public int hashCode () { int h = hash ; if (h == 0) { for ( char v : value ) { h = 31 * h + v; } hash = h; } return h; }
![Page 16: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/16.jpg)
String: Regexp String text = “Funny␣girl␣danced␣fell␣hurt␣and␣cried␣aloud."; String textDup = text.replaceAll ("␣", "␣␣"); Pattern pattern = Pattern.compile ("␣␣"); String [] charSplit () { return text.split ("␣"); } //191.6 ns/op String [] strSplit () { return textDup.split ("␣␣"); } //527.9 ns/op String [] strSplit_pattern () { return pattern.split(textDup); } //416.2 ns/op
charSplit: fast path for 1 byte symbols
strSplit: internally creates Pattern
strSplit_pattern: reuse Pattern
![Page 17: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/17.jpg)
Technical debt
![Page 18: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/18.jpg)
Try to not create a technical debt
Code review
Precision estimates
Bus factor (save information)
Try to measure the date your application will start suffer from this debt
Collect metrics as much as possible
Try to build trend
Fishy code
Technical Debt
![Page 19: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/19.jpg)
Include small refactoring in estimates
Formal code quality metrics (clear for you, not so clear for customer)
Developers group motivation (to not lose)
Technical Debt: Selling
![Page 20: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/20.jpg)
Regular expressions under hood
![Page 21: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/21.jpg)
Backtrack
/^.*ab$/ matches “ab”
/^.*?b$/ matches “aab”
can took 30 sec in 100Kb text
Pattern.compile("(Joker|JPoint)+")
String "JokerJPoint…700 times…Joker“ will cause StackOverflowError
Solutions:
Pattern.compile("(Joker|JPoint)++") – possessive
Pattern.compile("(?>Joker|JPoint)+") – forget back-reference
Regular Expressions: Backtrack
![Page 22: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/22.jpg)
Regular Expressions: Impl
TITLE RELEASED ALGORITHM
java.util.regex.Pattern 2015 Backtrack
com.basistech.tclre 2015 DFA
org.jruby.joni (Nashorn) 2014 Backtrack
Apache Oro 2000 Backtrack
JRegex 2013 backtrack
Deterministic automa
Non-deterministic automa
![Page 23: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/23.jpg)
Regular Expressions: Test
/(x+x+)+y/.test("xxxxx……xxx")
![Page 24: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/24.jpg)
Regular Expressions: Tradeoff
Java pattern
Non-deterministic response time
Managed backtrack
Low memory usage
TCL
DFA algorithm with deterministic response time
High memory usage
![Page 25: Sperasoft talks j point 2015](https://reader034.fdocuments.net/reader034/viewer/2022052701/55d2203dbb61eb9c4d8b45de/html5/thumbnails/25.jpg)
Visit Sperasoft online:
www/sperasoft.com www.facebook.com/Sperasoft
www.twitter.com/sperasoft
Thanks!