Measuring memory in Linux (the basics)
2011-11-11 14:44
483 查看
ReferenceLink:http://elinux.org/Runtime_Memory_Measurement
HerearesomebasictechniquesformeasuringmemoryusageinLinux.
Thisinformationisobtainedfrom/proc/meminfo,whichhasadditionaldetailsnotshownbythe'free'command.
Thefollowingisonmymachinewith512MbRAM,runningLinux2.6.3:
Seehttp://lwn.net/Articles/28345/foradescriptionofthesefields
Meaningofpsfields:
·%Mem-percentofmemory
·VSZ-VirtualSize
·RSS-ResidentSetSize
·SIZE-EquivalenttoVSZ
·%MEM--Memoryusage(RES)
§Atask'scurrentlyusedshareofavailablephysicalmemory.
·VIRT--VirtualImage(kb)
§Thetotalamountofvirtualmemoryusedbythetask.Itincludesallcode,dataandsharedlibrariespluspagesthathavebeenswappedout.
§VIRT=SWAP+RES
·SWAP--Swappedsize(kb)
§Theswappedoutportionofatask'stotalvirtualmemoryimage.
·RES--Residentsize(kb)
§Thenon-swappedphysicalmemoryataskhasused.
§RES=CODE+DATA.
·CODE--Codesize(kb)
§Theamountofphysicalmemorydevotedtoexecutablecode,alsoknownasthe'textresidentset'sizeorTRS
·DATA--Data+Stacksize(kb)
§Theamountofphysicalmemorydevotedtootherthanexecutablecode,alsoknownasthe'dataresidentset'sizeorDRS.
·SHR--SharedMemsize(kb)
§Theamountofsharedmemoryusedbyatask.Itsimplyreflectsmemorythatcouldbepotentiallysharedwithotherprocesses.
·nDRT--DirtyPagescount
§Thenumberofpagesthathavebeenmodifiedsincetheywerelastwrittentodisk.Dirtypagesmustbewrittentodiskbeforethecorrespondingphysicalmemorylocationcanbeusedforsomeothervirtualpage.
Hereanexample:69340658615805350
·VmSize:2772kB
·VmLck:0kB-???
·VmRSS:1624kB
·VmData:404kB
·VmStk:24kB
·VmExe:608kB
·VmLib:1440kB
Example:
mem_usagecommandtoconsolidatedata
DavidSchleefwroteaprogramtoconsolidatetheinformationfrom/proc/<pid>/maps,andtotalupeachkindofmemoryforaprocess.
Hereitis:
Media:mem_usage(Itwasobtainedfromhttp://www.schleef.org/~ds/mem_usage)
Hereistheresultofrunningmem_usageontheprocessusedinthepreviousexample:
accuratememoryinformation,see
AccurateMemoryMeasurement
"copy-on-write"pages-anmmap'edfilemaybeverylargeintheprocessaddressspace,butemptyuntilwrittento.
FromRatboyonSlashdot:
FromothersonSlashdot:
Topwillshowyouthesameaspsdoes,psreads/proc/<pid>/statmandaskswhat'sgoingon.Theproblemonlinuxisthecopyonwriteprinciplewhichsavesheapsofmemory,butmakesitvirtuallyimpossibletofigureoutwhatbelongstowhat.Thethingis,
whenyouforkitmapsthememoryandmarkseverythingascopyonwrite,whensomethingneedstowritetopartofthememory,thenitwillmakethecopyforeachprocess.
Howeveraskingtheprocesshowmuchmemoryithasallocatedwillshowallmemoryincludingstuffthatismarkedcopyonwrite-thatis,Icouldhave100processesshowingtheyeachuse1.4MBofmemory,becausetheyallsharethesamelibrary,butinfact,
itsthesamecopytheyareallusingsoI'monlyusing1.4MBinsteadof140MB(+PCBet.al)
Eachthreadinaprocessshowsupasconsumingthesameamountofmemory(eitherthisonlyhappensunderLinuxthreadsorIdon'thaveanythreadedapplicationsrunningonmysystem).
Devicemappingsshowupasconsumedmemory(whichgeneratesplentyofcomplaintsabouttheXserver).IfyouwanttofindouthowmuchmemoryXisactuallyusing(bytesincachedpixmapsonbehalfofeachprocessandsansdevicemappings),trytheprogramhere:
http://69.142.116.122/dist/pixmap_mem-1.0.tgz
ThiscontainsatinyprogramthatlistshowmuchmemoryXisusingforotherprogramsbycachingpixmapsandaperlscriptthatlistshowmuchmemoryXisusingsansdevicemappings.}}}
pmapisautilitywhichshowsthememoryusageofaprocess(itlookslikeitjustreadsandinterprets/proc/<pid>/maps).
SomeoneonSlashdotsaid:
Heapisthedynamicallyallocatedmemoryinsideeachprocess'addressspacethatismanagedbytheapplicationitself.ThestructureofthismemoryisactuallymanagedbytheClibrary,withtheapplicationcallingmalloc()andfree().
glibchasthecapabilitytocollectstatisticsinformationofheapfunctionslikemalloc()andotherfunctionslikememoryleakcheckingordoublefree.
MemoryDebuggersforalistofdifferenttoolsandtheirfeatures.
KernelFunctionTrace)
o
Thisnewfeaturehasnotyetbeenpublished
Recent-mmtreeaddedstack-corruption-detector.patch(8thMarch,2006)
Thescripts/checkstack.plscriptinthekerneltreewillshowthefunctionswiththelargeststaticstackfootprint.
EnablingCONFIG_DEBUG_STACKOVERFLOWinthekernelwillenablecheckingforlow-stacksituationsintheirqhandler.
EnablingCONFIG_4KSTACKSwillcausestackoverflowstooccurmorefrequently,particularlywithcertainkernelcodesuchasXFSenabled.
http://www.halobates.de/memorywaste.pdf-GreatpaperbyAndiKleen,ofSUSELabs,aboutdynamicmemoryusageofLinuxsystems
Check/proc/slabinfotofindhowmuchmemoryisbeingusedbythekernelSLABallocator(orSLUBorSLOB,dependingonwhatisenabled).
HerearesomebasictechniquesformeasuringmemoryusageinLinux.
'free'and/proc
The'free'commandshowsthememoryonamachine,incertaincategories.$free
totalusedfreesharedbufferscached
Mem:50756448156026004068888185220
-/+buffers/cache:227452280112
Swap:21366041051682031436
Thisinformationisobtainedfrom/proc/meminfo,whichhasadditionaldetailsnotshownbythe'free'command.
Thefollowingisonmymachinewith512MbRAM,runningLinux2.6.3:
$cat/proc/meminfo
MemTotal:507564kB
MemFree:26004kB
Buffers:68888kB
Cached:185220kB
SwapCached:29348kB
Active:342488kB
Inactive:32092kB
HighTotal:0kB
HighFree:0kB
LowTotal:507564kB
LowFree:26004kB
SwapTotal:2136604kB
SwapFree:2031436kB
Dirty:88kB
Writeback:0kB
Mapped:165648kB
Slab:73212kB
Committed_AS:343172kB
PageTables:2644kB
VmallocTotal:524212kB
VmallocUsed:7692kB
VmallocChunk:516328kB
See
Measuringuserprocessmemoryuse
'ps'fieldsformemoryinformation
The'ps'commandprovidesinformationaboutthememoryusageofprocessesonaLinuxsystem.However,itisnotwelldocumented.Herearesomenotesonusing'ps'and/proctoviewmemoryusageinformationonarunningLinuxsystem:Meaningofpsfields:
·%Mem-percentofmemory
·VSZ-VirtualSize
·RSS-ResidentSetSize
·SIZE-EquivalenttoVSZ
'top'fieldsformemoryinformation
See'mantop':·%MEM--Memoryusage(RES)
§Atask'scurrentlyusedshareofavailablephysicalmemory.
·VIRT--VirtualImage(kb)
§Thetotalamountofvirtualmemoryusedbythetask.Itincludesallcode,dataandsharedlibrariespluspagesthathavebeenswappedout.
§VIRT=SWAP+RES
·SWAP--Swappedsize(kb)
§Theswappedoutportionofatask'stotalvirtualmemoryimage.
·RES--Residentsize(kb)
§Thenon-swappedphysicalmemoryataskhasused.
§RES=CODE+DATA.
·CODE--Codesize(kb)
§Theamountofphysicalmemorydevotedtoexecutablecode,alsoknownasthe'textresidentset'sizeorTRS
·DATA--Data+Stacksize(kb)
§Theamountofphysicalmemorydevotedtootherthanexecutablecode,alsoknownasthe'dataresidentset'sizeorDRS.
·SHR--SharedMemsize(kb)
§Theamountofsharedmemoryusedbyatask.Itsimplyreflectsmemorythatcouldbepotentiallysharedwithotherprocesses.
·nDRT--DirtyPagescount
§Thenumberofpagesthathavebeenmodifiedsincetheywerelastwrittentodisk.Dirtypagesmustbewrittentodiskbeforethecorrespondingphysicalmemorylocationcanbeusedforsomeothervirtualpage.
/procinfo
see'manproc'fordetailedinformationaboutthefilesandfieldsinthe/procfilesystem./proc/<pid>/statm
/proc/<pid>/statmfields:columnsare(inpages):totalprogramsize| |
residentsetsize| |
sharedpages| |
text(code)| |
data/stack| |
library| |
dirtypages| |
/proc/<pid>/status
/proc/<pid>/statusfields:·VmSize:2772kB
·VmLck:0kB-???
·VmRSS:1624kB
·VmData:404kB
·VmStk:24kB
·VmExe:608kB
·VmLib:1440kB
/proc/<pid>/maps
Theprocessmapsshowstheactualmemoryareasthathavebeenmappedintoaprocess'addressspace,andtheirpermissions.Example:
$cat/proc/25042/maps
08048000-080e0000r-xp0000000003:05196610/bin/bash
080e0000-080e6000rw-p0009700003:05196610/bin/bash
080e6000-08148000rwxp0000000000:000
40000000-40016000r-xp0000000003:05147471/lib/ld-2.3.3.so
40016000-40017000rw-p0001500003:05147471/lib/ld-2.3.3.so
40017000-40018000rw-p0000000000:000
40018000-40019000r--p0000000003:05184090/usr/share/locale/en_US/LC_IDENTIFICATION
40019000-4001a000r--p0000000003:05184089/usr/share/locale/en_US/LC_MEASUREMENT
4001a000-4001b000r--p0000000003:05184083/usr/share/locale/en_US/LC_TELEPHONE
4001b000-4001c000r--p0000000003:05184091/usr/share/locale/en_US/LC_ADDRESS
4001c000-4001d000r--p0000000003:05184086/usr/share/locale/en_US/LC_NAME
4001d000-4001e000r--p0000000003:05184084/usr/share/locale/en_US/LC_PAPER
4001e000-4001f000r--p0000000003:05184088/usr/share/locale/en_US/LC_MESSAGES/SYS_LC_MESSAGES
4001f000-40020000r--p0000000003:05184087/usr/share/locale/en_US/LC_MONETARY
40020000-40026000r--p0000000003:05183689/usr/share/locale/ISO-8859-1/LC_COLLATE
40026000-40027000r--p0000000003:05184082/usr/share/locale/en_US/LC_TIME
40027000-4002a000r-xp0000000003:05147459/lib/libtermcap.so.2.0.8
4002a000-4002b000rw-p0000200003:05147459/lib/libtermcap.so.2.0.8
4002b000-4002c000rw-p0000000000:000
4002c000-4002e000r-xp0000000003:05147482/lib/libdl-2.3.3.so
4002e000-4002f000rw-p0000100003:05147482/lib/libdl-2.3.3.so
4002f000-40171000r-xp0000000003:05147511/lib/tls/libc-2.3.3.so
40171000-40174000rw-p0014200003:05147511/lib/tls/libc-2.3.3.so
40174000-40177000rw-p0000000000:000
40177000-40178000r--p0000000003:05184085/usr/share/locale/en_US/LC_NUMERIC
40178000-401a4000r--p0000000003:05183688/usr/share/locale/ISO-8859-1/LC_CTYPE
401a4000-401a5000r-xp0000000003:05180462/usr/lib/gconv/ISO8859-1.so
401a5000-401a6000rw-p0000100003:05180462/usr/lib/gconv/ISO8859-1.so
401b3000-401bd000r-xp0000000003:05147492/lib/libnss_files-2.3.3.so
401bd000-401be000rw-p0000900003:05147492/lib/libnss_files-2.3.3.so
bfffa000-c0000000rwxpffffb00000:000
ffffe000-fffff000---p0000000000:000
mem_usagecommandtoconsolidatedata
DavidSchleefwroteaprogramtoconsolidatetheinformationfrom/proc/<pid>/maps,andtotalupeachkindofmemoryforaprocess.
Hereitis:
Media:mem_usage(Itwasobtainedfrom
Hereistheresultofrunningmem_usageontheprocessusedinthepreviousexample:
$./mem_usage25042
Backedbyfile:
Executabler-x2048
Write/Exec(jumptables)rwx0
ROdatar--240
Datarw-56
Unreadable---0
Unknown0
Anonymous:
Writablecode(stack)rwx416
Data(malloc,mmap)rw-20
ROdatar--0
Unreadable---4
Unknown0
Inaccuraciesofkernelreportingmechanisms
Manyofthememoryreportingmechanismsforthekernelareinaccurate,duetonotrecordingsufficientinformationaboutthetruestateofthesystem.Herearesomerandomnotesontheseinaccuracies.Toseeinformationondifferentmethodsofgettingmoreaccuratememoryinformation,see
AccurateMemoryMeasurement
"copy-on-write"pages-anmmap'edfilemaybeverylargeintheprocessaddressspace,butemptyuntilwrittento.
FromRatboyonSlashdot:
Themmap()callcanmapafile(backingstore)andallowdatatobeshared.Memorydoes
notneedtobeuseduntilthedataisread(orwritten).Andthistime,thebacking
storedoesn'tevenneedswap(becausethefileisthebackingstore).
...
Apageofcodethatisshared-maybecomeapageofcodethatisprivate.Apageof
datathatisunwrittendoesn'thavetoexist.Evenifitisread!Apageofdatathat
iswrittenmaySTILLbeshared.
FromothersonSlashdot:
Topwillshowyouthesameaspsdoes,psreads/proc/<pid>/statmandaskswhat'sgoingon.Theproblemonlinuxisthecopyonwriteprinciplewhichsavesheapsofmemory,butmakesitvirtuallyimpossibletofigureoutwhatbelongstowhat.Thethingis,
whenyouforkitmapsthememoryandmarkseverythingascopyonwrite,whensomethingneedstowritetopartofthememory,thenitwillmakethecopyforeachprocess.
Howeveraskingtheprocesshowmuchmemoryithasallocatedwillshowallmemoryincludingstuffthatismarkedcopyonwrite-thatis,Icouldhave100processesshowingtheyeachuse1.4MBofmemory,becausetheyallsharethesamelibrary,butinfact,
itsthesamecopytheyareallusingsoI'monlyusing1.4MBinsteadof140MB(+PCBet.al)
Eachthreadinaprocessshowsupasconsumingthesameamountofmemory(eitherthisonlyhappensunderLinuxthreadsorIdon'thaveanythreadedapplicationsrunningonmysystem).
Devicemappingsshowupasconsumedmemory(whichgeneratesplentyofcomplaintsabouttheXserver).IfyouwanttofindouthowmuchmemoryXisactuallyusing(bytesincachedpixmapsonbehalfofeachprocessandsansdevicemappings),trytheprogramhere:
ThiscontainsatinyprogramthatlistshowmuchmemoryXisusingforotherprogramsbycachingpixmapsandaperlscriptthatlistshowmuchmemoryXisusingsansdevicemappings.}}}
pmapisautilitywhichshowsthememoryusageofaprocess(itlookslikeitjustreadsandinterprets/proc/<pid>/maps).
SomeoneonSlashdotsaid:
pmap*also*overestimatesmemoryusage,becausesomeportionofthemappedaddress
spaceisn'tactuallyinuse.RSS,ontheotherhand,onlymeasuresmemorythatis
actuallyinuse,butdoesn'tdistinguishbetweenmemorythatissharedandmemory
thatisnot.VSZisthemostpessimisticmeasure,sinceitincludesallmapped
memory,sharedandunshared.
Heapmemoryusage
Heapisthedynamicallyallocatedmemoryinsideeachprocess'addressspacethatismanagedbytheapplicationitself.ThestructureofthismemoryisactuallymanagedbytheClibrary,withtheapplicationcallingmalloc()andfree().glibchasthecapabilitytocollectstatisticsinformationofheapfunctionslikemalloc()andotherfunctionslikememoryleakcheckingordoublefree.
MemoryDebuggers
Severaltoolsareavailabletoanalyzememoryallocations,watchforreadingandwritingbeyondtheendofallocatedmemory,anddoothertaskswhichhelpwithdebuggingandtuningmemoryoperationsofaprogram.SeeMeasuringkernelmemoryuse
KernelStackUsage
TimisaddingastackcheckingfunctiontoKFT(SeeKernelFunctionTrace)
o
Thisnewfeaturehasnotyetbeenpublished
Recent-mmtreeaddedstack-corruption-detector.patch(8thMarch,2006)
Thescripts/checkstack.plscriptinthekerneltreewillshowthefunctionswiththelargeststaticstackfootprint.
EnablingCONFIG_DEBUG_STACKOVERFLOWinthekernelwillenablecheckingforlow-stacksituationsintheirqhandler.
EnablingCONFIG_4KSTACKSwillcausestackoverflowstooccurmorefrequently,particularlywithcertainkernelcodesuchasXFSenabled.
Generalkernelmemoryuse
Check/proc/slabinfotofindhowmuchmemoryisbeingusedbythekernelSLABallocator(orSLUBorSLOB,dependingonwhatisenabled).
相关文章推荐
- Memory related MACROs in ARM linux
- [Linux]How to alloc memory (kmalloc) in Linux ISR
- How to Profile Memory in Linux
- Feature: High Memory In The Linux Kernel
- 【简记】Operating System—— memory management in Linux(暂)
- User-Level Memory Management in Linux Programming转
- How to Profile Memory in Linux
- High Memory in the Linux Kernel
- SHELL:Find Memory Usage In Linux (统计每个程序内存使用情况)
- MemoryMappedFile 在 Mono in Linux 的开发笔记
- How to programmatically clear the filesystem memory cache in C++ on a Linux system?
- MemoryMappedFile 在 Mono in Linux 的开发笔记
- Memory Allocation API In Linux Kernel && Linux Userspace、kmalloc vmalloc Difference、Kernel Large Section Memory Allocation
- High Memory In The Linux Kernel(Cited)
- Understanding Memory Technology Devices in Embedded Linux
- Read DS18B20 Temperature via Mapping Memory Method in Linux
- zz Release memory in Linux (Unused or Cached)
- How to list processes attached to a shared memory segment in linux?
- 释放Linux Inactive Memory
- Feature: High Memory In The Linux Kernel