Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between memory leak and memory overflow in JAVA

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you Java memory leaks and memory overflow what the difference is, concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.

1. Memory leak :

It means that after the program applies for memory, it cannot release the memory space that has been applied for. A memory leak does not seem to have a big impact, but the consequence after the accumulation of memory leaks is memory overflow.

2. Memory overflow out of memory :

When the program requests memory, there is not enough memory for the applicant to use, or to give you a piece of storage space to store int type data, but you store long type data, then the result is insufficient memory, at this time will report OOM, the so-called memory overflow.

3. Relationship between the two:

Accumulation of memory leaks eventually leads to memory overflow

Memory overflow means that the memory space you want exceeds the space actually allocated to you by the system. At this time, the system is equivalent to not meeting your needs, and it will report a memory overflow error.

A memory leak is when you ask the system to allocate memory for use (new), but do not return it (delete) after use, so that you can no longer access the memory you requested (perhaps you lost its address), and the system can no longer allocate it to the program that needs it. It's like renting a locker with a key, locking it after you've stored it, losing the key or not returning it, and the result is that the locker won't be available to anyone, and it won't be recycled by the garbage collector, because you can't find any information about him.

Memory overflow: a plate exhausted all kinds of methods can only hold 4 fruits, you loaded 5, the result fell to the ground can not eat. This is overflow. For example, when the stack is full and then pushed into the stack, it must produce space overflow, called overflow, and when the stack is empty, it will also produce space overflow, called underflow. This means that the allocated memory is not enough to hold the sequence of data items, called a memory overflow. To put it bluntly, that is, I can't bear so much, then I will report wrongly.

4. Classification of memory leaks (classified by the way they occur)

Recurrent memory leaks. The code that has a memory leak will be executed multiple times, and each time it is executed, it will cause a memory leak.

Occasional memory leaks. Code that leaks memory can occur only under certain circumstances or operating procedures. Frequent and sporadic are relative. For a given environment, sporadic may become recurrent. So the test environment and test method are very important to detect memory leak.

One-time memory leak. Code that leaks memory is executed only once, or due to a bug in the algorithm, there will always be a single piece of memory that leaks. For example, memory is allocated in a class constructor but not freed in a destructor, so the memory leak occurs only once.

4. Implicit memory leak. The program allocates memory continuously during execution, but does not release memory until it is finished. Strictly speaking, there is no memory leak because the program eventually frees up all requested memory. But for a server program to run for days, weeks, or even months, failing to free up memory in time could eventually exhaust all of the system's memory. Therefore, we call this type of memory leak an implicit memory leak.

5. Causes and solutions of memory overflow:

(1) Memory overflow causes:

The amount of data loaded in memory is too large, such as taking too much data from the database at one time;

There are references to objects in the collection class that are not emptied after use, making the JVM unable to recycle;

There is an infinite loop in the code or a loop that produces too many repeated object entities;

Bugs in third-party software used;

The memory value of startup parameter is set too small

(2) Solution to memory overflow:

The first step is to modify JVM startup parameters and directly increase memory. (-Xms, -Xmx parameter must not forget to add.)

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

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

Focus on the following points:

1. Check whether there is a query that gets all the data at once in the database query. In general, if you take 100,000 records into memory at a time, it may cause memory overflow. This problem is relatively hidden. Before going online, there is less data in the database, so it is not easy to have problems. After going online, there is more data in the database, and a query may cause memory overflow. Therefore, for database queries as far as possible to use paging query.

2. Check your code for endless loops or recursive calls.

3. Check if there is a large loop repeating to generate new object entities.

4. Check whether there is a query that gets all the data at once in the database query. In general, if you take 100,000 records into memory at a time, it may cause memory overflow. This problem is relatively hidden. Before going online, there is less data in the database, so it is not easy to have problems. After going online, there is more data in the database, and a query may cause memory overflow. Therefore, for database queries as far as possible to use paging query.

5. Check whether collection objects such as List and MAP are not cleared after use. Collection objects such as List and MAP always have references to objects, making them impossible to recycle by GC.

Step 4: Use the Memory View tool to dynamically view memory usage

What is the difference between memory leak and memory overflow in JAVA? Have you learned knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report