Java performance mistakes

Slides:



Advertisements
Similar presentations
Java Virtual Machine Profiling
Advertisements

Keeping our websites running - troubleshooting with Appdynamics Benoit Villaumie Lead Architect Guillaume Postaire Infrastructure Manager.
Memory issues in production systems. Production system Restricted access Application, DB, Application server, log files Debugging, monitoring Investigation.
1 Advanced Data Structures. 2 Topics Data structures (old) stack, list, array, BST (new) Trees, heaps, union-find, hash tables, spatial, string Algorithm.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Java Security Updated May Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
1 Testing Concurrent Programs Why Test?  Eliminate bugs?  Software Engineering vs Computer Science perspectives What properties are we testing for? 
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Web/App Performance How to keep you out of the News
Computer Measurement Group, India Optimal Design Principles for better Performance of Next generation Systems Balachandar Gurusamy,
Meet with the AppEngine Márk Gergely eu.edge. What is AppEngine? It’s a tool, that lets you run your web applications on Google's infrastructure. –Google's.
Lecture 10 : Introduction to Java Virtual Machine
Ideas to Improve SharePoint Usage 4. What are these 4 Ideas? 1. 7 Steps to check SharePoint Health 2. Avoid common Deployment Mistakes 3. Analyze SharePoint.
Problem of the Day  Why are manhole covers round?
Click to add text © 2012 IBM Corporation Design Manager Server Instrumentation Instrumentation Data Documentation Gary Johnston, Performance Focal Point,
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
© 2006, National Research Council Canada © 2006, IBM Corporation Solving performance issues in OTS-based systems Erik Putrycz Software Engineering Group.
MSE Portfolio Presentation 1 Doug Smith November 13, 2008
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
CS321 Data Structures Jan Lecture 2 Introduction.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Object Lifetime and Pointers
CSC 222: Object-Oriented Programming
Pro-Active Performance Engineering
Recursion Topic 5.
Analysis: Algorithms and Data Structures
The Data Types and Data Structures
Java 9: The Quest for Very Large Heaps
Arab Open University 2nd Semester, M301 Unit 5
Testing and Debugging.
COMP280:Introduction to Software Development Week 12, Lecture 34
Lecture 3 of Computer Science II
Concepts of programming languages
Secure Software Development: Theory and Practice
Software engineering – 1
Algorithm Analysis CSE 2011 Winter September 2018.
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Memory leaks in Android. Introduction In our day-to da life many apps crash,and the reason one will point to it is memory leak In majority of the cases.
Load Testing January 2018 René Ernst.
Software Architecture in Practice
Java Virtual Machine Complete subject details are available at:
Cracking the Coding Interview
Compiler Construction
Data Structures and Algorithms
Building Java Programs
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Hashing CS2110.
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Adaptive Code Unloading for Resource-Constrained JVMs
Sridhar Narayan Java Basics Sridhar Narayan
Building Java Programs
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Performance Evaluation
(Computer fundamental Lab)
CS-3013 Operating Systems Hugh C. Lauer
CSE 373: Data Structures and Algorithms
M S COLLEGE ART’S, COMM., SCI. & BMS
CSE 332: Concurrency and Locks
Automating Memory Management
Java Virtual Machine Profiling. Agenda Introduction JVM overview Performance concepts Monitoring Profiling VisualVM demo Tuning Conclusions.
Presentation transcript:

Java performance mistakes Peter Veres Senior Software Engineer Peter_Veres2@epam.com

Common performace issues Performance is a business goal Most performance issues in Java can be attributed to a handful of root causes

Performance failures are exepensive 2012, Knight Capital: The most expensive faulire Instead of shutting down gracefully, computers began issuing commands to buy and sell securities It only took a half hour, but by the end the losses totaled an estimated $440 million Many similar failures aren’t as visible Only slow down or halt a website without leaving much of a trail The customers show up, click in vain, then move on http://www.bloomberg.com/news/articles/2012-08-02/knight-shows-how-to-lose-440-million-in-30-minutes http://www.bloomberg.com/news/articles/2012-11-28/getco-proposes-buying-knight-capital-at-3-50-a-share In 2012, Knight Capital’s computers started to fail in the worst possible way. Instead of shutting down gracefully, they began issuing commands to buy and sell securities. The orders were queued up to be executed over the coming weeks, but the errant computers dumped them on the market all at once, causing a buying and selling frenzy like a clerk who’d gone insane.

Time spent in bug fixing Also mention 80% of the bugs caused by 20% pattern Metrix are important

