When a program gets executed, it uses memory. In some cases, a poorly-written program may incorrectly manage the memory that has been given to it. As a consequence, the memory that was actually supposed to be reused after the program has finished its job cannot be accessed again. This is known as a memory leak.
One of the most common reasons for memory leaks is the program not giving its memory back after it has finished execution.
Memory leaks may result in several issues such as degradation of performance and systems being unresponsive.
Out of memory (OOM) is an often undesired state of computer operation where no additional memory can be allocated for use by programs or the operating system. Such a system will be unable to load any additional programs, and since many programs may load additional data into memory during execution, these will cease to function correctly. This usually occurs because all available memory, including disk swap space, has been allocated.
A Memory Leak is a situation where there are objects present in the heap that are no longer used, but the garbage collector is unable to remove them from memory, and therefore, they're unnecessarily maintained.
A memory leak is bad because it blocks memory resources and degrades system performance over time. If not dealt with, the application will eventually exhaust its resources, finally terminating with a fatal java.lang.OutOfMemoryError.
Although they might be sometimes used interchangeably, we should associate an end user when we are talking about an application and relate a program to a set of instructions that could be executed on a computer.
The first scenario[səˈnærioʊ](场景) that can cause a potential memory leak is heavy use of static variables.
In Java, static fields have a life that usually matches the entire lifetime of the running application (unless ClassLoader becomes eligible for garbage collection).
Let's create a simple Java program that populates a staticList:
public class StaticTest { public static List<Double> list = new ArrayList<>(); public void populateList() { for (int i = 0; i < 10000000; i++) { list.add(Math.random()); } Log.info("Debug Point 2"); } public static void main(String[] args) { Log.info("Debug Point 1"); new StaticTest().populateList(); Log.info("Debug Point 3"); }}
If we analyze the Heap memory during this program execution, then we'll see that between debug points 1 and 2, the heap memory increased as expected.
But when we leave the populateList() method at the debug point 3, the heap memory isn't yet garbage collected, as we can see in this VisualVM response:
However, if we just drop the keyword static in line number 2 of the above program, then it'll bring a drastic(激烈的) change to the memory usage, as shown in this Visual VM response:
The first part until the debug point is almost the same as what we obtained in the case of static. But this time, after we leave the populateList() method, all the memory of the list is garbage collected because we don't have any reference to it.
So we need to pay very close attention to (密切关注)our usage of static variables. If collections or large objects are declared as static, then they remain in the memory throughout the lifetime of the application, thus blocking vital memory(重要内存) that could otherwise be used elsewhere(本该在其它地方使用).
How to Prevent It?
Minimize the use of static variables.
When using singletons, rely upon an implementation that lazily loads the object, instead of eagerly loading.
Whenever we make a new connection or open a stream, the JVM allocates memory for these resources. A few examples of this include database connections, input streams, and session objects.
Forgetting to close these resources can block the memory, thus keeping them out of the reach of the GC. This can even happen in case of an exception that prevents the program execution from reaching the statement that's handling the code to close these resources.
In either case, the open connection left from the resources consumes memory, and if we don't deal with them, they can deteriorate performance, and even result in an OutOfMemoryError.
How to Prevent It?
Always use finally block to close resources.
The code (even in the finally block) that closes the resources shouldn't have any exceptions itself.
When using Java 7+, we can make use of the try-with-resources block.
When defining new classes, a very common oversight(疏忽) is not writing proper overridden methods for the equals() and hashCode() methods.
HashSet and HashMap use these methods in many operations, and if they're not overridden correctly, they can become a source for potential memory leak problems.
Let's take an example of a trivial Person class, and use it as a key in a HashMap:
public class Person { public String name; public Person(String name) { this.name = name; }}
Now we'll insert duplicate Person objects into a Map that uses this key.
Here we're using Person as a key. Since Map doesn't allow duplicate keys, the numerous duplicate Person objects that we inserted as a key shouldn't increase the memory.
But since we haven't defined the proper *equals()* method, the duplicate objects pile up(堆积) and increase the memory, which is why we see more than one object in the memory. The Heap Memory in VisualVM for this looks like:
Before implementing equals and hashcode
However, if we'd overridden the *equals()* and *hashCode()* methods properly, then only one *Person* object would exist in this *Map.*
Let's take a look at the proper implementations of equals() and hashCode() for our Person class:
public class Person { public String name; public Person(String name) { this.name = name; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Person)) { return false; } Person person = (Person) o; return person.name.equals(name); } @Override public int hashCode() { int result = 17; result = 31 * result + name.hashCode(); return result; }}
And in this case, the following assertions would be true:
After properly overriding equals() and hashCode(), the Heap Memory for the same program looks like:
Afterimplementing equals and hashcode
Another option is using an ORM tool like Hibernate, which uses the equals() and hashCode() methods to analyze the objects and saves them in the cache.
The chance of memory leaks is quite high if these methods aren't overridden because Hibernate wouldn't be able to compare objects and would fill its cache with duplicate objects.
How to Prevent It?
As a rule of thumb, when defining new entities, always override the equals() and hashCode() methods.
It's not enough to just override, these methods must be overridden in an optimal way(最佳方式) as well.
This happens in the case of non-static inner classes (anonymous classes(匿名类)). For initialization, these inner classes always require an instance of the enclosing class.
Every non-static Inner Class has, by default, an implicit reference(隐式引用) to its containing class. If we use this inner class' object in our application, then even after our containing class' object goes out of scope, it won't be garbage collected.
Consider a class that holds the reference to lots of bulky(笨重的) objects and has a non-static inner class. When we create an object of just the inner class, the memory model looks like:
Inner Classes That Reference Outer Classes
However, if we just declare the inner class as static, then the same memory model looks like this:
Static Classes That Reference Outer Classes
This happens because the inner class object implicitly holds a reference to the outer class object, thereby making it an invalid candidate for garbage collection. The same happens in the case of anonymous classes.
How to Prevent It?
If the inner class doesn't need access to the containing class members, consider turning it into a static class.
Use of finalizers is yet another source of potential memory leak issues. Whenever a class' finalize() method is overridden, then objects of that class aren't instantly garbage collected. Instead, the GC queues them for finalization, which occurs at a later point in time.
Additionally, if the code written in the finalize() method isn't optimal, and if the finalizer queue can't keep up with the Java garbage collector, then sooner or later our application is destined to meet an OutOfMemoryError.
To demonstrate this, let's imagine that we have a class for which we've overridden the finalize() method, and that the method takes a little bit of time to execute. When a large number of objects of this class get garbage collected, it looks like this in VisualVM:
Finalize method overridden
However, if we just remove the overridden finalize() method, then the same program gives the following response:
The Java String pool went through a major change in Java 7 when it was transferred from PermGen to HeapSpace. However, for applications operating on version 6 and below, we need to be more attentive when working with large Strings.
If we read a massive String object, and call intern() on that object, it goes to the string pool, which is located in PermGen (permanent memory), and will stay there as long as our application runs. This blocks the memory and creates a major memory leak in our application.
The PermGen for this case in JVM 1.6 looks like this in VisualVM:
Interned Strings
In contrast, if we just read a string from a file in a method, and don't intern it, then the PermGen looks like:
Normal Strings
How to Prevent It?
The simplest way to resolve this issue is by upgrading to the latest Java version, as String pool moved to HeapSpace starting with Java version 7.
If we're working on large Strings, we can increase the size of the PermGen space to avoid any potential OutOfMemoryErrors:
String Pool is possible only because String is immutable(不可变) in Java and its implementation of String interning concept. String pool is also example of Flyweight design pattern. String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String(尽管创建字符串需要更多的时间,字符串池有助于为 JavaRuntime 节省大量空间).
When we use double quotes to create a String, it first looks for String with the same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference. However using new operator, we force String class to create a new String object in heap space. We can use intern() method to put it into the pool or refer to another String object from the string pool having the same value.
ThreadLocal (discussed in detail in the Introduction to ThreadLocal in Java tutorial) is a construct that gives us the ability to isolate state to a particular thread, and thus allows us to achieve thread safety.
When using this construct, each thread will hold an implicit reference to its copy of a ThreadLocal variable and will maintain its own copy, instead of sharing the resource across multiple threads, as long as the thread is alive.
Despite its advantages, the use of ThreadLocal variables is controversial, as they're infamous for introducing memory leaks if not used properly. Joshua Bloch once commented on thread local usage that:
“Sloppy use of thread pools in combination with sloppy use of thread locals can cause unintended object retention, as has been noted in many places. But placing the blame on thread locals is unwarranted.”
Memory Leaks with ThreadLocals
ThreadLocals are supposed to be garbage collected once the holding thread is no longer alive. But the problem arises when we use ThreadLocals along with modern application servers.
Modern application servers use a pool of threads to process requests, instead of creating new ones (for example, the Executor in the case of Apache Tomcat). Moreover, they also use a separate classloader.
线程池使用单独的类加载器,使用的是线程池,不会进行垃圾回收。
Since Thread Pools in application servers work on the concept of thread reuse, they're never garbage collected; instead, they're reused to serve another request.
If any class creates a ThreadLocal variable, but doesn't explicitly remove it, then a copy of that object will remain with the worker Thread even after the web application is stopped, thus preventing the object from being garbage collected.
如果任何类创建了 ThreadLocal 变量,但没有显式地删除它,那么该对象的一个副本将保留在工作线程中,即使在 Web 应用程序停止之后也是如此,从而对象无法被垃圾收集。
How to Prevent It?
It's good practice to clean-up ThreadLocals when we're no longer using them. ThreadLocals provide the remove() method, which removes the current thread's value for this variable.
Don't use ThreadLocal.set(null) to clear the value. It doesn't actually clear the value, but will instead look up the Map associated with the current thread and set the key-value pair as the current thread and null, respectively.
Java profilers are tools that monitor and diagnose the memory leaks through the application. They analyze what's going on internally in our application, like how we allocate(分配) memory.
Using profilers, we can compare different approaches(方法) and find areas where we can optimally(最佳地) use our resources.
Throughout section 3 of this tutorial, we used Java VisualVM. Please check out our Guide to Java Profilers to learn about different types of profilers, like Mission Control, JProfiler, YourKit, Java VisualVM, and the Netbeans Profiler.
By enabling verbose(详细的) garbage collection, we can track the detailed trace of the GC. To enable this, we need to add the following to our JVM configuration:
-verbose:gc
By adding this parameter, we can see the details of what's happening inside the GC:
verbose-garbage-collection
这一块主要讲了,使用-verbose:gc命令,以查看更详细的垃圾回收内容。
Full GC后面的括号就是本次GC所产生的原因。
Full GC后面有一个单词叫Ergonomics,Ergonomics翻译成中文,一般都是“人体工程学”。在JVM中的垃圾收集器中的Ergonomics就是负责自动的调解gc暂停时间和吞吐量之间的平衡,然后你的虚拟机性能更好的一种做法。
We can also resort to reference objects in Java that come built-in with the java.lang.ref package to deal with memory leaks. Using the java.lang.ref package, instead of directly referencing objects, we use special references to objects that allow them to be easily garbage collected.
Reference queues make us aware of the actions the Garbage Collector performs. For more information, we can read the Soft References in Java tutorial, specifically section 4.
For projects on JDK 1.5 and above, Eclipse shows warnings and errors whenever it encounters obvious cases of memory leaks. So when developing in Eclipse, we can regularly visit the “Problems” tab and be more vigilant about memory leak warnings (if any):
We can measure and analyze the Java code's performance by executing benchmarks. This way, we can compare the performance of alternative approaches to do the same task. This can help us choose the best approach, and may help us conserve memory.