Java Virtual machine 6: Garbage collection (GC)-1 (Differences in memory overflow and memory leaks)

Source: Internet
Author: User

1. Preface

There are several more important concepts that need to be popularized before garbage collection.

2. Concepts and differences in memory overflow and memory leaks:

(1): Memory overflow: Refers to the program in the application of memory, there is not enough memory space to allocate, the system can not meet the requirements, there is an out of memory; for example, an int was applied, but it saved a long to save the number, that is, memory overflow.

(2): Memory leak: Refers to the program after the application of memory, can not release the requested memory space, it always occupies the memory, so the more accumulate, the last memory is occupied. Memory leaks are typically caused by a large object in memory, but cannot be freed. Memory leaks will eventually result in memory overflow!

Note that the first step in locating a virtual machine problem memory problem is to determine whether it is memory overflow or memory leak, the former good judgment, tracking stack information can be, the latter is more complex, is generally the old age of large objects did not release, to find out in the old age of the big object is not released.

3. Classification of memory leaks and solutions

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. Accidental 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.

4. Causes of memory overflow and workarounds

There are a number of reasons why memory overflow occurs:

1. The amount of data loaded in memory is too large, such as fetching too much data from the database at once;
2. There are references to objects in the collection class that are not emptied after use, so that the JVM cannot be recycled;
3. There are dead loops or loops in the code that produce too many duplicate object entities;
4. Startup parameter memory value setting is too small;

Solution for Memory Overflow:

The first step is to modify the JVM startup parameters to increase the memory directly. (-XMS,-XMX parameter must not forget to add.) )

In the second step, check the error log to see if there are any other exceptions or errors before the "OutOfMemory" error.

The third step is to walk through and analyze the code to find out where the memory overflow might occur.

Focus on troubleshooting the following points:
1. Check to see if there is a single query for all data in the database query. In general, if you take 100,000 records to memory at a time, you can cause a memory overflow. This problem is more covert, before the online, the database less data, not prone to problems, on-line, the database more data, a query may cause memory overflow. Therefore, query the database query as much as possible by paging.

2. Check the code for a dead loop or recursive call.

3. Check to see if there are cycle duplicate generation of new object entities.

4. Check to see if there is a single query for all data in the database query. In general, if you take 100,000 records to memory at a time, you can cause a memory overflow. This problem is more covert, before the online, the database less data, not prone to problems, on-line, the database more data, a query may cause memory overflow. Therefore, query the database query as much as possible by paging.

5. Check that the list, map, and other collection objects are not cleared after use. Collection objects such as List, map, and so on will always have a reference to the object so that they cannot be reclaimed by GC.

Fourth step, use the memory viewing tool to dynamically view memory usage.

Reprint Address: http://www.cnblogs.com/xrq730/p/4839245.html

Java Virtual machine 6: Garbage collection (GC)-1 (Differences in memory overflow and memory leaks)

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.