Easiest way to see linux memory usage when a process is killed

I've got a build server where dmesg is reporting that it is having to kill processes because it is running out of memory. As the system is running many builds and other processes concurrently, I need to figure out which process or processes are really using too much memory. i.e. I'm not convinced the process being killed is the one hogging the memory.

Ideally I'd like to dump the memory usage at the point when the out of memory killer kicks in, with a full command line of each process. Is there a way to do this? Alternatively, if I can't dump it at that specific point, I plan to set up a cron job to dump memory usage every minute or two, but I still need some help to get the correct output.

The output from smem is pretty good, but it truncates the command line:

PID User     Command                         Swap      USS      PSS      RSS
39090 user   /usr/bin/Xvfb +extension RA     4732      144      148      264
20837 user   -bash                              0      780     1100     2144
21144 user   python /usr/bin/smem               0    12120    12320    13248
19224 user   /opt/atlassian/bamboo_home/        0   234940   235303   237144
12414 user   /usr/java/jdk1.8.0_121/bin/   176128  2249180  2249338  2250428

Is there a way to tell smem to show the full command line? Alternatively a simple way of piping the output to show me what I need? I can pipe into xargs and ps to get the full command line like this:

smem -H -c "pid" | xargs ps

However then I've lost the memory usage values from smem.

2
задан 9 February 2018 в 14:51
3 ответа

Если у вас есть имя процесса, вы можете найти здесь довольно полезный ответ: Определение среднего размера одного процесса Apache для настройки MaxClients

Вы можете заменить httpd в начале этой команды с вашим именем процесса, и он покажет вам общее использование памяти для процессов с этим именем в первой строке и среднее использование памяти этими процессами во второй строке. Надеюсь это поможет! :)

1
ответ дан 3 December 2019 в 11:26

пробовали ли вы использовать top и c toggle, чтобы видеть имя процесса и командную строку?

https://en.wikipedia.org/wiki/Top_ (программное обеспечение)

0
ответ дан 3 December 2019 в 11:26

Я смоделировал сбой памяти с помощью этого кода:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv) {
    int max = -1;
    int mb = 0;
    char* buffer;

    if(argc > 1)
        max = atoi(argv[1]);

    while((buffer=malloc(1000*1024*1024)) != NULL && mb != max) {
        memset(buffer, 0, 1000*1024*1024);
        mb++;
        printf("Allocated %d MB\n", 1000*mb);
        sleep(1);
    }

    return 0;
}

Я помещаю код в файл munch.c , а затем:

gcc -o munch munch.c
./munch # will run until SIGINT or killed by kernel

Я открыл две другие консоли и запустил на них следующее:

# 1st console
dmesg -w
# 2nd console
sudo tail -f /var/log/{kern.log,syslog}

Однако я ничего не заметил после того, как процесс munch был прерван.

Думаю, вам просто нужно написать Скрипт для отслеживания потребления памяти. Это может быть отправной точкой:

watch 'ps auxw | head -1 && ps auxw | sort -k6 -nr | head -5'

Вы будете получать регулярно обновляемый список процессов, отсортированных по RSS . Параметр w показывает полные имена команд.

1
ответ дан 3 December 2019 в 11:26

Теги

Похожие вопросы