使用ps aux或top对C程序的内存使用情况进行分析

我们的老师告诉我们每秒分配500Bytes并进行分析

ps辅助

Linux上的命令

原始问题(德语)(可随时用Google翻译,翻译准确无误)

(c) Schreiben Sie ein einfaches Programm, das im Sekundentakt 500 Byte Speicher anfordert.Starten Sie das
Programm und uberprüfen Sie den Speicherplatzverbrauch zur Laufzeit.Was stellen Sie fest? Erklären Sie ihre
Beobachtung.(Hinweis: Sie können die "time.h"Bibliothek für sekundentakte Operation benutzen.)

我分配了更多并加快了速度,但是即使从理论上讲我也应该看不出清晰的结果。

我的C程序(如果我不泄漏任何内存)始终使用0.0%%MEM,即使在数学上应该使用的更多(我有16GB内存,16 * 1024 * 1024 * 1024 = 17179869184字节,17179869184/1000 = 17179869.184字节对应于我的RAM的0.1%,在进入INT_MAX之前,我可以分配2147400500字节的内存,超过我的内存的0.1%(12.4%),但在ps aux或top上仍显示0%... )

#include <stdio.h>
#include <time.h>
#include <stdlib.h> //for system() and malloc
time_t start;
char *job, *temp;
int printMenu(){
    //linux : clear, windows : cls, ..
    system("clear");
    printf("\nEnter: Wiederhole (fuer 60 Sekunden), Andere Zeichen: Quit\n");
    printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}

void delay(int mics){

    clock_t timeDelay = (mics-0.9999) * CLOCKS_PER_SEC + clock();
    while(timeDelay > clock());
}

int end(){
    delay(1);
    system("ps aux --sort -pmem | grep './2c.out' >> memreport.txt");
    delay(1);
    free(job);
    time_t end = clock();
    printf("\nTotal time elapsed: end-start clock: %lf\n", (double)(end - start)/CLOCKS_PER_SEC);

    return 0;

}
int main(){
    int exitstatus;
    int x = 500;
    start = clock();
    exitstatus = system("ps aux --sort -pmem | grep './2c.out' >> memreport.txt");

    // printf("%ld cps\n", CLOCKS_PER_SEC); // 1,000,000 oder (1takt = 1 mikrosekunde)
    system("gnome-terminal -e top -t check_memory");
    printMenu();
    char s = '\n'; // enter
    job = (char *)malloc(x);
    if(job == NULL)
    {
        printf("Allocation failed!");
        return end();
    }
    while(s == '\n')
    {

        for(int i = 0; i < 990; i++)
        {
            x += 100000;
            printf("Allocating %d Bytes..\n", x);
            /*
                job = (char *)malloc(x);
                if(job == NULL)
                {
                    printf("Malloc failed!");
                    return end();
                }
            */  
            /*
            temp = realloc(job, x);
            if( temp != NULL)
            {
                job = temp;
            }
            else
            {
                printf("Reallocation failed!");
                return end();

            }
            */


            free(job);
            temp = (char *)malloc(x);
                if(temp == NULL)
                {
                    printf("Malloc failed!");
                    return end();
                }
                else
                {
                    job = temp;
                }

           delay(1);

        }

        exitstatus = system("ps aux --sort -pmem | grep './2c.out' >> memreport.txt");

        printf("\nEnter: Wiederhole (fuer 60 Sekunden), Andere Zeichen: Quit\n");

        scanf("%c", &s);
        if(exitstatus != 0)
        {
            printf("Terminal couldn't run 'ps'");
        }
    }


    return end();

}

端子输出

Allocating 2146600500 Bytes..
Allocating 2146700500 Bytes..
Allocating 2146800500 Bytes..
Allocating 2146900500 Bytes..
Allocating 2147000500 Bytes..
Allocating 2147100500 Bytes..
Allocating 2147200500 Bytes..
Allocating 2147300500 Bytes..
Allocating 2147400500 Bytes..
Allocating -2147466796 Bytes..
Segmentation fault (core dumped)

在memreport.txt上输出

