Linux memory leaks

Source: Internet
Author: User

1.1 What is a memory 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.
1.2 In the way that occurs, memory leaks can be categorized into 4 categories:

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

2). Accidental memory leaks. 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.

3). Disposable memory leaks. The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a piece of memory that 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.

4). An implicit memory leak. 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. But for a server program that needs to run for days, weeks, or months, not releasing memory in time can also 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.
1.3 Linux Query Memory usage
The top command is a common performance analysis tool under Linux that shows the resource usage of individual processes in the system in real time, similar to the Task Manager for Windows.
You can view the contents of the%mem directly after you use the top command. You can choose to view by process or by user, if you want to see the process memory usage of the Oracle user, you can use the following command:
$ top-u Oracle
Content Explanation:

PID: ID of the process
USER: Process Owner
PR: The priority level of the process, the smaller the higher the priority is executed
Ninice: Value
VIRT: Virtual memory consumed by the process
RES: The physical memory occupied by the process
SHR: Shared memory used by the process
S: The state of the process. s for hibernation, R for running, Z for Zombie, n for the process priority value is negative
%CPU: Process Consuming CPU utilization
%MEM: The percentage of physical memory and total memory used by the process
Time+: The total CPU time that is consumed after the process is started, which is the cumulative value of the CPU usage time.
Command: Process start name

$ pmap-d PID
Depending on the process, you can view the memory used by the process-related information (the process number can be viewed by PS) as follows:
$ pmap-d 5647

$ free-m
Total used free shared buffers Cached

MEM:1002 769 232 0 62 421

-/+ buffers/cache:286 715

swap:1153 0 1153
Part One MEM line: Total Memory: 1002
Mused number of memory used: 769
Mfree Free Memory: 232
Mshared is currently obsolete. No, always 0.
Buffers Buffer Cache memory Number: 62
mcached Page Cache Memory: 421M
Relationship: Total (1002M) = Used (769M) + Free (232M)

Part II (-/+ Buffers/cache):
(-buffers/cache) Used memory: 286M (refers to the first part of the Mem line in the used-buffers-cached)
(+buffers/cache) Free memory: 715M (refers to the first part of the Mem line of Free + buffers + cached)
The visible-buffers/cache reflects the memory that is actually eaten by the program, and +buffers/cache reflects the total amount of memory that can be appropriated.

$ cat/proc/meminfo

1.4 Cache Memory

When you read and write files, the Linux kernel caches the files in memory in order to improve read and write performance and speed, which is the memory (cache memory). The Cache memory will not be released automatically even after your program has finished running. This will cause you to read and write files frequently in your Linux system, and you will find that there is little physical memory available.

In fact, this cache memory is automatically released when you need to use memory, so you don't have to worry about not having memory available. If you want to release the cache memory manually, there is a way.

How do I release cache memory?

Use the following command to release the cache Memory:

To free Pagecache:
Echo 1 >/proc/sys/vm/drop_caches
To free dentries and inodes:
Echo 2 >/proc/sys/vm/drop_caches
To free Pagecache, dentries and Inodes:
Echo 3 >/proc/sys/vm/drop_caches

Note that it is best to sync before releasing to prevent data loss.

Summary: Personal experience that there is no need to manually release, this memory management method is also better than win one of the places! Because of the kernel memory management mechanism of Linux, it is generally not necessary to deliberately release the cache that is already in use. These cache contents can improve the read and write speed of files and disks.

Linux memory leaks

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.