MemoryLint Petr Nejedlý, Radim Kubacki SUN Microsystems, http://www.sun.com/, http://www.netbeans.org BOF-9066 2007 JavaOne SM Conference Session BOF-9066
Goal Get ideas how to analyze content of Java heap Find how to analyze data structures of your application. Check for the patterns that reveals memory leaks, inefficient use of memory or other mistakes 2007 JavaOne SM Conference Session BOF-9066 2
Agenda Motivation Existing tools Next step MemoryLint Q&A 2007 JavaOne SM Conference Session BOF-9066 3
Agenda Motivation Existing tools Next step MemoryLint Q&A 2007 JavaOne SM Conference Session BOF-9066 4
Motivation Why to use tools? Same errors are repeated They have to be checked and fixed again and again Patterns are often similar Evaluate larger data structures to get better picture of application behavior Memory consumption of module system, project infrastructure, editor's data 2007 JavaOne SM Conference Session BOF-9066 5
Motivation Why to use tools? What can we automate? Pattern detection in... sources: PMD, Checkstyle bytecode: FindBugs data:??? Find problems as soon as possible during development Make it easy for everyone to run the scan 2007 JavaOne SM Conference Session BOF-9066 6
Agenda Motivation Existing tools Next step MemoryLint Q&A 2007 JavaOne SM Conference Session BOF-9066 7
Existing tools What do we have now? Profilers Usually good for memory leak hunting Object count/sizes, retained size, reference path Requires skillset to find problems Hard to automate Generally can't do custom/advanced analysis 2007 JavaOne SM Conference Session BOF-9066 8
Existing tools What do we have now? JHat/OQL Good concept Get a heap dump and analyze it in many ways Difficult to use Slow on larger heaps 2007 JavaOne SM Conference Session BOF-9066 9
Agenda Motivation Existing tools Next step MemoryLint Q&A 2007 JavaOne SM Conference Session BOF-9066 10
MemoryLint What is it? Automatic analysis tool Processes rules, provides report Extensible framework Has API for writing custom rules Builds on top of NetBeans Profiler's heap model Experimental platform... What kind of problematic patterns do you encounter?... but usable out of the box Few precooked rules, will demo them 2007 JavaOne SM Conference Session BOF-9066 11
MemoryLint Overview JVM Memory Lint Results Heap dump Heap model Rules Profiler integration 2007 JavaOne SM Conference Session BOF-9066 12
MemoryLint What do we look for? Known problems Memory leaks Memory waste CPU optimizations Correctness problems Interresting statistics Who uses all that HashMaps? Your ideas 2007 JavaOne SM Conference Session BOF-9066 13
MemoryLint Rule examples Memory leaks UI widgets, documents, classloaders Improperly used WeakHashMap Memory waste String class: too many identical strings, oversized Retained memory Patterns HashMap with bad distribution of entries Cache statistics 2007 JavaOne SM Conference Session BOF-9066 14
MemoryLint More ideas Duplicate listener registrations Sparsely populated arrays or duplicated entries Duplicated fields Unused/should-be-static fields Instances of Boolean IdentityHashMap with equal but not identical key (catch String only for example) Listener strongly reachable from singleton only WeakHashMap with most keys gone 2007 JavaOne SM Conference Session BOF-9066 15
Rule Examples: String public final class String implements Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; /** The offset is the first index of the storage that is used private final int offset; /** The count is the number of characters in the String. */ private final int count; /** Cache the hash code for the string */ private int hash; // Default to 0... 2007 JavaOne SM Conference Session BOF-9066 16
Rule Examples: String // curroff is 5631, len is 3 String s = doccontent.substring (curroff, len); 2007 JavaOne SM Conference Session BOF-9066 17
Rule Examples: HashMap Chaining of entries with bad hash function HashMap table Entry[] Entry next Entry next Entry next 2007 JavaOne SM Conference Session BOF-9066 18
Rule Examples: LeakHashMap Weak key strongly reachable from value WeakHashMap table Entry[] Entry referent value value key heap 2007 JavaOne SM Conference Session BOF-9066 19
DEMO Samples 2007 JavaOne SM Conference Session BOF-9066 20
Rule's Anatomy public class MyRule extends IteratingRule { public MyRule() { super( name, desc, classnamepattern ); } /** Called once for each rule to prepare helpers. */ protected void preparerule(memorylint context) {...} /** Called once for each heap instance matching the * classnamepattern. Can be called paralelly from * several worker threads. */ protected void perform(instance in) {...} /** Called once all matching instances processed. Allows * to compute and push the results lately */ protected void summary() {...}... } 2007 JavaOne SM Conference Session BOF-9066 21
Summary Memory problems are common Memory problems repeat Solving memory problems needs to be simplified (automated) You may be surprised how much low hanging fruit can be hidden in your application... we were ourselves... 2007 JavaOne SM Conference Session BOF-9066 22
For More Information List http://performance.netbeans.org/memorylint/ http://profiler.netbeans.org/ 2007 JavaOne SM Conference Session BOF-9066 23
Q&A Petr Nejedlý petr.nejedly@sun.com Radim Kubacki radim.kubacki@sun.com 2007 JavaOne SM Conference Session BOF-9066 24