Memory overflow and memory leaks

Source: Internet
Author: User

Memory overflow

One, the popular understanding is that the memory is not enough, usually when running large software or games, software or games need more memory than the memory installed in your host size, called memory overflow. At this point the software or game will not run, the system will prompt memory overflow, and sometimes automatically shut down the software, restart the computer or software after releasing a portion of memory and can run the software or game for a period of time.

For the sake of understanding, we might as well make an analogy. A buffer overflow is like putting 10 pounds of sugar into a container that can only fit five pounds. Once the container is full, the rest is spilled over the counter and the floor, making a mess. Because the writers of the computer program wrote some code, the code did not check the destination area or buffer-five-pound container-to see if they were large enough to fit completely into the new content-10 pounds of sugar, which could result in a buffer overflow. If the data that is intended to be put into a new place is not appropriate, overflowing everywhere, the data can create a lot of trouble. However, if the buffer only overflows, this is only a problem. Up to this point, it's not destructive. The counter is covered when the sugar overflows. You can wipe off the sugar or use a vacuum cleaner, and the counter is the original face. In contrast, when a buffer overflows, the excess information overwrites the previous contents of the computer's memory. Unless the overwritten content is saved or recoverable, it will be lost forever.

    has a list of subroutines in the missing message that can be called by the program until a buffer overflow occurs. In addition, the information given to those subroutines--parameters--is also lost. This means that the program cannot get enough information to return from the subroutine to complete its task. Just like a man walking through the desert. If he relies on his footsteps to backtrack, he will be lost in the desert when the dust storms wipe away the traces. This problem is much more serious than the program is just lost in the direction. The intruder uses a well-written intrusion code (a malicious program) to overflow the buffer and then tells the program to process the buffer according to the preset method and execute. At this point the program has been completely manipulated by the intruder. Intruders often adapt their existing application Run a different program. For example, an intruder can launch a new program that sends a secret file (check book, password file, or property list) to an intruder's e-mail

Second, there are many reasons for memory overflow, the following are common:

The amount of data loaded in memory is too large, such as fetching too much data from the database at once;The collection class has references to objects that are not emptied after use, making the JVM not recyclable; There is a dead loop or loop in the code that produces too many duplicate object entities;

bugs in the third-party software used ;

Boot parameter memory value setting is too small;

Memory leaks;

Third, avoid memory overflow

Minimize memory leaks, apply the dynamic space after use to remember to release;

Application of sufficient parameter memory;

Program optimization, avoid the cycle of death;

Remember to reclaim the release.

Memory leaks

Memory leak, memory leaks are a common problem in large, complex applications. A memory leak occurs when a previously allocated piece of memory is no longer needed or inaccessible, but does not release it, resulting in a decrease in total available memory for that process. While good programming practices ensure minimal leaks, the experience is that memory leaks are likely to occur when a large number of functions are used to process the same block of memory. This is especially true in situations where the wrong path is encountered.

memory leaks, also known as "storage Leaks", are dynamically opened up with dynamic storage allocation functions and are not released after they have been used, resulting in the deposit element being occupied. Until the end of the program. (In fact, the memory space is not recycled after use) is called a memory leak. Memory leaks The image metaphor is that "the storage space that the operating system can provide to all processes is being squeezed by a process", the end result is that the longer the program runs, the more storage space is consumed, and eventually the entire storage space is exhausted and the whole system crashes. So a "memory leak" is from the operating system's point of view. The storage space here does not refer to the physical memory, but the virtual memory size, which depends on the size of the disk swap area setting. A piece of memory requested by the program, and if there is no pointer pointing to it, then this memory is leaked.

In general, the memory leaks that we often say refer to the leaks in heap memory. Heap memory refers to the allocation of the program from the heap, any size (the size of the memory block can be determined during the program run time), after the use of the memory must be explicitly freed. Applications typically use functions such as malloc,realloc,new to allocate a piece of memory from the heap, and after use, the program must be responsible for the corresponding call to free or delete to release the memory block, otherwise the memory cannot be reused, we say this memory leak .

Second, the cause of memory leakage:


Often-made sex

The code that occurs in memory leaks is executed multiple times, causing a memory leak each time it is executed.

Sporadic nature

Code that occurs with a memory leak occurs only under certain circumstances or during operation. The occurrence and the incidental sex are opposite. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.

Disposable

The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a block and only one piece of memory leaks. For example, allocating memory in the class's constructor does not release the memory in the destructor, so a memory leak occurs only once.

Implicit

The program keeps allocating memory while it is running, but it does not release memory until the end. Strictly speaking, there is no memory leak, because the final program frees up all the requested memory. However, for a server program that needs to run for several days, weeks, or months, not releasing memory in a timely manner can result in the eventual exhaustion of all of the system's memory. So, we call this kind of memory leak as an implicit memory leak.

Third, avoid memory leaks

Objects and parameters with malloc or new should be released with delete or free to avoid occupancy;

Conditional statement if or switch although each case has requested space but only satisfies the condition to execute, so each time only executes one statement, the other will also occupy, so try to avoid the use of conditional statements utilization.




Suimi Hao Hao







Memory overflow and memory leaks

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.