当前位置: 首页 > 科技观察

TroubleshoottheproblemofinsufficientmemoryintheLinuxsystemduetoexcessivememoryusagebyCache

时间:2023-03-13 05:12:25 科技观察

ProblemDescriptionThememoryusageoftheLinuxserverexceedsthethresholdandanalarmistriggered.问题排查首先,通过free命令观察系统的内存使用情况,显示如下:totalusedfreesharedbufferscachedMem:24675796245871448865203570121612488-/+buffers/cache:226176442058152Swap:20964721082241988248其中,可以看出内存总量为24675796KB,已使用22617644KB,只剩余2058152KB。Then,afterusingthetopcommand,shift+Mtosortbymemory,observetheprocessesthatusethemostmemoryinthesystem,andfindthatonly18GBofmemoryisoccupied,andotherprocessesaresmallandcanbeignored.Therefore,whereisnearly4GBofmemory(22617644KB-18GB,about4GB)used?Further,throughcat/proc/meminfo,itisfoundthatthereisnearly4GB(3688732KB)ofSlabmemory:......Mapped:25212kBSlab:3688732kBPageTables:43524kB......Slabisusedtostorethekerneldatastructurecache,再通过slabtop命令查看这部分内存的使用情况:OBJSACTIVEUSEOBJSIZESLABSOBJ/SLABCACHESIZENAME1392634813926348100%0.21K773686183494744Kdentry_cache33404026205678%0.09K83514033404Kbuffer_head15104015053799%0.74K302085120832Kext3_inode_cache发现其中大部分(大约3.5GB)都是用于了dentry_cache。问题解决1、修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches官方文档):写入thiswillcausethekerneltodropcleancaches,dentriesandinodesfrommemory,causingthatmemorytobecomefree.Tofreepagecache:*echo1>/proc/sys/vm/drop_cachesTofreedes*echo2>/proc/sys/vm/drop_cachesTofrepagecache,dentriesandinodes:*echo3>/proc/sys/vm/drop_caches由于这是非破坏性操作,并且脏对象不可释放,因此用户应首先运行“同步”以确保释放所有缓存对象。此方法无法实现。如果用户1.2,方法1.2需要root权限。不是root,但有sudo权限,可以通过sysctl命令设置:$sync$sudosysctl-wvm.drop_caches=3$sudosysctl-wvm.drop_caches=0#recoverydrop_caches运行后,可以用sudosysctl-a|grepdrop_caches查看是否生效。3.修改/proc/sys/vm/vfs_cache_pressure,调整清理inode/dentrycaches的优先级(默认为100),LinuxInsight中有相关的解释:Atthedefaultvalueofvfs_cache_pressure=100thekernelwillattempttoreclaimdentriesandinodesata“fair”ratewithrespecttopagecacheandswapcachereclaim.Decreasingvfs_cache_pressurecausesthekerneltoprefertoretaindentryandinodecaches.Increasingvfs_cache_pressurebeyond100causesthekerneltoprefertoreclaimdentriesandinodes.具体的设置方法,youcanrefertomethod1ormethod2.Referenceshttps://www.kernel.org/doc/Documentation/sysctl/vm.txthttp://major.io/2008/12/03/reducing-inode-and-dentry-caches-to-keep-oom-killer-at-bay/http://linux-mm.org/Drop_CachesThefollowingrecordsaretheprogressoffurtherinvestigation.Deeperreasons.Theaboveinvestigationfoundthattherearealotofdentry_cacheintheLinuxsystemoccupyingmemory.Whyaretheresomanydentry_cache?1.First,clarifytheconceptandfunctionofdentry_cache:directoryentrycacheisdesignedbyLinuxtoimprovetheprocessingefficiencyofdirectoryentryobjects;itrecordsthemappingrelationshipbetweendirectoryentriesandinodes.Therefore,whentheapplicationinitiatesthestatsystemcall,thecorrespondingdentry_cacheitemwillbecreated(further,ifthefileofeachstatdoesnotexist,thentherewillalwaysbealargenumberofnewdentry_cacheitemscreated).2、当前服务器是storm集群的一个节点。首先,我想到了与风暴相关的工作流程。我strace了storm的worker进程,发现stat系统调用非常频繁,stat文件一直是新的文件名:sudostrace-fp-etrace=stat3。进一步观察,storm的worker进程会频繁的在本地目录下创建、打开、关闭、删除心跳文件,每秒都会有一个新的文件名:sudostrace-fp-etrace=open,stat,close,unlink以上就是为什么系统中有那么多dentry_cache的原因。观察/proc/meminfo发现一个奇怪的现象,slab内存分为两部分:其中大部分显示为SReclaimable,表示可以回收。但是通过slabtop观察到slab内存中最重要的部分(dentry_cache)的OBJS几乎是ACTIVE的,显示100%在使用中。OBJSACTIVEUSEOBJSIZESLABSOBJ/SLABCACHESIZENAME1392634813926348100%0.21K773686183494744Kdentry_cache33404026205678%0.09K83514033404Kbuffer_head15104015053799%0.74K302085120832Kext3_inode_cache为什么显示可回收的,但是又处于ACTIVE状态呢?请linux内核高手看到后热心解释一下:(dcache会不会因为处于ACTIVE状态而不会自动回收释放?让系统自动回收dcache。在上一节中,我们已经提到了大部分theserversontheserversslab内存处于SReclaimable可回收状态,那么是否可以交给操作系统让其在某个时间自动触发回收操作呢?答案是可以的,我查了一些linux的相关资料dcache,发现操作系统会在达到临界内存阈值时,触发kswapd内核进程释放,这个阈值的计算方法如下:1.首先greplow/proc/zoneinfo,然后得到如下结果:low1low380low120672将以上3列相加,乘以4KB,这就是阈值,通过这种方法计算后,发现recycli目前服务器的ng阈值只有48MB,所以很难看到这种现象。实际上,操作系统可能会在不等待回收的情况下挂起并变得无响应。3.可以通过以下方法增加这个阈值:将vm.extra_free_kbytes设置为与vm.min_free_kbytes相同的大小,那么/proc/zoneinfo中相应的低阈值将增加一倍,高阈值也会相应增加。等等。$须藤sysctl-a|grepfree_kbytesvm.min_free_kbytes=39847vm.extra_free_kbytes=0$sudosysctl-wvm.extra_free_kbytes=836787######1GB4。例如,当低阈值设置为1GB时,当系统空闲内存小于1GB时,观察到kswapd进程开始工作(进程状态由Sleeping变为Running),dcache开始工作同时被系统回收,直到系统空闲内存在低阈值和高阈值之间,停止回收。原文链接:http://www.cnblogs.com/panfeng412/p/drop-caches-under-linux-system.htmlhttp://www.cnblogs.com/panfeng412/p/drop-caches-under-linux-system-2.html

最新推荐
猜你喜欢