There are many reasons for memory overflow, the common ones are as follows:
The amount of data loaded in the memory is too large Huge, such as retrieving too much data from the database at one time;
There are references to objects in the collection class, which are not cleared after use, making the JVM unable to recycle;
There are infinite loops or loops in the code Generating too many duplicate object entities;
BUG in the third-party software used;
The startup parameter memory value is set too small;
Memory overflow Solution:
The first step is to modify the JVM startup parameters and directly increase the memory. (Be sure to add the -Xms and -Xmx parameters.)
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 memory overflow may occur.
Focus on the following points:
Check whether there is a query to obtain all data at once in the database query. Generally speaking, if one hundred thousand records are fetched into the memory at one time, it may cause a memory overflow. This problem is relatively hidden. Before going online, there was less data in the database and it was less likely to cause problems. After going online, there was more data in the database, and a single query may cause memory overflow.
Therefore, try to use paging for database queries. Check the code for infinite loops or recursive calls. Check whether there is a large loop that repeatedly generates new object entities.
Check whether there is a query to obtain all the data in the database query. Generally speaking, if one hundred thousand records are fetched into the memory at one time, it may cause a memory overflow.
This problem is relatively hidden. Before going online, there was less data in the database and it was less likely to cause problems. After going online, there is more data in the database, and a single query may cause memory overflow.
Therefore, try to use paging for database queries.
Check whether collection objects such as List and MAP are not cleared after use. Collection objects such as List and MAP will always have references to the objects, making these objects unable to be recycled by GC.
The fourth step is to use the memory viewing tool to dynamically view memory usage.
The above is the detailed content of java memory overflow interview questions. For more information, please follow other related articles on the PHP Chinese website!