{"id":11704,"date":"2020-06-12T09:30:57","date_gmt":"2020-06-12T13:30:57","guid":{"rendered":"https:\/\/www.eginnovations.com\/blog\/?p=11704"},"modified":"2024-01-01T12:42:01","modified_gmt":"2024-01-01T17:42:01","slug":"6-tips-fast-java-applications","status":"publish","type":"post","link":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/","title":{"rendered":"6 Tips to Make Java Applications Faster"},"content":{"rendered":"<h2><span class=\"ez-toc-section\" id=\"Making_Java_Applications_Run_Faster\"><\/span>Making Java Applications Run Faster<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Application developers and application operations personnel are together responsible for ensuring that Java applications perform well. In an earlier blog, we had discussed <a href=\"https:\/\/www.eginnovations.com\/blog\/how-to-enhance-performance-java-applications\/\">7 configurations<\/a> that Application Operations teams can use to make their Java applications high-performing. In this blog, we will focus on Application Developers and discuss 6 ways in which they can enhance the <a href=\"https:\/\/www.eginnovations.com\/supported-technologies\/java-application-monitoring\">performance of their Java applications<\/a> and make Java run faster.<\/p>\n<div class=\"tips-box\" style=\"display: inline-block; margin-top: 0!important; margin-bottom: 20px; padding: 15px 20px 0px;\">\n<p><a href=\"https:\/\/www.eginnovations.com\/product\/application-performance-monitoring\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-7575 size-full\" style=\"margin-top: 25px; margin-right: 0; width: 80px!important;\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2018\/11\/monitoring-tools-list.png\" alt=\"Java application monitoring dashboard\" width=\"78\" height=\"68\" border=\"0\" \/><\/a><\/p>\n<p style=\"margin-top: -15px;\"><a href=\"https:\/\/www.eginnovations.com\/product\/application-performance-monitoring\">eG Enterprise<\/a> is a Java-based web <a href=\"https:\/\/www.eginnovations.com\/blog\/what-is-application-performance-monitoring\/\/\">application performance monitoring<\/a>, diagnosis, and reporting solution. Most of the tips in this article are based on techniques that our development team uses to make our solution highly scalable and performing well.<\/p>\n<\/div>\n<p style=\"margin-bottom: 15px;\">Several articles over the years have documented best practices you can follow to make your Java applications\u2019 performance better. For instance, see <a href=\"https:\/\/blog.jooq.org\/2015\/02\/05\/top-10-easy-performance-optimisations-in-java\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https:\/\/blog.jooq.org\/2015\/02\/05\/top-10-easy-performance-optimisations-in-java\/<\/a>. Common recommendations include:<\/p>\n<ul>\n<li>String concatenation using StringBuilder<\/li>\n<li>Avoiding regular expressions<\/li>\n<li>Avoiding iterators<\/li>\n<li>Avoiding recursion where possible<\/li>\n<li>Using primitive types where possible<\/li>\n<\/ul>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"10\">\n<tbody>\n<tr>\n<th class=\"problems-list-title\" valign=\"top\">\n<h2 style=\"font-size: 18px; margin: 0; padding: 0; font-weight: bold;\"><span class=\"ez-toc-section\" id=\"6_Tips_for_Application_Developers_to_Make_Java_Applications_Faster\"><\/span>6 Tips for Application Developers to Make Java Applications Faster<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<\/th>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<ol class=\"ol_style_line\" style=\"margin: 15px 0;\">\n<li><a href=\"#question1\" rel=\"question1\">Select the Java collection to use in your application carefully<\/a><\/li>\n<li><a href=\"#question2\" rel=\"question2\">Minimize the number of method calls you need to make to access the collections<\/a><\/li>\n<li><a href=\"#question3\" rel=\"question3\">Use \u201ccontains\u201d with caution<\/a><\/li>\n<li><a href=\"#question4\" rel=\"question4\">Use Maps if you are handling large data sets and need to index\/search them<\/a><\/li>\n<li><a href=\"#question5\" rel=\"question5\">Remember Java is full of References<\/a><\/li>\n<li><a href=\"#question6\" rel=\"question6\">Be careful when you use synchronization<\/a><\/li>\n<\/ol>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table id=\"question1\" class=\"new_table_style\" style=\"width: 100%;\">\n<tbody>\n<tr>\n<td>1.<\/td>\n<td>\n<h3><span class=\"ez-toc-section\" id=\"Select_the_Java_collection_to_use_in_your_application_carefully\"><\/span>Select the Java collection to use in your application carefully<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Many large-scale Java applications will have to deal with a large amount of data. The\u00a0Java Collections\u00a0Framework is a collection of interfaces and classes, which helps in storing and processing the data efficiently. Let\u2019s explore how the right use of Java collections and related methods can significantly boost the performance of your Java application.<\/p>\n<p><a href=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/collection-framework-hierarchy.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11706 size-large\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/collection-framework-hierarchy.png\" alt=\"Collections that can be used in Java applications\" width=\"640\" height=\"350\" border=\"0\" \/><\/a><\/p>\n<div class=\"img_caption\">Java Collection Framework Hierarchy<\/div>\n<p>Bear in mind that ArrayList, HashMap, etc. are not synchronized whereas collections such as Vectors and Hashtables are synchronized. Multi-threaded applications may need thread-safe access and hence, synchronized collections need to be used in such cases. Using Vectors or Hashtables within a thread when synchronized access is not required should be avoided. You can get multi-fold performance gains by avoiding unnecessary use of synchronized collections.<\/p>\n<p>Inadvertent use of synchronized objects will result in frequent thread-blocking and could result in your application appearing to be very slow to users. If you must use synchronized objects, use collections such as ConcurrentHashMap that allow multiple threads to be accessing different parts of the same object at the same time.<\/p>\n<p><a href=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11709 size-large\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map-1024x448.png\" alt=\"Java Collection options\" width=\"640\" height=\"280\" border=\"0\" srcset=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map-1024x448.png 1024w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map-300x131.png 300w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map-800x350.png 800w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map-310x136.png 310w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map-140x61.png 140w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-collection-hash-map.png 1050w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<div class=\"img_caption\">Differences between different Java collection types<\/div>\n<table id=\"question2\" class=\"new_table_style\" style=\"width: 100%;\">\n<tbody>\n<tr>\n<td>2.<\/td>\n<td>\n<h3><span class=\"ez-toc-section\" id=\"Minimize_the_number_of_method_calls_you_need_to_make_to_access_collections_in_your_Java_applications\"><\/span>Minimize the number of method calls you need to make to access collections in your Java applications<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Collection objects offer several access methods \u2013 to check the keys, to check the values, size of the collection, to put objects, etc. Many a times, developers have a tendency to check if a Hashtable or a HashMap has a key and then get the value of that key for processing. See the code snippet below:<\/p>\n<pre>Hashtable test = new Hashtable();\r\n\u2026\r\nfor (int i=0; i &lt; vars.size(); i++)\r\n{\r\n\tif(h.containsKey((String) vars.get(i)))\r\n\t{\r\n\t\tString value = (String) h.get((String) vars.get(i));\r\n\t\t\u2026\r\n\t}\r\n}\r\n<\/pre>\n<p>In the above code snippet, the index of the Hashtable is first checked and then the value is accessed. This requires two method calls \u2013 both of which involve synchronized access to the Hashtable. A far more efficient way to do this is represented by the code snippet below:<\/p>\n<pre>Hashtable test = new Hashtable();\r\n\u2026\r\nfor (int i=0; i &lt; vars.size(); i++)\r\n{\r\n\tString value = (String) h.get((String) vars.get(i));\r\n\tif (value != null)\r\n\t{\r\n\t\u2026\r\n\t}\r\n}\r\n<\/pre>\n<p>In this case, there is one less access to the Hashtable for each iteration of the loop. If you are making 1000s of accesses to the Hashtable in a loop, this could save 1000s of method calls. This is a simple way to boost the speed of your Java applications without any change in the code logic.<\/p>\n<table id=\"question3\" class=\"new_table_style\" style=\"width: 100%;\">\n<tbody>\n<tr>\n<td>3.<\/td>\n<td>\n<h3><span class=\"ez-toc-section\" id=\"Use_contains_with_caution_in_your_Java_applications\"><\/span>Use \u201ccontains\u201d with caution in your Java applications<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-11708\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-applications-caution-300x160.jpg\" alt=\"Use 'contains' with caution!\" width=\"300\" height=\"160\" border=\"0\" srcset=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-applications-caution.jpg 300w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-applications-caution-140x75.jpg 140w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>Lists, ArrayLists, and Vectors have a contains method that allows programmers to check if a collection already has a similar object. You may be iterating through a large sample and, very often, you may need to find a list of unique objects in the sample. Your code might look like this:<\/p>\n<pre>ArrayList al = new ArrayList();\r\n\u2026\r\nfor (int i=0; i &lt; vars.size(); i++)\r\n{\r\n\tString obj = (String) vars.get(i);\r\n\tif (!al.contains(obj))\r\n\t{\r\n\t\tal.add(obj);\r\n\t}\r\n}\r\n<\/pre>\n<p>Functionally, this code is fine, but from a performance standpoint, you are checking whether the ArrayList contains the object on every iteration of the loop. The contains method scans the entire ArrayList each time. So, as the ArrayList gets bigger, the performance penalty increases.<\/p>\n<p>You will be better off adding all the samples to the ArrayList first, conducting a duplicate check once, using a collection such as a HashSet that inherently provides uniqueness, and creating the unique ArrayList once. Instead of having potentially 1000s of contains checks on the ArrayList, now you have a one-time duplicate check.<\/p>\n<pre>ArrayList al = new ArrayList();\r\n\u2026\r\nfor (int i=0; i &lt; vars.size(); i++)\r\n{\r\n\tString obj = (String) vars.get(i);\r\n\tal.add(obj);\r\n}\r\nal = removeDuplicates(al);\r\n\u2026\r\nstatic ArrayList removeDuplicates(ArrayList list) \r\n{\r\n\tif (list == null || list.size() == 0)\r\n\t{\r\n\t\treturn list;\r\n\t}\r\n      \tSet set = new HashSet(list);\r\n\tlist.clear();\r\n\tlist.addAll(set);\r\n \treturn list;\r\n}\r\n<\/pre>\n<p>The table below shows the time difference between our original code and the modified code above:<\/p>\n<table class=\"second_style\" style=\"width: 100%;\">\n<tbody>\n<tr class=\"second_style_row\">\n<td><strong>List Size<\/strong><\/td>\n<td>100<\/td>\n<td>1000<\/td>\n<td>10000<\/td>\n<td>100000<\/td>\n<\/tr>\n<tr class=\"second_style_rows\">\n<td><strong>Original Code<\/strong><\/td>\n<td>0ms<\/td>\n<td>5ms<\/td>\n<td>171ms<\/td>\n<td>49,820ms<\/td>\n<\/tr>\n<tr>\n<td><strong>Modified Code<\/strong><\/td>\n<td>0ms<\/td>\n<td>1ms<\/td>\n<td>7ms<\/td>\n<td>28ms<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see above, the savings from the performance tweaks we made increases as the size of the ArrayList being accessed increases. When your application is dealing with larger data sets, the importance of performance tuning is more.<\/p>\n<p><a href=\"https:\/\/www.eginnovations.com\/supported-technologies\/citrix-monitoring\/trial\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11729 size-full\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/free-trial-now.jpg?2020\" alt=\"Java application performance monitoring tool - free trial\" width=\"640\" height=\"150\" border=\"0\" \/><\/a><\/p>\n<table id=\"question4\" class=\"new_table_style\" style=\"width: 100%;\">\n<tbody>\n<tr>\n<td>4.<\/td>\n<td>\n<h3><span class=\"ez-toc-section\" id=\"Use_Maps_if_you_are_handling_large_data_sets_and_need_to_indexsearch_them_in_your_Java_applications\"><\/span>Use Maps if you are handling large data sets and need to index\/search them in your Java applications<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-11710\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-large-data-sets-300x180.jpg\" alt=\"Use maps if you are handling large data sets\" width=\"300\" height=\"180\" border=\"0\" srcset=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-large-data-sets.jpg 300w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-large-data-sets-140x84.jpg 140w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>Java developers have been taught to use Hashtables and HashMaps when dealing with key value pairs and to use a collection like a LinkedList, ArrayList, Vector, etc. when there are a list of items, respectively. When you have a large list and you need to search this list, you have to use the contains method of the List object. Consider the code snippet below:<\/p>\n<pre>ArrayList al = new ArrayList();\r\n\/\/insert records into the ArrayList\r\n\u2026\r\nfor (\u2026)\r\n{\r\n\t\u2026\r\n\tboolean b = al.contains(astring);\r\n}\r\n<\/pre>\n<p>Now, instead of using an ArrayList, let\u2019s use a HashMap instead:<\/p>\n<pre>HashMap myHash = new HashMap();\r\n\/\/insert records into the HashMap\r\n\u2026\r\nfor (\u2026)\r\n{\r\n\t\u2026\r\n\tboolean b = myHash.contains(astring);\r\n}\r\n<\/pre>\n<p>The table below summarizes the performance difference between the two approaches as the list size increases:<\/p>\n<table class=\"second_style\" style=\"width: 100%;\">\n<tbody>\n<tr class=\"second_style_row\">\n<td><strong>List Size<\/strong><\/td>\n<td>100<\/td>\n<td>1000<\/td>\n<td>10000<\/td>\n<td>100000<\/td>\n<\/tr>\n<tr class=\"second_style_rows\">\n<td><strong>When using ArrayList<\/strong><\/td>\n<td>0ms<\/td>\n<td>6ms<\/td>\n<td>186ms<\/td>\n<td>62,105ms<\/td>\n<\/tr>\n<tr>\n<td><strong>When using HashMap<\/strong><\/td>\n<td>0ms<\/td>\n<td>0ms<\/td>\n<td>1ms<\/td>\n<td>8ms<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see from the table above, the performance improvements from using HashMap rather than ArrayLists to hold large objects that need to be searched or indexed is very significant as the number of objects involved is higher. Similar performance improvements have also been reported in <a href=\"https:\/\/www.baeldung.com\/java-hashset-arraylist-contains-performance\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">earlier analysis<\/a> too.<\/p>\n<p>The figure below shows how the performance improvements translate into lower resource usage of the Java application as well. Here you see the CPU usage of the JVM before and after the optimization was applied. The JVM\u2019s CPU usage which was around 50% on an average dropped to 20% after this optimization was applied \u2013 that\u2019s a 60% reduction in CPU usage, a significant cost saving, if you are running the application in a cloud environment like AWS or Azure.<\/p>\n<p><a href=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11769 size-large\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces-1024x404.png\" alt=\"How CPU usage of a Java application falls when you optimize the Java code\" width=\"640\" height=\"253\" border=\"0\" srcset=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces-1024x404.png 1024w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces-300x118.png 300w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces-800x316.png 800w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces-310x122.png 310w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces-140x55.png 140w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/Java-application-reduces.png 1299w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<div class=\"img_caption\">How CPU usage of a Java application reduces when you optimize the code<\/div>\n<table id=\"question5\" class=\"new_table_style\" style=\"width: 100%;\">\n<tbody>\n<tr>\n<td>5.<\/td>\n<td>\n<h3><span class=\"ez-toc-section\" id=\"Remember_Java_is_full_of_References\"><\/span>Remember Java is full of References<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"tips-box\" style=\"display: inline-block; margin-top: 0!important; margin-bottom: 20px; padding: 15px 20px 5px;\">\n<div style=\"padding: 15px; text-align: center; font-size: 20px;\"><strong>Java doesn\u2019t have pointers; Java has references.<\/strong><\/div>\n<p><a href=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-references.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-11711 size-medium\" style=\"margin-top: 0px;\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-references-300x150.png\" alt=\"Java pointers\" width=\"300\" height=\"150\" border=\"0\" srcset=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-references-300x150.png 300w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-references-310x155.png 310w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-references-140x70.png 140w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-references.png 500w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><strong>Reference:<\/strong>\u00a0A reference is a variable that refers to something else and can be used as an alias for that something else.<br \/>\n<strong>Pointer:<\/strong>\u00a0A pointer is a variable that stores a memory address for the purpose of acting as an alias to what is stored at that address.<br \/>\nSo, a pointer is a reference, but a reference is not necessarily a pointer. Pointers are particular implementations of the concept of a reference, and the term tends to be used only for languages that give you direct access to the memory address.<br \/>\n<strong>Source<\/strong>: <a href=\"https:\/\/www.geeksforgeeks.org\/is-there-any-concept-of-pointers-in-java\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https:\/\/www.geeksforgeeks.org\/is-there-any-concept-of-pointers-in-java\/<\/a><\/p>\n<\/div>\n<p>This understanding of references is useful when dealing with collections. If you remember that Java objects are references, you will be able to optimize your code for better performance. Check this example below:<\/p>\n<pre>HashMap myHash = new HashMap ();\r\n\u2026\r\nif (myHash.containsKey(astring))\r\n{\r\n\tArrayList myList = (ArrayList) h.get(astring);\r\n\tmyList.add(avalue);\r\n\tmyHash.put(astring,myList);\r\n}\r\n<\/pre>\n<p>As Java uses references and the ArrayList <em>myList<\/em> is a reference, the <em>put<\/em> method call above is not necessary. If you were doing this in a loop that executes a 1000 times, that\u2019s a 1000 method calls done unnecessarily in the example above. And if <em>myHash<\/em> had been a Hashtable, the time saved is even more \u2013 one synchronized method call less in every iteration of the loop!<\/p>\n<p>The time saved here is not as dramatic as in the previous cases. When processing 100,000 objects, the performance gain is about 33%.<\/p>\n<table id=\"question6\" class=\"new_table_style\" style=\"width: 100%;\">\n<tbody>\n<tr>\n<td>6.<\/td>\n<td>\n<h3><span class=\"ez-toc-section\" id=\"Be_careful_when_you_use_synchronization_in_your_Java_applications\"><\/span>Be careful when you use synchronization in your Java applications<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-11767 size-medium\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-words-colud-300x226.png\" alt=\"java synchronization options\" width=\"300\" height=\"226\" border=\"0\" srcset=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-words-colud-300x226.png 300w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-words-colud-310x234.png 310w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-words-colud-140x105.png 140w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/java-words-colud.png 673w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>Synchronization in C and C++ was difficult to do &#8211; mutexes and semaphores had to be used carefully. Java makes it really easy to use synchronized blocks. Anyone can use synchronized blocks and methods without knowing the performance impact of such usage. Improper use of synchronization can result in thread-blocking or even thread deadlocks. <a href=\"https:\/\/www.eginnovations.com\/blog\/java-synchronized-blocks\/\">Read more on synchronization of Java threads in our earlier blog<\/a>.<\/p>\n<p>A common mistake made by Java programmers is to synchronize on static variables unknowingly. You might have defined a variable in one class that point to a string as below:<\/p>\n<pre>String string1 = \u201csyncstring\u201d;\r\n<\/pre>\n<p>You might have a completely different variable defined in another class:<\/p>\n<pre>String string2 = \u201csyncstring\u201d;\r\n<\/pre>\n<p>If you use synchronized blocks that synchronize on <em>string1<\/em> in the first class and that synchronize on <em>string2<\/em> in the second class, you might expect that these two synchronized blocks cannot interfere with each other. Unfortunately, <em>string1<\/em> and <em>string2<\/em> are pointers to the same string \u201c<em>syncstring<\/em>\u201d. So, when the two classes are executed, they have a possibility of interfering with each other. Therefore, to minimize interference between classes, create new instances in each class. In the above example, <em>string1<\/em> should have been set to <em>new String(\u201csyncstring\u201d)<\/em>.<\/p>\n<p>For best performance, it is important to keep the amount of processing done inside synchronized methods and blocks to an absolute minimum. If there are activities you can perform outside the synchronized blocks\/methods, do that first for improved performance.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Performance is not always top of the mind for application developers. But, if you are developing Java applications that will handle large volumes of data, performance considerations are especially important and will affect how users perceive your applications.<\/p>\n<p>As you have seen in the examples above, application developers can achieve an order of magnitude better performance for Java web applications, if one keeps the 6 best practices presented here in mind.<\/p>\n<p><a href=\"https:\/\/www.eginnovations.com\/supported-technologies\/citrix-monitoring\/trial\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11729 size-full\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/free-trial-now.jpg?2020\" alt=\"Java performance benchmark\" width=\"640\" height=\"150\" border=\"0\" \/><\/a><\/p>\n<div class=\"tips-box\" style=\"display: inline-block; margin-top: 0!important; margin-bottom: 20px; padding: 20px 20px 15px; font-size: 18px; line-height: 30px;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-11791\" style=\"width: 100px; margin-top: -10px;\" src=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/comments-arrow-300x296.png\" alt=\"comments-arrow\" width=\"300\" height=\"296\" border=\"0\" srcset=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/comments-arrow-300x296.png 300w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/comments-arrow-310x305.png 310w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/comments-arrow-140x138.png 140w, https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/comments-arrow.png 470w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>What do you think of these 6 best tips or best practices? Do you disagree with any point? Do you have any other tip or trick up your sleeve that you would like to share with us and fellow readers? Please do comment.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Making Java Applications Run Faster Application developers and application operations personnel are together responsible for ensuring that Java applications perform well. In an earlier blog, we had discussed 7 configurations that Application Operations teams can use to make their Java applications high-performing. In this blog, we will focus on Application Developers and discuss 6 ways [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":11712,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[27],"tags":[1216,171,435,500,602,1270,1273,1276,650,1272,176,177,1271,1254,1278,1279,1277,1274,1248,480,1275,687,178,179],"class_list":["post-11704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-monitoring","tag-garbage-collection-in-java","tag-java","tag-java-apm","tag-java-application","tag-java-code","tag-java-collections","tag-java-contains","tag-java-deadlock","tag-java-gc","tag-java-map","tag-java-monitoring","tag-java-performance","tag-java-performance-monitoring","tag-java-profiler","tag-java-programming","tag-java-programming-tips","tag-java-speed","tag-java-stack","tag-java-stack-trace","tag-java-synchronization","tag-java-synchronized","tag-java-tips","tag-java-tuning","tag-jvm-performance"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to make Java run faster - 6 Tips | eG Innovations<\/title>\n<meta name=\"description\" content=\"These 6 recommendations will help Java developers enhance the performance of their Java applications and make Java run faster. Learn more!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"6 Tips for Faster Java Applications | eG Innovations\" \/>\n<meta property=\"og:description\" content=\"These 6 Java application performance recommendations will help developers enhance performance of all Java applications. Learn More!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/\" \/>\n<meta property=\"og:site_name\" content=\"eG Innovations\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/eGInnovations\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-12T13:30:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-01T17:42:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/fb1.jpg\" \/>\n<meta name=\"author\" content=\"Keren Emil\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"6 Tips for Faster Java Applications | eG Innovations\" \/>\n<meta name=\"twitter:description\" content=\"These 6 Java application performance recommendations will help developers enhance performance of all Java applications. Learn More!\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/fb1.jpg\" \/>\n<meta name=\"twitter:creator\" content=\"@eginnovations\" \/>\n<meta name=\"twitter:site\" content=\"@eginnovations\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Keren Emil\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to make Java run faster - 6 Tips | eG Innovations","description":"These 6 recommendations will help Java developers enhance the performance of their Java applications and make Java run faster. Learn more!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/","og_locale":"en_US","og_type":"article","og_title":"6 Tips for Faster Java Applications | eG Innovations","og_description":"These 6 Java application performance recommendations will help developers enhance performance of all Java applications. Learn More!","og_url":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/","og_site_name":"eG Innovations","article_publisher":"https:\/\/www.facebook.com\/eGInnovations","article_published_time":"2020-06-12T13:30:57+00:00","article_modified_time":"2024-01-01T17:42:01+00:00","og_image":[{"url":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/fb1.jpg","type":"","width":"","height":""}],"author":"Keren Emil","twitter_card":"summary_large_image","twitter_title":"6 Tips for Faster Java Applications | eG Innovations","twitter_description":"These 6 Java application performance recommendations will help developers enhance performance of all Java applications. Learn More!","twitter_image":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/fb1.jpg","twitter_creator":"@eginnovations","twitter_site":"@eginnovations","twitter_misc":{"Written by":"Keren Emil","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#article","isPartOf":{"@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/"},"author":{"name":"Keren Emil","@id":"https:\/\/www.eginnovations.com\/blog\/#\/schema\/person\/6e6a6382a7491a6c5da334e912adb508"},"headline":"6 Tips to Make Java Applications Faster","datePublished":"2020-06-12T13:30:57+00:00","dateModified":"2024-01-01T17:42:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/"},"wordCount":1675,"commentCount":0,"publisher":{"@id":"https:\/\/www.eginnovations.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/thumbnail2.jpg","keywords":["garbage collection in java","Java","Java APM","Java application","Java code","Java collections","java contains","java deadlock","Java GC","java map","Java Monitoring","Java performance","Java performance monitoring","java profiler","java programming","java programming tips","java speed","java stack","java stack trace","Java synchronization","java synchronized","Java tips","Java Tuning","JVM Performance"],"articleSection":["Java Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/","url":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/","name":"How to make Java run faster - 6 Tips | eG Innovations","isPartOf":{"@id":"https:\/\/www.eginnovations.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#primaryimage"},"image":{"@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/thumbnail2.jpg","datePublished":"2020-06-12T13:30:57+00:00","dateModified":"2024-01-01T17:42:01+00:00","description":"These 6 recommendations will help Java developers enhance the performance of their Java applications and make Java run faster. Learn more!","breadcrumb":{"@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#primaryimage","url":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/thumbnail2.jpg","contentUrl":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2020\/06\/thumbnail2.jpg","width":300,"height":200},{"@type":"BreadcrumbList","@id":"https:\/\/www.eginnovations.com\/blog\/6-tips-fast-java-applications\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.eginnovations.com\/blog\/"},{"@type":"ListItem","position":2,"name":"6 Tips to Make Java Applications Faster"}]},{"@type":"WebSite","@id":"https:\/\/www.eginnovations.com\/blog\/#website","url":"https:\/\/www.eginnovations.com\/blog\/","name":"eG Innovations","description":"IT Performance Monitoring Insights","publisher":{"@id":"https:\/\/www.eginnovations.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.eginnovations.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.eginnovations.com\/blog\/#organization","name":"eG Innovations","alternateName":"eg innovations","url":"https:\/\/www.eginnovations.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.eginnovations.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2014\/07\/eg-logo-dark-gray1_new.jpg","contentUrl":"https:\/\/www.eginnovations.com\/blog\/wp-content\/uploads\/2014\/07\/eg-logo-dark-gray1_new.jpg","width":362,"height":235,"caption":"eG Innovations"},"image":{"@id":"https:\/\/www.eginnovations.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/eGInnovations","https:\/\/x.com\/eginnovations"]},{"@type":"Person","@id":"https:\/\/www.eginnovations.com\/blog\/#\/schema\/person\/6e6a6382a7491a6c5da334e912adb508","name":"Keren Emil","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.eginnovations.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1d5e57d310e83b565706d96dd2cac1cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1d5e57d310e83b565706d96dd2cac1cd?s=96&d=mm&r=g","caption":"Keren Emil"},"sameAs":["eguser"],"url":"https:\/\/www.eginnovations.com\/blog\/author\/keren\/"}]}},"modified_by":"Victorious Seo","_links":{"self":[{"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/posts\/11704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/comments?post=11704"}],"version-history":[{"count":0,"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/posts\/11704\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/media\/11712"}],"wp:attachment":[{"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/media?parent=11704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/categories?post=11704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.eginnovations.com\/blog\/wp-json\/wp\/v2\/tags?post=11704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}