您的位置:首页 > 运维架构 > Linux

Measuring memory in Linux (the basics)

2011-11-11 14:44 483 查看
ReferenceLink:http://elinux.org/Runtime_Memory_Measurement

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

Seehttp://lwn.net/Articles/28345/foradescriptionofthesefields

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|

Hereanexample:69340658615805350

/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(Itwasobtainedfromhttp://www.schleef.org/~ds/mem_usage)
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.Toseeinformationondifferentmethodsofgettingmore
accuratememoryinformation,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:
http://69.142.116.122/dist/pixmap_mem-1.0.tgz

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.See
MemoryDebuggersforalistofdifferenttoolsandtheirfeatures.

Measuringkernelmemoryuse

KernelStackUsage

TimisaddingastackcheckingfunctiontoKFT(See
KernelFunctionTrace)

o

Thisnewfeaturehasnotyetbeenpublished

Recent-mmtreeaddedstack-corruption-detector.patch(8thMarch,2006)
Thescripts/checkstack.plscriptinthekerneltreewillshowthefunctionswiththelargeststaticstackfootprint.

EnablingCONFIG_DEBUG_STACKOVERFLOWinthekernelwillenablecheckingforlow-stacksituationsintheirqhandler.

EnablingCONFIG_4KSTACKSwillcausestackoverflowstooccurmorefrequently,particularlywithcertainkernelcodesuchasXFSenabled.

Generalkernelmemoryuse

http://www.halobates.de/memorywaste.pdf-GreatpaperbyAndiKleen,ofSUSELabs,aboutdynamicmemoryusageofLinuxsystems

Check/proc/slabinfotofindhowmuchmemoryisbeingusedbythekernelSLABallocator(orSLUBorSLOB,dependingonwhatisenabled).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: