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 java jvm memory overflow and memory leak

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "what is the difference between java jvm memory overflow and memory leak". In daily operation, I believe many people have doubts about the difference between java jvm memory overflow and memory leak. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between java jvm memory overflow and memory leak"! Next, please follow the editor to study!

1. Distinction between concepts memory overflow out of memory

Refers to the program in the application for memory, there is not enough memory space for its use, the emergence of out of memory.

For example, apply for a 10MB space, but the current memory is only 5MB, that is, memory overflow.

Javadoc's explanation of OutOfMemoryError is that there is no free memory, and the garbage collector fails to provide more memory space.

Memory leak memory leak

It means that the memory space already occupied is not released after the program is run, and a memory leak does not seem to have a big impact, but the consequence of memory leak accumulation is memory overflow.

1) singleton mode

The life cycle of a singleton is as long as that of an application, so if a singleton holds a reference to an external object, then the external object cannot be recycled, which will lead to memory leaks.

2) memory leaks caused by unclosed resources that provide close

The connection of database connection (dataSource.getConnection ()), network connection (socket) and IO stream must be close in finally, otherwise it cannot be reclaimed.

2. The relationship between the two:

Memory overflow means that the memory space you want exceeds the space actually allocated to you by the system. When the system is unable to meet your needs, it will report a memory overflow error.

Memory overflow: a plate can only hold 4 fruits in various ways, but you have 5, and you fall to the ground and can't eat it. This is spillover. For example, the stack, when the stack is full, it is bound to produce space overflow, which is called upper overflow, and when the stack is empty, it also produces space overflow, which is called underflow. That is, the allocated memory is not enough to drop the sequence of data items, which is called memory overflow. To put it bluntly, I can't bear so much, so I'm wrong.

Memory leak means that you apply to the system for allocating memory for use (new), but do not return it after use (delete). As a result, you can no longer access the piece of memory you applied for (maybe you lost its address), and the system cannot allocate it to the needed program again. It is equivalent to renting a cabinet with a key. after you lock the cabinet after you have saved something, you lose the key or do not return the key, then the result is that the cabinet will not be available to anyone and cannot be recycled by the garbage collector. because I can't find any information about him.

The accumulation of memory leaks will eventually lead to memory overflows.

3. The cause of memory overflow and its solution 3.1. The cause of memory overflow.

There are many reasons for memory overflow. The editor lists the following common ones:

The amount of data loaded in memory is too large, such as fetching too much data from the database at once

There is a reference to the object in the collection class, which is not cleared after use, so that the JVM cannot be recycled.

There is an endless loop in the code or an object entity whose loop produces too much repetition.

BUG in the third-party software used

The startup parameter memory value is set too small.

3.2. Solution to memory overflow:

The first step is to modify the JVM startup parameters to directly increase memory. (don't forget to add the parameters-Xms and-Xmx)

The second step is to check the error log to see if there are 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 may occur.

Focus on the following points:

1. Check whether there is a query to get all the data in the database query.

Generally speaking, if 100, 000 records are taken to memory at a time, it may cause a memory overflow.

This problem is more hidden, before the launch, there is less data in the database, so it is not easy to cause problems.

If there is too much data in the database, a query may cause a memory overflow. Therefore, the database query should be paged as far as possible.

two。 Check the code for endless loops or recursive calls.

3. Check to see if there are large loops that repeatedly generate new object entities.

4. Check whether there is a query to get all the data in the database query.

Generally speaking, if 100, 000 records are taken to memory at a time, it may cause a memory overflow.

This problem is more hidden, before going online, there is less data in the database, so it is not easy to cause problems.

After going online, there is more data in the database, and a query may cause memory overflow. Therefore, the database query should be paged as far as possible.

5. Check whether there are any problems that have not been cleared after the collection objects such as List and MAP are finished.

Collection objects such as List, MAP, etc., always have references to objects, so that these objects cannot be recycled by GC.

The fourth step is to use the memory View tool to dynamically view memory usage.

At this point, the study on "what is the difference between java jvm memory overflow and memory leak" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 204

*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