JAVA PERFORMACE General problems

General problems Bad „architecture” decisions Using of existing APIs in a wrong way Bad using of existing API google-ing Use a solution in a wrong way

JAVA PERFORMACE SQL

N+1 problem Tools: Lazy associations Tuning fetch strategies

Caching No cacheing Configurare caching Distributed caches No cacheing Unless you have specific reasons not to cache, you should be caching Configurare caching Cache with fixed size -> optimize it Distributes caches The performance challenge is to balance your distributed caching behavior with your business rules: try to opt for the loosest distribution strategy that satisfies your business requirements (example: Facebook posts) This is the 2nd level cache in Hibernate

Connection pooling Reasons Relatively expensive to create/close Share connections across business transactions Limit to load (load testing) Size of pool: too much load to the database vs performance of the app

JAVA PERFORMACE Memory

Stop-the-world GC Managed memory management in Java Major GC stops-the-world Apply a GC strategy sun.misc.UnSafe / Off-heap APIs Managed memory: JVM in this case, not the programmer. 2-3 gigabyte heap this might be 3-5 seconds, 30-gigabyte head this could be more on the order of 30 seconds Solutions Concurrent Mark Sweep (CMS) garbage collection strategy G1 garbage collection strategy (Java 7 Update 9 ) Zing JVM Use an off-heap API (eg. OpenHFT)

Memory leaking Reference to an object that it does not ever intend to use again Tipically Collection framework classes with unbounded growth Running out of memory vs memory leak JVM to eventually exhaust its memory and throw the dreaded OutOfMemoryError, which usually means you have to restart your JVM -XX:-HeapDumpOnOutOfMemoryError

JAVA PERFORMACE Problems with the code

StringBuilder String result = (new StringBuilder("<")) .append(epam).append(">").toString(); System.out.println(result); String result = "<" + epam + ">"; System.out.println(result); String result = (new StringBuilder("<")) .append(epam).append(">").toString(); result = (new StringBuilder(String.valueOf(result))) .append("</").append(epam) .append(">").toString(); System.out.println(result); String result = "<" + epam + ">"; result+= "</" + epam + ">"; System.out.println(result); StringBuilder result = new StringBuilder("<"); result.append(epam).append(">").append("</").append(epam).append(">"); System.out.println(result); On the right the compiled code + mention JIT (run-time compilation to reach better performance)

Avoid iterator A common source of garbage are Iterators Iterator List values = new ArrayList(); String value; // return new Itr(); for(Iterator iterator = values.iterator(); iterator.hasNext(); System.out.println(value)) value = (String)iterator.next(); } List<String> values = new ArrayList<String>(); for (String value : values) { System.out.println(value); } Avoid iterator A common source of garbage are Iterators

Using the stack // on the heap Integer value = 123456; // on the stack int value = 123456; // 3 objects on the heap Integer[] i = { 123456, 654321 }; // 1 object on the heap Integer[] i = { 123456, 654321 }; In Java that collections framewrok cannot be used on primitive types. Use arrays or use a special library for primitive types (like trove4j)

.valueOf() Integer i1 = Integer.valueOf(NUM); System.out.println(i1 == i2); Integer i1 = NUM; Integer i2 = NUM; System.out.println(i1 == i2); Boolean a1 = true; // ... syntax sugar for: Boolean a2 = Boolean.valueOf(true);   Byte b1 = (byte) 123; // ... syntax sugar for: Byte b2 = Byte.valueOf((byte) 123);

Recursion Recursive methods solve difficult problems in a really simple way Each recursive call adds a new frame to the stack Some compilers optimizing tail-recursing algorithms back into iterative ones, but javac NOT int sum(int actual, int total) { if (actual == 0) { return total; } else { return sum(actual - 1, total + actual); int sum(int actual) { int total = 0; for (int i = actual; i > 0; i--) { total+=i; } return total;

hashCode() and equals() JDK hash based collections store their values in an array. Hash code is used to calculate an initial lookup position in this array If all values will have the same hash code, hash map (or set) will degrade into a list  Topics not touched: Concurrency Algorithms Network Scalability

Metrics vs checking code Before you start the check the code manually, check metrics SQL executions Time spent in functions 3rd party calls Memory usage GC runs Heap dump etc. Tools: JMH Jvisual VM Jprofiler Heap analyzer

Test automation Goal: functional and architecture confidence Automate and measure you application Use the metrics as quality gates

Thanks for your kind attention Peter Veres Senior Software Engineer Peter_Veres2@epam.com