Resident and Virtual memory on Linux: A short example
来源:互联网 发布:会员积分软件下载 编辑:程序博客网 时间:2024/06/03 02:26
文章出处:http://www.darkcoding.net/software/resident-and-virtual-memory-on-linux-a-short-example/
Tools like top
show processes using two kinds of memory:
- Resident memory, labelled RES: How much physical memory, how much RAM, your process is using.RES is the important number.
- Virtual memory, labelled VIRT: How much memory your process thinks it’s using. Usually much bigger than RES, thanks to the Linux kernel’s clever memory management.
Here’s a short C program to illustrate the difference:
#include <stdio.h>#include <stdlib.h>void fill(unsigned char* addr, size_t amount) { unsigned long i; for (i = 0; i < amount; i++) { *(addr + i) = 42; }}int main(int argc, char **argv) { unsigned char *result; char input; size_t s = 1<<30; result = malloc(s); printf("Addr: %p\n", result); //fill(result, s); scanf("%c", &input); return 0;}
Save it as mem.c
, compile it cc -Wall -g mem.c -o mem
and run it.
It requests 1 Gig of memory (1 << 30), but doesn’t use it. With top
(orhtop
, which is much nicer), or even ps -Ao rsz,vsz,cmd | grep mem
view it’s memory usage. The resident size should be quite small (~280k for me), but the virtual size will be the full 1 Gig we requested.
Uncomment the fill
line, compile and run it again. You should see the resident size get much bigger, as we’re actually using the memory we requested (we’re filling it with the number 42). The kernel has to give us real memory to work with.
We can request far more virtual memory than the machine can handle. Here’s a screenshot where I’ve requested 16 Gig of memory, on a machine with only 3 Gig of RAM and 3 Gig of swap. The VIRT’s sum up to way more than that machine can handle. At the top left of the screenshot you can see none of that memory has really been allocated. The kernel won’t allocate it until we use it.
In summary, virtual size can largely be ignored. Resident size is the important number.
- Resident and Virtual memory on Linux: A short example
- 一个java程序的virtual memory 和 resident memory
- Linux Memory Management – Virtual Memory and Demand Paging
- ICDE2013论文翻译草稿:CPU and Cache Efficient Management of Memory-Resident Databases
- Sample Code on Linux semaphore and shared memory
- Shared memory on Linux(difference posix and systemv )
- 常驻程序 terminate and stay resident
- A. Memory and Crow
- Debugging Memory on Linux
- Standby and suspend on memory
- Understanding virtual memory and dynamic allocation
- Memory Layout for Multiple and Virtual Inheritance
- Memory Layout for Multiple and Virtual Inheritance
- Android Studio and NDK Integration on Windows Step by Step with a Hello JNI example
- Introducing deep learning and long-short term memory networks
- [windows_internals]Memory Layout (Virtual address space of a C process) under Linux OS
- codeforces A. Memory and Crow
- Understanding The Linux Virtual Memory Manager
- NodeJS优缺点及适用场景讨论
- 排序算法(一):插入排序、冒泡排序、合并排序、选择排序
- hud1495 非常可乐(bfs)
- [codevs 1232] 飞行员配对方案问题
- Java创建File失败
- Resident and Virtual memory on Linux: A short example
- 在项目中,控制权限保存时,如果多次修改权限,该如何写?
- 12.6 实现选项的计算表达式
- c++primer笔记1
- U-boot、Kernel、文件系统的几种下载方式总结(基于TQ2440开发板)
- Ubuntu下搭建web服务器
- 最近研究xcodebuild批量打包的一些心得
- dynamic-insert,dynamic-update
- [每天一个知识点]21-Java语言-MultiCatch