linux 에서 프로세스의 RSS 값이 바로 갱신이 안됩니다. 왜 그런가요?

trymp의 이미지


linux 32bit 에서 ps axuf 로 보면 프로세스의 메모리 사용량 VSZ, RSS 값등을 볼수 있잖아요

여기서 아래와 같은 코드로 테스트를 했을 때, alloc 을 하면 바로 RSS 값에 반영이 되는데,

free 를 시키면 RSS 값이 감소되지 않고 그대로 유지 됩니다. 왜 그런가요?

통계수치에 늦게 반영되는 것인지? 아니면 프로세스가 free 임에도 계속 메모리를 잡고 있는지 궁금합니다.

만일 계속 잡고 있다면 메모리를 더 이상 메모리를 할당 못하는 상황에서 문제가 될 것 같은데요.

고수님들의 조언 부탁드립니다.

#define CHUNK_SIZE (1024 * 100)
 
 
int g_mem_alloc = 0;
int g_mem_free = 0;
 
void sig_handler(int signum)
{
    if (signum == SIGUSR1)
        g_mem_alloc = 1;
    else if (signum == SIGUSR2)
        g_mem_free = 1;
 
}
 
int main(int ac, char *av[])
{
    int i;
    TAILQ_HEAD(, mem_chunk) mem_chunk_head;
    unsigned char *chunk = NULL;
    struct mem_chunk *entry = NULL;
 
    TAILQ_INIT(&mem_chunk_head);
 
    signal(SIGUSR1, sig_handler);
    signal(SIGUSR2, sig_handler);
 
    while (1)
    {
        if (g_mem_alloc)
        {
            g_mem_alloc = 0;
 
            for (i = 0; i < 100; i++)
            {
                entry = malloc(sizeof(*entry));
                if (entry)
                {
                    chunk = malloc(CHUNK_SIZE);
 
                    if (chunk)
                    {
                        chunk[0] = '\0';
                        chunk[CHUNK_SIZE-1] = '\0';
                        entry->data = chunk;
                        TAILQ_INSERT_TAIL(&mem_chunk_head, entry, l);
                        printf("alloc \n");
                    }
                    else
                        free(entry);
                }
            }
        }
        if (g_mem_free)
        {
            g_mem_free = 0;
 
            while ((entry = TAILQ_FIRST(&mem_chunk_head)))
            {
                TAILQ_REMOVE(&mem_chunk_head, entry, l);
                free(entry->data);
                free(entry);
                printf("free \n");
            }
        }
        usleep(100);
    }
 
    return 0;
}

pchero의 이미지

RSS와 VSZ에 대해서 살펴봤습니다. 리얼 메모리/가상 메모리로 구분되네요. 가상메모리는 SWAP 메모리 영역, 리얼메모리는 실제 RAM 영역으로 보여집니다. -> 이부분 확실치가 않네요. 아시는 분 댓글 바랍니다.

rss RSS resident set size, the non-swapped physical memory that a task has used (inkiloBytes). (alias rssize, rsz).
vsz VSZ virtual memory size of the process in KiB (1024-byte units). Device mappings are currently excluded; this is subject to change. (alias vsize)

그런데, 리눅스에서는 메모리 해제시, 바로 돌려주는 것이 아니고 Cached 메모리로 관리합니다. 이후 top 로 메모리 사용량을 확인할 시, cached 메모리 역시 현재 사용중인 메모리 공간에 포함되어 확인됩니다.
따라서 질문하신 부분은 확인이 안될 수도 있는 것 같습니다.

그리고 프로그램의 메모리 누수와 관련해서는 valgrind 사용을 추천합니다.

$ sudo valgrind --leak-check=full --tool=memcheck ./a.out

---------------------------------
제일 왼쪽이 저입니다 :)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.