- 浏览: 482084 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
alvin198761:
renzhengzhi 写道我参与过12306余票查询系统的开 ...
别给12306 辩解了 -
renzhengzhi:
我参与过12306余票查询系统的开发,用户请求被前面3层缓存拦 ...
别给12306 辩解了 -
renzhengzhi:
写的很好。
JAVA线程dump的分析 -
liyonghui160com:
说好的附件呢
分布式服务框架 Zookeeper -- 管理分布式环境中的数据 -
ghpaas:
orbeon作为xforms标准的实现,不论其设计器还是运行时 ...
XForms 1.1 中文翻译—第1章 关于XForms标准
Memory usage analysis
System-wide memory analysis
Top/Free
These two command-line tools are the two most well-known and (especially with the default columns shown) very useless and misleading.
Free
In free the used swap shows the amount of used swap and used -/+ buffers/cache is supposed to show the amount of really used RAM. The latter is computed as all memory used minus various caches and buffers, however since sizes reported for caches and buffers don't reflect reality very well (they include actually really used memory), the numbers are useless for anything but getting a very rough picture.
Top
The totals in top are the same case like with free. The values reported for processes that are shown by default are not of much use either - their meaning is unclear or they are of no practical use. It is possible to change the columns shown using the f key to get some more useful values. However top is still only useful for rough analysis as it doesn't provide any details and does not account at all for sharing memory between processes.
Explanation of some of the columns (some of which may be wrong though because of their confusing meaning and values):
- VIRT - the size of allocated address space. Not memory, but address space. This value in practice means next to nothing. When a process requests a large memory block from the system but uses only a small part of it, real usage will be low, VIRT will be high.
- RES - resident memory size, i.e. the memory that the process uses in RAM. While this value has some practical value it is not that useful because it includes all memory this process possibly shares with other processes (e.g. KDE applications share large KDE libraries and the size of these libraries is includes in RES of every such application). Swap is not included which additionally decreases the usefulness of this value.
- SHR - the amount of memory that could be potentially shared with other processes. The meaning is somewhat unclear and this value is of little practical value and it is not useful on its own.
- SWAP - the amount of swapped out memory - not only memory moved to the swap but also for example memory-mapped files, which again makes this value not very useful.
- CODE - memory taken by executable code. Various tables used by the code (e.g. virtual tables) don't seem to be included though - not very useful.
- DATA - it clearly does not mean what the manual page says and the actual meaning of the value is unclear.
See the manual page for top for more details. Note however that the manual page (as of procps version 2.3.7 and Linux version 2.6.18) is either obsolete or appears to be wrong in description of some of the fields (e.g. DATA).
/proc
Various files in the /proc filesystem can provide information about memory. However they're are poorly documented (linux/Documentation/filesystems/proc.txt) and many of them are broken (and since top and free get their values the same way, they are equally broken). In /proc/PID/ there are several files:
- status - human-readable data about the process, includes some data shown by top
- stat/statm - similar, not in human-readable format
- maps - lists memory mappings of the process (TODO if somebody feels like documenting, feel free to do so)
- smaps - lists memory mappings of the process with more details (TODO if somebody feels like documenting, feel free to do so)
Exmap
Exmap (http://www.berthels.co.uk/exmap/) is probably the best currently available tool for system-wide memory analysis. Advantages include detailed information about memory, accounting for shared memory and, last but not least, reporting values that make sense. Since exmap is a relatively new tool it's not in very wide-spread use, so you will possibly have to download and build it yourself (it requires gtkmm and boost libraries). At http://ktown.kde.org/~seli/download/exmap/ is my lousy attempt at SUSE packages (unsupported).
Exmap has documentation that explains its use and the meaning of the various values. In short:
- Run exmap. It may be better to run exmap as root in order to get access to all memory.
- The 'Processes' tab shows memory information about each process in the first listview. The values are:
- VM - VIRT from top - not really useful.
- Mapped size - the total of memory actually used by the process, both in RAM and swap. Note however that e.g. libraries are not swapped out to swap but simply discarded (and read again from the files if needed). This value includes even shared memory.
- Resident size - mapped size, but only in RAM (without swap).
- Sole mapped - memory used only by this process. For example if a process uses a shared library that no other process uses at the moment it is included here.
- Writable - memory with the processes' private data. It is part of sole mapped that the process has already written to.
- Effective - the effective values are an attempt to compute how much memory a process uses in practice. Effective mapped/resident are mapped/resident values adjusted for shared memory. Memory that is shared by more processes is equally divided among them, i.e. if 10 processes use a 10MB large shared library (and each of them really uses the whole library), the library is counted as 10MB in mapped/resident values but only as 1MB in effective values.
- The second listview shows memory mappings for the selected process. There are memory-mapped files and binaries and also several special mappings:
- [heap] - dynamically allocated memory (i.e. malloc etc.)
- [stack] - the stack of the process
- [anon] - anonymous mapping from the mmap() system call - they should usually be viewed the same like [heap] although with multithreaded applications some [anon] mappings may be another [stack] mappings
- The remaining two listviews provide details about the mappings. For experts (see exmap documentation).
Exmap in practice: Run exmap (possibly as root, if you get messages about failures to open files and you need them). Sort processes by effective mapped size. Higher values are worse. Use the second listview to find out which file is possibly responsible or if the high memory usage comes from the process data ([heap], [anon] and [stack]). It usually cannot be changed which libraries are used, so the values that should be actually checked are writable and sole mapped columns - they are the memory actually used by the application itself (although a portion may come from calling library functions of course).
X resources analysis
Applications using the X window system allocate some resources (such as pixmaps and windows) in the X server process and refer to them only using their handles (ID numbers). The memory for these resources is allocated in the X server process. The xrestop tool shows resource usage of applications in a way similar to top (without being seriously broken). The two most important columns are 'Pxms mem', which is memory taken by pixmaps (QPixmap), and 'Other', which is memory taken by other resources (should be usually low, high value may indicate a leak in code using directly Xlib, possibly in some library). TODO: there should be a tool to help detect pixmap leaks
Application memory analysis
Intro
Applications use memory in several ways that can be seen in exmap. There is memory taken by binaries, there are data segments from binaries (such as global variables but also e.g. relocation tables) and there is dynamic memory used. Dynamic memory is stack and mainly heap. Heap is part of memory for allocations using malloc(), operator new and similar functions.
There are two ways memory is allocated in the heap, as far as memory usage is concerned. There is one main heap area (shown as [heap] in exmap) that has one end fixed and other end is moved using the brk() system call. So when an allocation needs to be done a part of this area is reserved (recorded in heap internal structures) and its address is returned. If there's no more contiguous free space available the area is enlarged by moving the upper end using the brk() call. This has several consequences:
- Every allocation has a certain overhead - when allocation many very small blocks the overhead caused by heap internal structures can significantly increase the actual memory usage. Various additional implementations of memory allocation like pools, arenas or obstacks can be used to reduce this problem (they use a special area of memory and the whole area must be freed at once, so there's no need to keep track of each allocation).
- Memory can get easily fragmented - allocating blocks of different sizes can lead to heap having holes that are not big enough for following allocations. Heap implementations try to group small sizes together to reduce this problem and again additional memory allocation implementations like obstacks can reduce this problem as well (they use special area of memory that is freed at once so it cannot fragment the heap).
- The main heap area is one contiguous memory area that cannot be split into smaller parts - when a lot of memory is allocated, then one permanent allocation is made and previous allocations are freed, the permanent allocation keeps the movable end fixed and prevents shrinking (TODO: does malloc actually ever use brk() to shrink?). This problem again can be reduced by using a special allocation implementation with its own memory area. Heap implementations also try to reduce this problem by allocating separate memory for large allocations (usually 128KB and larger) using mmap() system call instead of brk() - these show as [anon] in exmap.
Memprof
Memprof (http://www.gnome.org/projects/memprof/) tracks malloc() calls and can therefore be used for analysing heap usage. Application needs to be launched using memprof (pay attention avoid forking using --nofork or similar) and they should have debug information available to get full details. Top bar shows memory usage (yellow - used, blue - peak usage, red - leaks, after pressing the Leaks), number of allocations (at the moment, does not include already freed memory) and bytes allocated (heap overhead is not included). After pressing the Profile button it can be examined where the allocations come from.
Memprof in practice: Compile application with debug info, run it using memprof. Press the Profile button when you want memory analysis. In the listbox select the item with the maximum total (should be __libc_start_main or main), in the listview on the right there will be a tree of allocations from this function and the functions called from it. Open the tree, follow the high values, find problems.
kmtrace
Kmtrace (from kdesdk/kmtrace in KDE SVN) has similar purpose to memprof but with different usage and different way of presenting results (the output format is text file and the usage needs more manual intervention).
Installation (e.g. to /tmp/kmtrace):
svn co svn://anonsvn.kde.org/home/kde/branches/KDE/3.5/kdesdk -N cd kdesdk svn co svn://anonsvn.kde.org/home/kde/branches/KDE/3.5/kdesdk/kmtrace svn co svn://anonsvn.kde.org/home/kde/branches/KDE/3.5/kde-common/admin make -f Makefile.cvs ./configure --prefix=/tmp/kmtrace make && make install
Usage:
LD_PRELOAD=/tmp/kmtrace/lib/libktrace.so MALLOC_TRACE=kmtrace.out xterm /tmp/kmtrace/bin/kmtrace kmtrace.out --tree kmtrace.tree >kmtrace.txtStarting and finishing of tracking is done using functions ktrace() and kuntrace() from libktrace.so, so it is possible to finish tracking even before the application exits by calling kuntrace() manually (TODO kmtrace could be perhaps hacked to react on a signal or something):
gdb attach [pid] call kutrace() quit
File kmtrace.out is an internal file that needs to be post-processed and can be deleted afterwards. File kmtrace.txtcontains summary information and all allocations sorted by size - they're actually referred to as leaks, since tracking usually is finished after program exit, but when stopping tracking while the application is still running they represent all currently existing allocations. File kmtrace.tree provides all the allocations in a tree (first number is number of bytes, second is number of allocations). It is possible to limit the depth of the tree and to ignore subtrees with small sizes, see 'kmtrace --help'.
Valgrind
Valgrind's skin Massif ('valgrind --tool=massif application') traces memory usage of the application. After it exits it generates a .ps file showing allocations progress for main places and a text file with details (TODO: the text file does not seem very usable).
Valgrind can also detect memory leaks ('valgrind --tool=memcheck --leak-check=yes application', additionally also '--show-reachable=yes' may be used to show memory still allocated at application exit even if it's not unreachable). After application exit valgrind will print out backtrace of all leaked allocations (use '--num-callers=50' to get deeper backtraces).
HOWTO (AKA right to the point)
If you want to analyse memory usage of the whole system or to thoroughly analyse memory usage of one application (not just its heap usage), use exmap. For whole system analysis, find processes with the highest effective usage, they take the most memory in practice, find processes with the highest writable usage, they create the most data (and therefore possibly leak or are very ineffective in their data usage). Select such application and analyse its mappings in the second listview. See exmap section for more details. Also use xrestop to check high usage of X resources, especially if the process of the X server takes a lot of memory. See xrestop section for details.
If you want to detect leaks, use valgrind or possibly kmtrace (TODO memprof doesn't work for leaks for me). See their sections for more details.
If you want to analyse heap (malloc etc.) usage of an application, either run it in memprof or with kmtrace, profile the application and search the function call tree for biggest allocations. See their sections for more details.
http://ktown.kde.org/~seli/memory/analysis.html
发表评论
-
使用 RPM 打包软件,第 1 部分: 构建和分发包
2012-03-26 10:31 1271顾名思义,开源软件 ... -
用 RPM 打包软件,第 3 部分
2012-03-26 10:30 1162安装和卸载脚本的工作原理 安装和卸载脚本看起来很简单, ... -
用 RPM 打包软件,第 2 部分
2012-03-26 10:28 1166不作为 root 用户来构建 RPM 包 正如您在第 1 ... -
用 RPM 打包软件,第 1 部分
2012-03-26 10:23 980RPM(Red Hat Package Manager)是用于 ... -
Linux: How to measure actual memory usage of an application or process?
2010-09-03 23:31 1278http://stackoverflow.com/questi ... -
HowTo: Profile Memory in a Linux System
2010-09-03 22:56 1183HOWTO: Profile Memory in a Li ... -
Linux内存管理机制
2010-09-03 22:48 2096内存是Linux内核所管理的最重要的资源之一,内存管理 ... -
linux内存管理概述
2010-09-03 22:44 2457Linux中的地址空间(一)有这么一系列的问题,是否在困扰 ... -
linux上buffer和cache的区别
2010-09-03 15:14 1523free free 命令相对于top 提供了更简洁的查看系统 ... -
linux下top命令参数解释
2010-09-03 14:56 884top命令是Linux下常用的性能分析工具,能够实时显示系统中 ... -
smem memory reporting tool
2010-08-25 15:41 972smem is a tool that can give ... -
Linux进程虚拟内存和物理内存
2010-08-25 15:39 5070先介绍几个基本概念: SIZE: 进程使用的 ... -
Memory: VSS/RSS/PSS/USS
2010-08-25 13:54 1539Terms VSS - Virtual Set ... -
Linux 内核的文件 Cache 管理机制介绍
2010-08-18 18:21 10601 前言 自从诞生以来,Linux 就被不断完善和普及 ... -
运行时: 块内存复制,第 2 部分
2010-08-06 12:33 1249我的 前一专栏专注于 ... -
RunTime: 块内存复制
2010-08-06 12:32 1183内存复制 在计算机中,内存复制经常而普遍。它们出现在联网 ... -
内存详解
2010-08-06 11:34 956文档选项 ... -
Linux slab 分配器剖析
2010-08-06 11:32 1326良好的操作系统性能部分依赖于操作系统有效管理资源的能力。在 ... -
降低 Linux 内存开销
2010-08-06 11:30 1100Linux 广受追捧的一个优点是它比 Microsoft® ... -
在 Linux 平台中调试 C/C++ 内存泄漏方法
2010-08-06 11:29 1836由于 C 和 C++ 程序中完全由程序员自主申请和释放内存 ...
相关推荐
Finally, you will be walked through complex userspace memory infection analysis. This book will lead you into territory that is uncharted even by some experts; right into the world of the computer ...
A Compressed Suffix Tree Based Implementation With Low Peak MemoryUsage 1Daniel Saad Nogueira Nunes2 Mauricio Ayala-Rincón3Instituto de Ciências Exatas Departamentos de Ciência da Computação...
Finally, you will be walked through complex userspace memory infection analysis. This book will lead you into territory that is uncharted even by some experts; right into the world of the computer ...
Finally, you will be walked through complex userspace memory infection analysis., This book will lead you into territory that is uncharted even by some experts; right into the world of the computer ...
- Flyweight pattern reduces memory usage by sharing common objects in a large number of similar objects. - Proxy pattern provides a surrogate or placeholder for another object to control access, ...
Describe how configuration options affect memory usage. Describe the effect on the I/O subsystem when memory runs low. List at least two memory myths and why they are not true. Recommended ...
It provides developers with a detailed analysis of their Java programs' performance, enabling them to identify bottlenecks, optimize memory usage, and enhance overall application efficiency....
Graph-tool is an efficient Python module for manipulation and ... This confers it a level of performance that is comparable (both in memory usage and computation time) to that of a pure C/C++ library.
Usage: icc8051 {<options>} <sourcefile> {<options>} Sourcefile: 'C' source file with default extension: .c Environment: QCC8051 Options (specified order is of no importance): -o file Put object on: ...
此外,还可以考虑在分析过程中使用内存分析工具,如Redis的`MEMORY USAGE`命令,以更精确地获取单个键的内存占用。 总结来说,通过Key前缀分析Redis内存占用并导出结果到CSV文件,是一个涉及Redis内存管理、PHP编程...
4. **Big-Oh Notation**: Big-Oh notation is a mathematical notation used to describe the upper bound of an algorithm’s running time or space usage. It provides a way to express the worst-case scenario...
More tool coverage, including expanded usage of Visual Studio More benchmarking New GC configuration options Code warmup techniques New .NET features such as ref-returns, value tuples, SIMD, and more ...
- **A Quick Tip on the Execution and Memory Analysis of an Assembly in Visual Studio 2015**: Using tools like the Visual Studio Profiler to analyze the memory usage and performance of .NET ...
- **内存使用优化(Memory Usage)**: 减少了内存占用,提升了系统在大规模索引时的稳定性。 4. **示例应用** `demo`目录下的代码展示了如何使用Lucene进行基本的索引创建、搜索和分析操作。开发者可以通过这些...
Analyzing processor and memory usage with Instruments Integrating with Mavericks Server’s sleek continuous integration system Register your book at www.informit.com/register for access to this title...
- **Memory Usage Statistics**:跟踪每个事务执行时所使用的内存资源。 - **RFC Profiles**:分析远程函数调用(RFC)产生的系统负载,按事务、显示模式或目标端口分类。 - **User Profile**:显示每个用户执行...
###### 3.2.4 Real-Time JTAG and Analysis - **Description**: Provides debugging capabilities through a JTAG interface. - **Functionality**: Enables developers to monitor and control the execution of ...