MySQL OOM series I Linux memory allocation, mysqloom

Source: Internet
Author: User

MySQL OOM series I Linux memory allocation, mysqloom

RDS (Netease cloud Relational Database Service) has been launched for some time, and products have been constantly migrated to RDS. During online O & M, some products have not been taken into account, or not all things. You can share it with us later.

Today, I want to mention a 4G RDS instance online. The out of memory (out of memory) problem occurs, and the MySQL process is killed directly. To solve this problem, we first need to talk about the memory allocation policy in Linux.
Generally, we use malloc to dynamically apply for memory space (JVM is responsible for memory management in Java). The malloc function will apply for a continuous memory unit from the operating system, then return the starting address of the space. If the return value of the malloc function is null, the system has no memory available for allocation. This is our general thinking. Of course, this is true in some operating systems (Solaris ).
But this is not the case in Linux. the Linux memory allocation adopts a more active allocation policy. It assumes that the application does not use the memory immediately after applying for the memory space, therefore, a certain amount of oversales is allowed. When an application really needs to use it, the operating system may have become able to meet the needs of this application by reclaiming the memory space of other applications, simply put, the application is allowed to apply for more memory than the actual allocable space (including physical memory and Swap). This feature is called OverCommit.
This feature is also configurable in the Linux operating system. You can adjust the OverCommit policy by setting/proc/sys/overcommit_memory to different values.
Overcommit_memory can have three values:
0: default value. The Linux kernel uses some heuristic algorithms to determine the oversold and oversold sizes. Generally, slight oversold is allowed, some obviously impossible requests are rejected, and some rule restrictions are imposed, for example, the size of overcommit varies with users.
1: Yes, no restrictions on oversales, of course, this is not infinite, but also limited by addressing space, the maximum 32-bit system may be 4 GB, 64-bit system about 16 TB.
2: No. No oversales is allowed. The system can allocate no more memory than swap + actual physical memory * overcommit_ratio. This value can be set through/proc/sys/vm/overcommit_ratio. The default value is 50%.

To verify the Linux memory allocation, we use a small program to test it:

#include <stdio.h>#include <stdlib.h>#define MEGABYTE 1024*1024int main(int argc, char *argv[]){    void *myblock = NULL;    int count = 0;    while (1)    {        myblock = (void *) malloc(MEGABYTE);        if (!myblock) break;        printf("Currently allocating %d MB\n", ++count);    }        exit(0);}#include <stdio.h>#include <stdlib.h>#define MEGABYTE 1024*1024int main(int argc, char *argv[]){    void *myblock = NULL;    int count = 0;    while(1)    {        myblock = (void *) malloc(MEGABYTE);        if (!myblock) break;        memset(myblock,1, MEGABYTE);        printf("Currently allocating %d MB\n",++count);    }    exit(0);    }

After applying for memory space through malloc (), the former does not use it immediately, while the latter uses 1 to fill the space after each application. Let's take a look at the results of the two programs running.

This is the result of running on 1g RAM, 400 M Swap virtual machine. The former applied for a space far beyond the actual memory, and the latter did not exceed the actual memory available space. This verifies the Linux memory allocation policy described above.
This is a system optimization, and it is understandable. However, we know that "oversold" is based on the assumption that there will not be a large number of programs using resources at the same time, which is obviously risky. Therefore, Linux uses an Out Of Memory Killer mechanism. Before the available system Memory (including Swap) is used up, choose to Kill some processes to release some memory. In the next chapter, we will focus on the mechanism of Linux OOM Killer.

Related Article

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.