A way to determine a process's “real” memory usage, i.e. private dirty RSS?

后端 未结 10 1678
我在风中等你
我在风中等你 2020-11-29 15:15

Tools like \'ps\' and \'top\' report various kinds of memory usages, such as the VM size and the Resident Set Size. However, none of those are the \"real\" memory usage:

10条回答
  •  無奈伤痛
    2020-11-29 16:14

    Check it out, this is the source code of gnome-system-monitor, it thinks the memory "really used" by one process is sum(info->mem) of X Server Memory(info->memxserver) and Writable Memory(info->memwritable), the "Writable Memory" is the memory blocks which are marked as "Private_Dirty" in /proc/PID/smaps file.

    Other than linux system, could be different way according to gnome-system-monitor code.

    static void
    get_process_memory_writable (ProcInfo *info)
    {
        glibtop_proc_map buf;
        glibtop_map_entry *maps;
    
        maps = glibtop_get_proc_map(&buf, info->pid);
    
        gulong memwritable = 0;
        const unsigned number = buf.number;
    
        for (unsigned i = 0; i < number; ++i) {
    #ifdef __linux__
            memwritable += maps[i].private_dirty;
    #else
            if (maps[i].perm & GLIBTOP_MAP_PERM_WRITE)
                memwritable += maps[i].size;
    #endif
        }
    
        info->memwritable = memwritable;
    
        g_free(maps);
    }
    
    static void
    get_process_memory_info (ProcInfo *info)
    {
        glibtop_proc_mem procmem;
        WnckResourceUsage xresources;
    
        wnck_pid_read_resource_usage (gdk_screen_get_display (gdk_screen_get_default ()),
                                      info->pid,
                                      &xresources);
    
        glibtop_get_proc_mem(&procmem, info->pid);
    
        info->vmsize    = procmem.vsize;
        info->memres    = procmem.resident;
        info->memshared = procmem.share;
    
        info->memxserver = xresources.total_bytes_estimate;
    
        get_process_memory_writable(info);
    
        // fake the smart memory column if writable is not available
        info->mem = info->memxserver + (info->memwritable ? info->memwritable : info->memres);
    }
    

提交回复
热议问题