Java memory leaks and processing

Source: Internet
Author: User
Tags object object

Memory overflow out of memory, refers to the program in the application when the RAM, there is not enough memory space for its use, appear out of the memory;

Memory leak memories leak, refers to the program after the application of memory, can not release the requested memory space, a memory leak damage may be ignored, but the memory leak accumulation of serious consequences, no matter how much memory, sooner or later will be occupied.

Memory leak will eventually result in out of memory!

In the way that happens, memory leaks can be categorized into 4 categories:

    1. Frequent memory leaks. The code that occurs in memory leaks is executed multiple times, causing a memory leak each time it is executed.
    2. Sporadic memory leaks. Code that occurs with a memory leak occurs only under certain circumstances or during operation. The occurrence and the incidental sex are opposite. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.
    3. Disposable memory leaks. The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a piece of memory that leaks. For example, allocating memory in the class's constructor does not release the memory in the destructor, so a memory leak occurs only once.
    4. An implicit memory leak. The program keeps allocating memory while it is running, but it does not release memory until the end. Strictly speaking, there is no memory leak, because the final program frees up all the requested memory. But for a server program that needs to run for days, weeks, or months, not releasing memory in time can also result in the eventual exhaustion of all of the system's memory. So, we call this kind of memory leak as an implicit memory leak.

From the user's point of view of using the program, the memory leak itself does not have any harm, as a general user, there is no sense of memory leaks. What is really harmful is the accumulation of memory leaks, which eventually consumes all the memory of the system. From this point of view, a one-time memory leak is harmless, because it does not accumulate, and the implicit memory leak is very harmful because it is more difficult to detect than the usual and sporadic memory leaks

First, the Java Memory recovery mechanism
Regardless of the memory allocation in any language, it is necessary to return the actual address of the allocated memory, that is, to return a pointer to the first address of the memory block. Objects in Java are created using new or reflected methods, which are created in the heap, and all objects are reclaimed by the Java virtual machine through a garbage collection mechanism. In order to properly dispose of objects, the GC monitors the health of each object, monitors their applications, references, references, assignments, and so on, and Java uses a graph-like approach to manage memory, real-time monitoring of whether objects can be reached, and if not, to recycle them.

Ii. causes of Java memory leaks
Memory leaks refer to useless objects (objects that are no longer being used) that persist in memory or the memory of useless objects is not released in a timely manner, resulting in a waste of memory space known as memory leaks. Memory leaks are sometimes not critical and imperceptible, so developers do not know that there is a memory leak, but sometimes it can be very serious and will prompt you out of your memory.

So what is the root cause of the Java memory leak? Long life-cycle objects that hold references to short life-cycle objects are likely to have a memory leak, although a short life-cycle object is no longer needed, but because a long-life-cycle object holds its reference and cannot be recycled, this is the scenario where memory leaks occur in Java. There are several major categories:
1. A static collection class causes a memory leak:
Uses such as HashMap, vectors, and so on are the most prone to memory leaks, and the lifetime of these static variables is consistent with the application, and all objects referenced by them cannot be freed because they will always be referenced by vectors.

Static vector v = new vector (10);
for (int i = 1; i<100; i++)
{
Object o = new Object ();
V.add (o);
o = null;
}//
Copy Code
In this example, the loop applies to the Object object and puts the requested objects into a vector, and if you just release the reference itself (O=null), then the vector still references the object, so the object is not recyclable to the GC. Therefore, if the object has to be removed from the vector after it has been added to the vector, the simplest way is to set the vector object to null. ----Add V=null outside the loop so that the memory inside the vector can be recycled.

Case 2
public static void Main (string[] args) {
if (true) {
ArrayList

Outside the scope of the local variable, AL this reference becomes a null pointer, but the new arraylist< String > () object can still exist, its actual address value is the object in the memory of the address or stored in the stack, the GC can not be recycled it

public static void Main (string[] args) {
if (true) {
ArrayList

Java memory leaks and processing

Related Article

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.