Memory Leak and Memory overflow

Source: Internet
Author: User

1. What is a memory leak (leak)?

A situation in which a program fails to release memory that is no longer in use due to negligence or error. A memory leak does not mean that there is physical disappearance, but that the application allocates some memory, and because of the design error, it loses the control of the memory, thus causing the memory waste.


2. Two types of memory leaks:


Heap memory leak (heap leak). The memory refers to the allocation of a piece of memory from the heap, such as Malloc,realloc new, as required by the program, and must be deleted by calling the corresponding free or delete after completion. If the program's design errors cause this part of the memory to not be released, then this memory will not be used, resulting in heap Leak.


System Resource Leakage (Resource Leak). Mainly refers to the program using the system allocation of resources such as bitmap,handle, socket and so does not use the corresponding function to release, resulting in system resources waste, serious can lead to lower system performance, system operation is not stable.
In Linux, two file descriptors point to a filename. Must be closed two times, or memory leaks will occur
Also in Linux, open () must add close () after
Eg:closehandle () closesocket ()

Memory leak leads to memory overflow topic:
The so-called memory overflow is that you want to allocate more memory than the system can give you, the system can not meet the requirements, so it will create a memory overflow problem.

Common spills include:
The memory allocation was unsuccessful, but it was used.
A common workaround is to check if the pointer is null before using memory. If the pointer p is an argument to a function, it is checked with an assert (P!=null) at the entrance of the function. If you are using malloc or new to request memory, you should use if (p==null) or if (P!=null) for error-proof handling.

The memory allocation succeeds, but it is not initialized to reference it.
The memory allocation succeeds and has been initialized, but the operation crosses the memory boundary.
For example, the use of arrays often occurs when the subscript "more 1" or "less 1" operation. Especially in a For loop statement, the number of loops can be easily mistaken, resulting in array operations being out of bounds.

After freeing memory with free or delete, the pointer is not set to null. Causes the "wild pointer" to be produced.

Memory Leak and Memory overflow

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.