Linux Memory Peep--free

Source: Internet
Author: User

The free command is a shell command that is commonly used to view memory in Linux systems and performs the following effects:

[Email protected]_24fd52f24e00:/# free

Total used free shared buffers

mem:61056 34296 26760 0 3776

-/+ buffers:30520 30536

swap:0 0 0

[Email protected]_24fd52f24e00:/#

(The base unit of this output is KB)


Free This result is generated by calling the SysInfo () system call function to get the corresponding results, the results are tracked in the 2.6.36 kernel as follows:


[KERNEL/TIMER.C]

Syscall_define1 (sysinfo, struct sysinfo __user *, info)

{

struct SysInfo val;


Do_sysinfo (&val);


if (Copy_to_user (info, &val, sizeof (struct sysinfo)))

Return-efault;


return 0;

}

The SysInfo () system call ultimately gives the kernel a statistical value from the above function.


Where the implementation of the Do_sysinfo () function is also in the same file, the part of the definition is as follows:

[Kernel/timer.c:sysinfo ()->do_sysinfo ()]

/**

* Do_sysinfo-fill in SysInfo struct

* @info: Pointer to buffer to fill

*/

int do_sysinfo (struct sysinfo *info)

{

...

Si_meminfo (info);

...

Bitcount = 0;

Mem_unit = info->mem_unit;

while (Mem_unit > 1) {

bitcount++;

Mem_unit >>= 1;

Sav_total = Mem_total;

Mem_total <<= 1;

if (Mem_total < sav_total)

Goto out;

}


/*

* If Mem_total did not overflow, multiply all memory values by

* Info->mem_unit and set it to 1. This leaves things compatible

* with 2.2.x, and also retains compatibility with earlier 2.4.x

* Kernels ...

*/


Info->mem_unit = 1;

Info->totalram <<= Bitcount;

Info->freeram <<= Bitcount;

Info->sharedram <<= Bitcount;

Info->bufferram <<= Bitcount;

Info->totalswap <<= Bitcount;

Info->freeswap <<= Bitcount;

Info->totalhigh <<= Bitcount;

Info->freehigh <<= Bitcount;


Out

return 0;

}

where Do_sysinfo () calls Si_meminfo () gets vm_stat[] The total amount of unused memory currently used, obtained through the Global_page_state () function:

[Mm/page_alloc.c:sysinfo ()->do_sysinfo ()->si_meminfo ()]

void Si_meminfo (struct sysinfo *val)

{

Val->totalram = totalram_pages;

Val->sharedram = 0;

Val->freeram = Global_page_state (nr_free_pages);

Val->bufferram = Nr_blockdev_pages ();

Val->totalhigh = totalhigh_pages;

Val->freehigh = Nr_free_highpages ();

Val->mem_unit = page_size;

}

The name of the Global_page_state () function can be guessed, vm_stat the basic unit of memory management for the page, here is the size of 4KB memory.


The source of the value of the Freeram is determined, and it can be known that the basic unit of the output of the free command is 4KB, which means that free has a maximum of 4KB errors. At this point, if the memory leak of the check program, the leakage of memory is far less than 4KB, it may take a certain amount of time to accumulate in order to know the remaining internal loss, because the system is generally running, and the Linux kernel will use memory as much as possible to improve the efficiency of the system execution, so, In order to get a clear evidence of a smaller memory leak program, the time will increase a lot and, ultimately, it should be a phenomenon where the statistical value of the remaining memory of the free result is less volatile.


This article is from the "Scott Huang" blog, so be sure to keep this source http://scottgh.blog.51cto.com/406525/1709772

Linux Memory Peep--free

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.