Linux内存占用常用的几个分析方法,你确定都知道?( 四 )

注意这个文件显示的单位是kB而不是KB , 1kB=1000B,但是实际上应该是KB , 1KB=1024B
还可以使用命令 less /proc/meminfo 直接读取该文件 。通过使用 less 命令 , 可以在长长的输出中向上和向下滚动 , 找到你需要的内容 。
从中我们可以很清晰明了的看出内存中的各种指标情况 , 例如 MemFree的空闲内存和SwapFree中的交换内存 。
3. 代码实例负责输出/proc/meminfo的源代码是:
fs/proc/meminfo.c : meminfo_proc_show()static int meminfo_proc_show(struct seq_file *m, void *v){ struct sysinfo i; unsigned long committed; long cached; long available; unsigned long pages[NR_LRU_LISTS]; int lru;si_meminfo(&i); si_swapinfo(&i); committed = percpu_counter_read_positive(&vm_committed_as);cached = global_node_page_state(NR_FILE_PAGES) -total_swapcache_pages() - i.bufferram; if (cached < 0)cached = 0;for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)pages[lru] = global_node_page_state(NR_LRU_BASE + lru);available = si_mem_available();show_val_kb(m, "MemTotal:", i.totalram); show_val_kb(m, "MemFree:", i.freeram); show_val_kb(m, "MemAvailable:", available); show_val_kb(m, "Buffers:", i.bufferram); show_val_kb(m, "Cached:", cached); show_val_kb(m, "SwapCached:", total_swapcache_pages()); show_val_kb(m, "Active:", pages[LRU_ACTIVE_ANON] +pages[LRU_ACTIVE_FILE]); show_val_kb(m, "Inactive:", pages[LRU_INACTIVE_ANON] +pages[LRU_INACTIVE_FILE]); show_val_kb(m, "Active(anon):", pages[LRU_ACTIVE_ANON]); show_val_kb(m, "Inactive(anon): ", pages[LRU_INACTIVE_ANON]); show_val_kb(m, "Active(file):", pages[LRU_ACTIVE_FILE]); show_val_kb(m, "Inactive(file): ", pages[LRU_INACTIVE_FILE]); show_val_kb(m, "Unevictable:", pages[LRU_UNEVICTABLE]); show_val_kb(m, "Mlocked:", global_zone_page_state(NR_MLOCK)); #ifdef CONFIG_HIGHMEM show_val_kb(m, "HighTotal:", i.totalhigh); show_val_kb(m, "HighFree:", i.freehigh); show_val_kb(m, "LowTotal:", i.totalram - i.totalhigh); show_val_kb(m, "LowFree:", i.freeram - i.freehigh);#endif #ifndef CONFIG_MMU show_val_kb(m, "MmapCopy:",(unsigned long)atomic_long_read(&mmap_pages_allocated));#endifshow_val_kb(m, "SwapTotal:", i.totalswap); show_val_kb(m, "SwapFree:", i.freeswap); show_val_kb(m, "Dirty:",global_node_page_state(NR_FILE_DIRTY)); show_val_kb(m, "Writeback:",global_node_page_state(NR_WRITEBACK)); show_val_kb(m, "AnonPages:",global_node_page_state(NR_ANON_MAPPED)); show_val_kb(m, "Mapped:",global_node_page_state(NR_FILE_MAPPED)); show_val_kb(m, "Shmem:", i.sharedram); show_val_kb(m, "Slab:",global_node_page_state(NR_SLAB_RECLAIMABLE) +global_node_page_state(NR_SLAB_UNRECLAIMABLE));show_val_kb(m, "SReclaimable:",global_node_page_state(NR_SLAB_RECLAIMABLE)); show_val_kb(m, "SUnreclaim:",global_node_page_state(NR_SLAB_UNRECLAIMABLE)); seq_printf(m, "KernelStack:%8lu kBn",global_zone_page_state(NR_KERNEL_STACK_KB)); show_val_kb(m, "PageTables:",global_zone_page_state(NR_PAGETABLE));#ifdef CONFIG_QUICKLIST show_val_kb(m, "Quicklists:", quicklist_total_size());#endifshow_val_kb(m, "NFS_Unstable:",global_node_page_state(NR_UNSTABLE_NFS)); show_val_kb(m, "Bounce:",global_zone_page_state(NR_BOUNCE)); show_val_kb(m, "WritebackTmp:",global_node_page_state(NR_WRITEBACK_TEMP)); show_val_kb(m, "CommitLimit:", vm_commit_limit()); show_val_kb(m, "Committed_AS:", committed); seq_printf(m, "VmallocTotal:%8lu kBn",(unsigned long)VMALLOC_TOTAL >> 10); show_val_kb(m, "VmallocUsed:", 0ul); show_val_kb(m, "VmallocChunk:", 0ul); #ifdef CONFIG_MEMORY_FAILURE seq_printf(m, "HardwareCorrupted: %5lu kBn",atomic_long_read(&num_poisoned_pages) << (PAGE_SHIFT - 10));#endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE show_val_kb(m, "AnonHugePages:",global_node_page_state(NR_ANON_THPS) * HPAGE_PMD_NR); show_val_kb(m, "ShmemHugePages: ",global_node_page_state(NR_SHMEM_THPS) * HPAGE_PMD_NR); show_val_kb(m, "ShmemPmdMapped: ",global_node_page_state(NR_SHMEM_PMDMAPPED) * HPAGE_PMD_NR);#endif #ifdef CONFIG_CMA show_val_kb(m, "CmaTotal:", totalcma_pages); show_val_kb(m, "CmaFree:",global_zone_page_state(NR_FREE_CMA_PAGES));#endifhugetlb_report_meminfo(m);arch_report_meminfo(m);return 0;}四、top 指令用途: 用于打印系统中的CPU和内存使用情况 。输出结果中 , 可以很清晰的看出已用和可用内存的资源情况 。top 最好的地方之一就是发现可能已经失控的服务的进程 ID 号(PID) 。有了这些 PID , 你可以对有问题的任务进行故障排除(或 kill) 。


推荐阅读