{"id":12826,"date":"2017-08-14T10:37:48","date_gmt":"2017-08-14T10:37:48","guid":{"rendered":"https:\/\/stackify.com\/?p=12826"},"modified":"2024-03-04T07:07:03","modified_gmt":"2024-03-04T07:07:03","slug":"memory-leaks-java","status":"publish","type":"post","link":"https:\/\/stackify.com\/memory-leaks-java\/","title":{"rendered":"How Memory Leaks Happen in a Java Application"},"content":{"rendered":"<h2 id=\"introduction\"><strong>Introduction to Memory Leaks In Java Apps<\/strong><\/h2>\n<p>One of the core benefits of Java is the JVM, which is an out-of-the-box <a href=\"\/java-heap-vs-stack\/\">memory management<\/a>. Essentially, we can\u00a0create objects and the <a href=\"https:\/\/stackify.com\/what-is-java-garbage-collection\/\">Java Garbage Collector<\/a> will take care of allocating and freeing up memory for us.<\/p>\n<p><strong>Nevertheless, memory leaks can still occur in Java applications.<\/strong><\/p>\n<p>In this article, we&#8217;re going to describe the most common memory leaks, understand their causes, and look at a few techniques to detect\/avoid them. We&#8217;re also going to use the Java YourKit profiler throughout the article, to analyze the state of our memory at runtime.<\/p>\n<h3 id=\"introduction\"><strong>1. What is a Memory Leak in Java?<\/strong><\/h3>\n<p>The standard definition of a memory leak is a scenario that occurs when <strong>objects are no longer being used by the application, but the Garbage Collector is unable to remove them from working memory<\/strong>\u00a0&#8211; because they&#8217;re still being referenced. As a result, the application consumes more and more resources &#8211; which eventually leads to a fatal <em>OutOfMemoryError<\/em>.<\/p>\n<p>For a better understanding of the concept, here&#8217;s a simple visual representation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12828 aligncenter\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/how-memory-leaks-happen-java.jpeg\" alt=\"How memory leaks happen in Java\" width=\"650\" height=\"400\" \/><\/p>\n<p>As we can see, we have two types of objects &#8211; referenced and unreferenced; the Garbage Collector can remove objects that are unreferenced. Referenced objects won&#8217;t be collected, even if they&#8217;re actually not longer used by the application.<\/p>\n<p>Detecting memory leaks can be difficult. A number of tools perform static analysis to determine potential leaks, but these techniques aren&#8217;t perfect because the most important aspect is the actual runtime behavior of the running system.<\/p>\n<p>So, let&#8217;s have a focused look at some of the standard practices of <a href=\"\/java-memory-leaks-solutions\/\">preventing memory leaks<\/a>, <strong>by analyzing some common scenarios<\/strong>.<\/p>\n<h3 id=\"introduction\"><strong>2. Java Heap Leaks<\/strong><\/h3>\n<p>In this initial section, we&#8217;re going to focus on the classic memory leak scenario &#8211; where Java objects are continuously created without being released.<\/p>\n<p>An advantageous technique to understand these situations is to make reproducing a memory leak easier by <strong>setting a lower size for the Heap<\/strong>. That&#8217;s why, when starting our application, we can adjust the JVM to suit our memory needs:<\/p>\n<pre class=\"prettyprint\">-Xms&lt;size&gt;<\/pre>\n<pre class=\"prettyprint\">-Xmx&lt;size&gt;<\/pre>\n<p>These parameters specify the initial Java Heap size as well as the maximum Heap size.<\/p>\n<h4 id=\"introduction\"><strong>2.1. Static Field Holding On to the Object Reference<\/strong><\/h4>\n<p>The first scenario that might cause a Java memory leak is referencing a heavy object with a static field.<\/p>\n<p>Let&#8217;s have a look at a quick example:<\/p>\n<pre class=\"prettyprint\">private Random random = new Random();\npublic static final ArrayList&lt;Double&gt; list = new ArrayList&lt;Double&gt;(1000000);\n\n@Test\npublic void givenStaticField_whenLotsOfOperations_thenMemoryLeak() throws InterruptedException {\n    for (int i = 0; i &lt; 1000000; i++) {\n        list.add(random.nextDouble());\n    }\n    \n    System.gc();\n    Thread.sleep(10000); \/\/ to allow GC do its job\n}<\/pre>\n<p>We created our <em>ArrayList<\/em>\u00a0as a static field &#8211; which will never be collected by the JVM Garbage Collector during the lifetime of the JVM process, even after the calculations it was used for are done. We also invoked\u00a0<em>Thread.sleep(10000)<\/em> to allow the GC to perform a full collection and try to reclaim everything that can be reclaimed.<\/p>\n<p>Let&#8217;s run the test and analyze the JVM with our profiler:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-12829\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/2-static-memory-leak-1024x609.png\" alt=\"Java static memory leak\" width=\"1024\" height=\"609\" \/><\/p>\n<p>Notice how, at the very beginning, all memory is, of course, free.<\/p>\n<p>Then, in just 2 seconds, the iteration process runs and finishes &#8211; loading everything into the list (naturally this will depend on the machine you&#8217;re running the test on).<\/p>\n<p>After that, a full garbage collection cycle is triggered, and the test continues to execute, to allow this cycle time to run and finish. As you can see, the list is not reclaimed and the memory consumption doesn&#8217;t go down.<\/p>\n<p>Let&#8217;s now see the exact same example, only this time, the <em>ArrayList<\/em> isn&#8217;t referenced by a static variable. Instead, it&#8217;s a local variable that gets created, used and then discarded:<\/p>\n<pre class=\"prettyprint\">@Test\npublic void givenNormalField_whenLotsOfOperations_thenGCWorksFine() throws InterruptedException {\n    addElementsToTheList();\n    System.gc();\n    Thread.sleep(10000); \/\/ to allow GC do its job\n}\n    \nprivate void addElementsToTheList(){\n    ArrayList&lt;Double&gt; list = new ArrayList&lt;Double&gt;(1000000);\n    for (int i = 0; i &lt; 1000000; i++) {\n        list.add(random.nextDouble());\n    }\n}<\/pre>\n<p>Once the method finishes its job, we&#8217;ll observe the major GC collection, around 50th second on the image below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-12830\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/3-static-no-memory-leak-1024x301.png\" alt=\"Java static no memory leak\" width=\"1024\" height=\"301\" \/><\/p>\n<p>Notice how the GC is now able to reclaim some of the memory utilized by the JVM.<\/p>\n<h4><strong>How to prevent it?<\/strong><\/h4>\n<p>Now that you understand the scenario, there are of course ways to prevent it from occurring.<\/p>\n<p>First, we need to <strong>pay close attention to our usage of <em>static<\/em><\/strong>; declaring any collection or heavy object as <em>static <\/em>ties its lifecycle to the lifecycle of the JVM itself, and makes the entire object graph impossible to collect.<\/p>\n<p>We also need to be aware of collections in general &#8211; that&#8217;s a common way to unintentionally hold on to references for longer than we need to.<\/p>\n<h4 id=\"introduction\"><strong>2.2. Calling <em>String.intern()<\/em> on Long <em>String<\/em><\/strong><\/h4>\n<p>The second group of scenarios that frequently causes memory leaks involves\u00a0<em>String<\/em> operations &#8211; specifically the\u00a0<em>String.intern()<\/em> API<em>.\u00a0<\/em><\/p>\n<p>Let&#8217;s have a look at a quick example:<\/p>\n<pre class=\"prettyprint\">@Test\npublic void givenLengthString_whenIntern_thenOutOfMemory()\n  throws IOException, InterruptedException {\n    Thread.sleep(15000);\n    \n    String str \n      = new Scanner(new File(\"src\/test\/resources\/large.txt\"), \"UTF-8\")\n      .useDelimiter(\"\\\\A\").next();\n    str.intern();\n    \n   \u00a0System.gc(); \n    Thread.sleep(15000);\n}<\/pre>\n<p>Here, we simply try to load a large text file into running memory and then return a canonical form, using .<em>intern()<\/em>.<\/p>\n<p>The <em>intern<\/em> API\u00a0will place the <em>str<\/em>\u00a0String in the JVM memory pool &#8211; where it can&#8217;t be collected &#8211; and again, this will cause the GC to be unable to free up enough memory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12831\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/4-String-intern-memory-leak.png\" alt=\"Java String intern memory leak\" width=\"863\" height=\"352\" \/><\/p>\n<p>We can clearly see that in the first 15th seconds JVM is stable, then we load the file and JVM perform\u00a0garbage collection (20th second).<\/p>\n<p>Finally, the <em>str.intern()<\/em> is invoked, which leads to the memory leak &#8211; the stable line indicating high heap memory usage, which will never be released.<\/p>\n<h4><strong>How to prevent it?<\/strong><\/h4>\n<p>Please remember that interned <em>String\u00a0<\/em>objects are stored in <em>PermGen<\/em> space &#8211; if our application is intended to perform a lot of operations on <a href=\"\/dos-and-donts-of-java-strings\/\">large strings<\/a>, we might need to increase the size of the permanent generation:<\/p>\n<pre class=\"prettyprint\">-XX:MaxPermSize=&lt;size&gt;<\/pre>\n<p>The second solution is to use Java 8 &#8211; where the <em>PermGen<\/em> space is replaced by the\u00a0<em>Metaspace <\/em>&#8211; which won&#8217;t lead to any\u00a0<em>OutOfMemoryError\u00a0<\/em>when using\u00a0<em>intern<\/em> on Strings:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-12832\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/5-String-intern-no-memory-leak-1024x322.png\" alt=\"\" width=\"1024\" height=\"322\" \/><\/p>\n<p>Finally, there are also several options of avoiding the <em>.intern()<\/em> API on Strings as well.<\/p>\n<h4 id=\"introduction\"><strong>2.3. Unclosed Streams<\/strong><\/h4>\n<p>Forgetting to close a stream is a very common scenario, and certainly, one that most developers can relate to. The problem was partially removed in Java 7 when the ability to automatically close all types of streams was introduced into the <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/exceptions\/tryResourceClose.html\" target=\"_blank\" rel=\"noopener noreferrer\"><em>try-with-resource<\/em> clause<\/a>.<\/p>\n<p>Why partially? Because <strong>the\u00a0<em>try-with-resources<\/em>\u00a0syntax is optional<\/strong>:<\/p>\n<pre class=\"prettyprint\">@Test(expected = OutOfMemoryError.class)\npublic void givenURL_whenUnclosedStream_thenOutOfMemory()\n  throws IOException, URISyntaxException {\n    String str = \"\";\n    URLConnection conn \n      = new URL(\"http:\/\/norvig.com\/big.txt\").openConnection();\n    BufferedReader br = new BufferedReader(\n      new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));\n    \n    while (br.readLine() != null) {\n        str += br.readLine();\n    } \n    \n    \/\/\n}<\/pre>\n<p>Let&#8217;s see how the memory of the application looks when loading a large file from an URL:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-12833\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/6-unclosed-streams-memory-leak-1024x354.png\" alt=\"Java unclosed streams memory leak\" width=\"1024\" height=\"354\" \/><\/p>\n<p>As we can see, the heap usage is gradually increasing over time &#8211; which is the direct impact of the memory leak caused by not closing the stream.<\/p>\n<p>Let&#8217;s dig a bit deeper into this scenario because it&#8217;s not as clear-cut as the rest. Technically, an unclosed stream will result in two types of leaks &#8211; a low-level resource leak and memory leak.<\/p>\n<p>The low-level resource leak is simply the leak of an OS-level resource &#8211; such as file descriptors, open connections, etc. These resources can also leak, just like memory does.<\/p>\n<p>Of course, the JVM uses memory to keep track of these underlying resources as well, which is why <strong>this also results in a memory leak<\/strong>.<\/p>\n<h4><strong>How to prevent it?<\/strong><\/h4>\n<p>We always need to remember to close streams manually, or to make a use of the auto-close feature introduced in Java 8:<\/p>\n<pre class=\"prettyprint\">try (BufferedReader br = new BufferedReader(\n  new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {\n    \/\/ further implementation\n} catch (IOException e) {\n    e.printStackTrace();\n}<\/pre>\n<p>In this case, the <em>BufferedReader<\/em> will be automatically closed at the end of the <em>try<\/em> statement, without the need to close it in an explicit <em>finally<\/em> block.<\/p>\n<h4 id=\"introduction\"><strong>2.4. Unclosed Connections<\/strong><\/h4>\n<p>This scenario is quite similar to the previous one, with the primary difference of dealing with unclosed connections (e.g. to a database, to an FTP server, etc.). Again, improper implementation can do a lot of harm, leading to memory problems.<\/p>\n<p>Let&#8217;s see a quick example:<\/p>\n<pre class=\"prettyprint\">@Test(expected = OutOfMemoryError.class)\npublic void givenConnection_whenUnclosed_thenOutOfMemory()\n  throws IOException, URISyntaxException {\n    \n    URL url = new URL(\"ftp:\/\/speedtest.tele2.net\");\n    URLConnection urlc = url.openConnection();\n    InputStream is = urlc.getInputStream();\n    String str = \"\";\n    \n    \/\/\n}<\/pre>\n<p>The <em>URLConnection<\/em> remains open, and the result is, predictably, a memory leak:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-12834\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/7-unclosed-connections-memory-leak-1024x420.png\" alt=\"Java unclosed connections memory leak\" width=\"1024\" height=\"420\" \/><\/p>\n<p>Notice how the Garbage Collector cannot do anything to release unused, but referenced memory. The situation is immediately clear after the 1st minute &#8211; the number of GC operations rapidly decreases, causing increased Heap memory use, which leads to the <em>OutOfMemoryError.<\/em><\/p>\n<h4><strong>How to prevent it?<\/strong><\/h4>\n<p>The answer here is simple &#8211; we need to always close connections in a disciplined manner.<\/p>\n<h4 id=\"introduction\"><strong>2.5. Adding Objects with no <em>hashCode()<\/em> and <em>equals()<\/em> into a <em>HashSet<\/em><\/strong><\/h4>\n<p>A simple but very common example that can lead to a memory leak is to use a <em>HashSet<\/em>\u00a0with objects that are missing their\u00a0<em>hashCode()<\/em> or <em>equals()<\/em>\u00a0implementations.<\/p>\n<p>Specifically, when we start adding duplicate\u00a0objects into a <em>Set<\/em>\u00a0&#8211; this will only ever grow, instead of ignoring duplicates as it should. We also won&#8217;t be able to remove these objects, once added.<\/p>\n<p>Let&#8217;s create a simple class without either <em>equals<\/em> or <em>hashCode<\/em>:<\/p>\n<pre class=\"prettyprint\">public class Key {\n    public String key;\n    \n    public Key(String key) {\n        Key.key = key;\n    }\n}<\/pre>\n<p>Now, let&#8217;s see the scenario:<\/p>\n<pre class=\"prettyprint\">@Test(expected = OutOfMemoryError.class)\npublic void givenMap_whenNoEqualsNoHashCodeMethods_thenOutOfMemory()\n  throws IOException, URISyntaxException {\n    Map&lt;Object, Object&gt; map = System.getProperties();\n    while (true) {\n        map.put(new Key(\"key\"), \"value\");\n    }\n}<\/pre>\n<p>This simple implementation will lead to the following scenario at runtime:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-12835\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/07\/8-no-hascode-equals-memory-leak-1024x458.png\" alt=\"Java no hascode equals memory leak\" width=\"1024\" height=\"458\" \/><\/p>\n<p>Notice how the garbage collector stopped being able to reclaim memory around 1:40, and notice the memory leak; the number of GC collections dropped almost four times immediately after.<\/p>\n<h4><strong>How to prevent it?<\/strong><\/h4>\n<p>In these situations, the solution is simple &#8211; it&#8217;s crucial to provide the <em>hashCode()<\/em> and <em>equals()<\/em> implementations.<\/p>\n<p>One tool worth mentioning here is <a href=\"https:\/\/projectlombok.org\" target=\"_blank\" rel=\"noopener noreferrer\">Project Lombok<\/a>\u00a0&#8211; this provides a lot of default implementation by annotations, e.g.\u00a0<a href=\"https:\/\/projectlombok.org\/features\/EqualsAndHashCode\" target=\"_blank\" rel=\"noopener noreferrer\"><em>@EqualsAndHashCode<\/em><\/a>.<\/p>\n<h3 id=\"introduction\"><strong>3. How to Find Leaking Sources in Your Application<\/strong><\/h3>\n<p>Diagnosing memory leaks is a lengthy process that requires a lot of practical experience, debugging skills and detailed knowledge of the application.<\/p>\n<p>Let&#8217;s see which techniques can help you in addition to standard profiling.<\/p>\n<h4 id=\"introduction\"><strong>3.1. Verbose Garbage Collection<\/strong><\/h4>\n<p>One of the quickest ways to identify a memory leak is to enable verbose garbage collection.<\/p>\n<p>By adding the\u00a0<em>-verbose:gc<\/em> parameter to the JVM configuration of our application, we&#8217;re enabling a very detailed trace of GC. Summary reports are shown in default error output file, which should help you understand how your memory is being managed.<\/p>\n<h4 id=\"introduction\"><strong>3.2. Do Profiling<\/strong><\/h4>\n<p>The second technique is the one we&#8217;ve been using throughout this article &#8211; and that&#8217;s profiling. The most popular profiler is\u00a0<a href=\"https:\/\/visualvm.github.io\" target=\"_blank\" rel=\"noopener noreferrer\">Visual VM<\/a>\u00a0&#8211; which is a good place to start moving past command-line JDK tools and into lightweight profiling.<\/p>\n<p>In this article, we used another profiler &#8211; YourKit &#8211; which has some additional, more advanced features compared to Visual VM.<\/p>\n<h4 id=\"introduction\"><strong>3.3. Review Your Code<\/strong><\/h4>\n<p>Finally, this is more of a general good practice than a specific technique to deal with memory leaks.<\/p>\n<p>Simply put &#8211; review your code thoroughly, practice regular code reviews and make good use of static analysis tools to help you understand your code and your system.<\/p>\n<h3 id=\"introduction\"><strong>Conclusion<\/strong><\/h3>\n<p>In this tutorial, we had a practical look at how memory leaks happen on the JVM. Understanding how these scenarios happen is the first step in the process of dealing with them.<\/p>\n<p>Then, having the techniques and tools to really see what&#8217;s happening at runtime, as the leak occurs, is critical as well. Static analysis and careful code-focused reviews can only do so much, and &#8211; at the end of the day &#8211; it&#8217;s the runtime that will show you the more complex leaks that aren&#8217;t immediately identifiable in the code.<\/p>\n<p>Finally, <strong>leaks can be notoriously hard to find and reproduce\u00a0because many of them only happen under intense load, which generally happens in production.\u00a0<\/strong>This is where you need to go beyond code-level analysis and work on two main aspects &#8211; reproduction and early detection.<\/p>\n<p>The best and most reliable way to <strong>reproduce memory leaks<\/strong> is to simulate the usage patterns of a production environment as close as possible, with the help of a <a href=\"https:\/\/stackify.com\/ultimate-guide-performance-testing-and-software-testing\/\">good suite of performance tests<\/a>.<\/p>\n<p>And <strong>early detection<\/strong> is where a\u00a0solid <a href=\"https:\/\/stackify.com\/retrace\/\">performance management solution<\/a>\u00a0and even <a href=\"https:\/\/stackify.com\/prefix\/\">an early detection solution<\/a> can make a significant difference, as it&#8217;s the only way to have the necessary insight into the runtime of your application in production.<\/p>\n<p>The\u00a0full implementation\u00a0of this tutorial can be found over on GitHub. This is a Maven based project, so it can simply be imported and run as it is.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Memory Leaks In Java Apps One of the core benefits of Java is the JVM, which is an out-of-the-box memory management. Essentially, we can\u00a0create objects and the Java Garbage Collector will take care of allocating and freeing up memory for us. Nevertheless, memory leaks can still occur in Java applications. In this article, [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":38269,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7],"tags":[40],"class_list":["post-12826","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v25.6) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understand and Prevent Memory Leaks in a Java Application<\/title>\n<meta name=\"description\" content=\"Memory leaks are a very real problem in Java and the JVM can only help so much. Learn how to understand leaks, recognize them at runtime and deal with them.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/stackify.com\/memory-leaks-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understand and Prevent Memory Leaks in a Java Application\" \/>\n<meta property=\"og:description\" content=\"Memory leaks are a very real problem in Java and the JVM can only help so much. Learn how to understand leaks, recognize them at runtime and deal with them.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/stackify.com\/memory-leaks-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Stackify\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Stackify\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-08-14T10:37:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-04T07:07:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"881\" \/>\n\t<meta property=\"og:image:height\" content=\"441\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Eugen Paraschiv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@stackify\" \/>\n<meta name=\"twitter:site\" content=\"@stackify\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Eugen Paraschiv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/\"},\"author\":{\"name\":\"Eugen Paraschiv\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/e372b6a6f64edbedafad33027d518482\"},\"headline\":\"How Memory Leaks Happen in a Java Application\",\"datePublished\":\"2017-08-14T10:37:48+00:00\",\"dateModified\":\"2024-03-04T07:07:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/\"},\"wordCount\":1902,\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg\",\"keywords\":[\"Java\"],\"articleSection\":[\"Developer Tips, Tricks &amp; Resources\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/\",\"url\":\"https:\/\/stackify.com\/memory-leaks-java\/\",\"name\":\"Understand and Prevent Memory Leaks in a Java Application\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg\",\"datePublished\":\"2017-08-14T10:37:48+00:00\",\"dateModified\":\"2024-03-04T07:07:03+00:00\",\"description\":\"Memory leaks are a very real problem in Java and the JVM can only help so much. Learn how to understand leaks, recognize them at runtime and deal with them.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/stackify.com\/memory-leaks-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg\",\"width\":881,\"height\":441},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stackify.com\/#website\",\"url\":\"https:\/\/stackify.com\/\",\"name\":\"Stackify\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stackify.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/stackify.com\/#organization\",\"name\":\"Stackify\",\"url\":\"https:\/\/stackify.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"width\":1377,\"height\":430,\"caption\":\"Stackify\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Stackify\/\",\"https:\/\/x.com\/stackify\",\"https:\/\/www.instagram.com\/stackify\/\",\"https:\/\/www.linkedin.com\/company\/2596184\",\"https:\/\/www.youtube.com\/stackify\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/e372b6a6f64edbedafad33027d518482\",\"name\":\"Eugen Paraschiv\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0c0bb39bd24aea78b56c6516a3e7dcab?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0c0bb39bd24aea78b56c6516a3e7dcab?s=96&d=mm&r=g\",\"caption\":\"Eugen Paraschiv\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Understand and Prevent Memory Leaks in a Java Application","description":"Memory leaks are a very real problem in Java and the JVM can only help so much. Learn how to understand leaks, recognize them at runtime and deal with them.","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:\/\/stackify.com\/memory-leaks-java\/","og_locale":"en_US","og_type":"article","og_title":"Understand and Prevent Memory Leaks in a Java Application","og_description":"Memory leaks are a very real problem in Java and the JVM can only help so much. Learn how to understand leaks, recognize them at runtime and deal with them.","og_url":"https:\/\/stackify.com\/memory-leaks-java\/","og_site_name":"Stackify","article_publisher":"https:\/\/www.facebook.com\/Stackify\/","article_published_time":"2017-08-14T10:37:48+00:00","article_modified_time":"2024-03-04T07:07:03+00:00","og_image":[{"width":881,"height":441,"url":"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg","type":"image\/jpeg"}],"author":"Eugen Paraschiv","twitter_card":"summary_large_image","twitter_creator":"@stackify","twitter_site":"@stackify","twitter_misc":{"Written by":"Eugen Paraschiv","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/stackify.com\/memory-leaks-java\/#article","isPartOf":{"@id":"https:\/\/stackify.com\/memory-leaks-java\/"},"author":{"name":"Eugen Paraschiv","@id":"https:\/\/stackify.com\/#\/schema\/person\/e372b6a6f64edbedafad33027d518482"},"headline":"How Memory Leaks Happen in a Java Application","datePublished":"2017-08-14T10:37:48+00:00","dateModified":"2024-03-04T07:07:03+00:00","mainEntityOfPage":{"@id":"https:\/\/stackify.com\/memory-leaks-java\/"},"wordCount":1902,"publisher":{"@id":"https:\/\/stackify.com\/#organization"},"image":{"@id":"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg","keywords":["Java"],"articleSection":["Developer Tips, Tricks &amp; Resources"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/stackify.com\/memory-leaks-java\/","url":"https:\/\/stackify.com\/memory-leaks-java\/","name":"Understand and Prevent Memory Leaks in a Java Application","isPartOf":{"@id":"https:\/\/stackify.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage"},"image":{"@id":"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg","datePublished":"2017-08-14T10:37:48+00:00","dateModified":"2024-03-04T07:07:03+00:00","description":"Memory leaks are a very real problem in Java and the JVM can only help so much. Learn how to understand leaks, recognize them at runtime and deal with them.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/stackify.com\/memory-leaks-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/memory-leaks-java\/#primaryimage","url":"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2017\/08\/Java-Memory-Leaks-881x441-1.jpg","width":881,"height":441},{"@type":"WebSite","@id":"https:\/\/stackify.com\/#website","url":"https:\/\/stackify.com\/","name":"Stackify","description":"","publisher":{"@id":"https:\/\/stackify.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stackify.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/stackify.com\/#organization","name":"Stackify","url":"https:\/\/stackify.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/","url":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","width":1377,"height":430,"caption":"Stackify"},"image":{"@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Stackify\/","https:\/\/x.com\/stackify","https:\/\/www.instagram.com\/stackify\/","https:\/\/www.linkedin.com\/company\/2596184","https:\/\/www.youtube.com\/stackify"]},{"@type":"Person","@id":"https:\/\/stackify.com\/#\/schema\/person\/e372b6a6f64edbedafad33027d518482","name":"Eugen Paraschiv","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0c0bb39bd24aea78b56c6516a3e7dcab?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0c0bb39bd24aea78b56c6516a3e7dcab?s=96&d=mm&r=g","caption":"Eugen Paraschiv"}}]}},"_links":{"self":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/12826"}],"collection":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/comments?post=12826"}],"version-history":[{"count":2,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/12826\/revisions"}],"predecessor-version":[{"id":42907,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/12826\/revisions\/42907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media\/38269"}],"wp:attachment":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media?parent=12826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/categories?post=12826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/tags?post=12826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}