Record Learning bits (Java memory leaks) __java

Source: Internet
Author: User
Tags object object
Java memory leaks
1. Definition
Objects that are no longer used continue to consume memory or objects that are no longer in use and are not released in time, resulting in a waste of memory space.
(The memory of the object that is no longer in use cannot be reclaimed)
2. Examples and Solutions
--1 objects are life-cycle, some long, some short, if the long life cycle of the object holding a short life cycle of reference, it is likely that memory leaks.
public class Simple {
Object object;
public void Method1 () {
Object = new Object ();
//... Other code
}
}
The object instance here, in fact, we expect it to work only in the Method1 () method, and it will not be used elsewhere, but when the Method1 () method completes, the memory allocated by the object objects is not immediately considered an object that can be freed, Only the object created by the Simple class is released, and strictly speaking, this is a memory leak. The workaround is to use object as a local variable in the Method1 () method. Of course, if you have to write this, you can change this way:
public class Simple {
Object object;
public void Method1 () {
Object = new Object ();
//... Other code
object = null;
}
}
In this way, the memory allocated by the "new Object ()" can be reclaimed by GC.
When the memory object is no longer needed, it still retains the memory and the way it is accessed (referenced), which is a memory leak that is likely to occur in all languages. If you are not careful in programming, we can easily happen this way, if not too serious, it may just be a short memory leak.
Memory leaks when--2 containers are in use
void Method () {
Vector vector = new vector ();
for (int i = 1; i<100; i++)
{
Object = new Object ();
Vector.add (object);
object = null;
}
//... Operations on vectors
//... Other operations that have nothing to do with vectors
}
Memory leaks here refer to the fact that the following vector-independent code is executed after the vector operation, and if a GC operation occurs, the sequence of object is not recoverable, and the memory leak here may be short-lived because after the completion of the entire method () Those objects can still be recycled. The solution here is simple, and the manual assignment is null.
void Method () {
Vector vector = new vector ();
for (int i = 1; i<100; i++)
{
Object = new Object ();
Vector.add (object);
object = null;
}
//... Operations on V
vector = null;
//... Other operations that have nothing to do with V
}
--3 various objects that provide a close () method
For example, database connections (Datasourse.getconnection ()), Network Connections (sockets) and IO connections, and other frameworks, unless they explicitly invoke their close () method (or similar method) to turn their connection off, Otherwise, it is not automatically recycled by GC. The reason is still the long life cycle object holds a reference to a short lifecycle object. Many people may have used hibernate, and when we manipulate the database, we get a session through Sessionfactory.
Session session=sessionfactory.opensession ();
We must call the close () method off when we are done.
Session.close ();
Sessionfactory is a long lifecycle object, and the session is a short lifecycle object, but the framework is designed to be reasonable: it's not clear how long we're going to use the session, so we can only provide a way for us to decide when to stop using it. Because an exception may be thrown before the close () method call, and the method cannot be invoked, we usually use the try language and then perform cleanup work such as close () in the finally statement.
try{
Session=sessionfactory.opensession ();
//... Other actions
}finally{
Session.close ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.