Home > Back-end >  The difference between memory and memory leaks and the solution
The difference between memory and memory leaks and the solution

Time:11-01

1, memory leak memory leak: refers to the process after the application memory, can't release the memory has applied for space, a memory leak does not seem to have a big impact, but the memory leak consequence after the accumulation of memory,
2, memory leak out of memory: refers to the program application memory, there is not enough memory for the use of the applicant, or, give you a storage type int data storage space, but you can store long types of data, then the result is not enough memory to use, at this point would be an error OOM, the so-called memory,
3, the relationship between the two

Accumulation of memory leak will ultimately lead to memory overflow of memory is the memory space than the actual distribution system you want to give you space, the system is equivalent to can't meet your demand, will be submitted to the out of memory error and memory leak is you apply to the system allocate memory for use (new), but don't return after use (delete), you apply for to the results of the block of memory that you can no longer access (maybe you lost it addresses to), the system also can't assign it to need the program again, as you rented a key cabinet, after you put the things after the cabinet lock, had lost his keys or not return the key, then the result is that the cabinet will not be able to supply to anyone to use, also cannot be reclaimed by the garbage collector, because can't find any of his information, memory: a plate with a variety of methods can only four fruit, you have five, the results fell on the ground can't eat, this is overflow, for example stack, stack up again into the stack overflow, must have a space called overflow, do back again when the stack is empty stack overflow, also produce a space called the underflow, is allocated memory is not enough to put down the sequence of data items, called the memory, in plain English is I can't afford so much, then I can report errors,

4, the classification of the memory leak (by way of classification)

Often occurring memory leaks, code can be executed multiple times to a memory leak, every time is carried out will result in a memory leak, accidental memory leaks, the code memory leak occurred only in certain environmental or operating process occurs, and accidental is relative, often for a specific environment, sporadic may become permanent, so the test environment and test methods is very important to detect memory leaks, one-time memory leaks, memory leak occurred code will be executed only once, or because of shortfalls in the algorithm, cause there is always one and only one memory leak, for example, allocate memory in the constructor of a class, in the destructor but have not release the memory, so a memory leak occurs only once, implicit memory leaks, program continuously allocate memory in the process of running, but it was not until the end of the free memory, memory leak strictly speaking here didn't happen, because the final program released all the application of memory, but to a server program that needs to be run for a few days, weeks or even months, not timely release memory could also lead to eventually run out of all memory of the system, and so, we call this type of memory leak implicit memory leaks,

5, the cause and the solution: the memory

Out of memory:
1. The loading amount of data in the memory is too big, such as too much data removed from a database;
2. Collection class of object of reference, after the use is not empty, makes the JVM can't recycle;
3. Code in an infinite loop or loop produce too much repetition object entities;
4. Use of the third party software BUG;
5. Start the memory parameters setting is too small,

The memory solution:
The first step is to modify the JVM startup parameters, increasing memory directly, (Xms, -xmx parameter must don't forget to add,)

The second step, check the error log, see if there are any more its "OutOfMemory error before it exception or error,

Third step, the code walkthrough and analysis, find out the possible memory leak location,

Focus on trying to identify the following:
1. Check the database query, whether to have a full data query, in general, if take one hundred thousand records into memory at a time, it may cause out of memory, this problem is more covert, before the launch, less data in the database, it is not easy to a problem, after launch, more data in the database, a query is likely to cause a memory leak, so for database query as far as possible with the method of paging query,

2. Check the code for infinite loop or recursive calls,

3. Check whether there is a large cycle repeated to generate new object entities,

4. Check the database query, whether to have a full data query, in general, if take one hundred thousand records into memory at a time, it may cause out of memory, this problem is more covert, before the launch, less data in the database, it is not easy to a problem, after launch, more data in the database, a query is likely to cause a memory leak, so for database query as far as possible with the method of paging query,

5. Check the List, the MAP after the collection objects, such as whether to have to use, not clear question, List, MAP collection objects such as always being the object of reference, to make these objects cannot be GC recycling,

The fourth step, the use of dynamic memory view tool for memory usage

CodePudding user response:

Memory leak problem is the hardest to screening,
The fourth step, memory view tool, can only be observed whether the memory suddenly increases, it will be very hard to locate where out of memory.

CodePudding user response:

Excel data export, products are all exported 100000 tm data, not paging
  • Related