inducti+ 21367  0.0  0.0  22948  1148 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21365  0.0  0.0   4624   784 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364  0.0  0.0   4372   708 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21364  0.0  0.0 101188  1488 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21382  0.0  0.0  22948  1108 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21380  0.0  0.0   4624   832 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364  7.0  0.0 197864  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21387  0.0  0.0  22948  1048 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21385  0.0  0.0   4624   800 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 10.6  0.0 294544  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21390  0.0  0.0  22948  1080 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21388  0.0  0.0   4624   772 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 14.0  0.0 391224  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21393  0.0  0.0  22948  1052 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21391  0.0  0.0   4624   804 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 18.0  0.0 487904  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21396  0.0  0.0  22948  1008 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21394  0.0  0.0   4624   808 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 16.0  0.0 584584  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21399  0.0  0.0  22948  1016 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21397  0.0  0.0   4624   820 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 18.7  0.0 681264  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21402  0.0  0.0  22948  1084 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21400  0.0  0.0   4624   800 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 21.5  0.0 777944  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21405  0.0  0.0  22948  1104 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21403  0.0  0.0   4624   804 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 24.2  0.0 874624  1552 pts/0    S+   21:40   0:00 ./2c.out
inducti+ 21408  0.0  0.0  22948  1060 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21406  0.0  0.0   4624   804 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 27.0  0.0 971304  1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21411  0.0  0.0  22948  1008 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21409  0.0  0.0   4624   828 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 29.5  0.0 1067984 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21414  0.0  0.0  22948  1004 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21412  0.0  0.0   4624   820 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 32.5  0.0 1164664 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21417  0.0  0.0  22948  1088 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21415  0.0  0.0   4624   876 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 35.2  0.0 1261344 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21420  0.0  0.0  22948   968 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21418  0.0  0.0   4624   776 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 30.2  0.0 1358024 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21423  0.0  0.0  22948  1008 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21421  0.0  0.0   4624   808 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 32.6  0.0 1454700 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21426  0.0  0.0  22948  1036 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21424  0.0  0.0   4624   784 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 34.8  0.0 1551380 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21429  0.0  0.0  22948  1004 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21427  0.0  0.0   4624   812 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 30.8  0.0 1648060 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21432  0.0  0.0  22948  1032 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21430  0.0  0.0   4624   820 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 32.8  0.0 1744740 1552 pts/0    S+   21:40   0:01 ./2c.out
inducti+ 21435  0.0  0.0  22948  1088 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21433  0.0  0.0   4624   772 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 34.6  0.0 1841420 1552 pts/0    S+   21:40   0:02 ./2c.out
inducti+ 21438  0.0  0.0  22948  1000 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21436  0.0  0.0   4624   820 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 36.6  0.0 1938100 1552 pts/0    S+   21:40   0:02 ./2c.out
inducti+ 21441  0.0  0.0  22948  1052 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21439  0.0  0.0   4624   784 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 38.5  0.0 2034780 1552 pts/0    S+   21:40   0:02 ./2c.out
inducti+ 21444  0.0  0.0  22948  1004 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21442  0.0  0.0   4624   832 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt
inducti+ 21364 34.1  0.0   4504  1552 pts/0    S+   21:40   0:02 ./2c.out
inducti+ 21447  0.0  0.0  22948  1004 pts/0    S+   21:40   0:00 grep ./2c.out
inducti+ 21445  0.0  0.0   4624   804 pts/0    S+   21:40   0:00 sh -c ps aux --sort -pmem | grep './2c.out' >> memreport.txt

如果我不使用free,而是使用注释掉的块之一分配内存,则我的%MEM会上升到0.04%

job = (char *)malloc(x);
if(job == NULL)
{
   printf("Malloc failed!");
   return end();
}

但这就是内存泄漏。

ps aux和top命令的结果是否仅显示内存泄漏,如果是这样,这有什么意义?

我试图用谷歌搜索答案,但找不到任何令人满意的东西,我相信谷歌搜索,请不要仅仅链接到一个已经给出的答案,而这个答案与这个答案并不十分相似...

最后的笔记

我知道delay()函数不会延迟1秒钟,但是更少,我暂时做到了,只是为了更快地看到结果      如果我使用显示的备用代码块进行内存分配,也会收到“终端无法运行'ps'”错误,其背后的原因对我来说还是不清楚的...

感谢您提供的任何帮助,谢谢:) 如果问题太复杂或不清楚,我们将深感抱歉。