memory indexing differences

for 구문 사용시, 인덱싱하는 order 에 따라, 실행시간에 차이가 발생한다는 글을 보았다(참조: http://process3.blog.me/20030421397)

정말로 그럴까? 한번 확인해 보았다. 결론은? 확실한 차이가 있었다.

이유를 확인해보니, 메모리 캐시와 관련된 내용이었다.

 

— Program 1
#include <stdio.h>
#define MAX_CNT 100
int main(int argc, char** argv)
{
    int i, j, k;
    int test_data[MAX_CNT][MAX_CNT][MAX_CNT];
    for(i = 0; i < MAX_CNT; i++)
    {
        for(j = 0; j < MAX_CNT; j++)
        {
            for(k = 0; k < MAX_CNT; k++)
            {
                test_data[i][j][k] = 100;
            }
        }
    }
    return 0;
}
Result
$ time ./main
real    0m0.008s
user    0m0.004s
sys     0m0.004s
— Program 2
#include <stdio.h>
#define MAX_CNT 100
int main(int argc, char** argv)
{
    int i, j, k;
    int test_data[MAX_CNT][MAX_CNT][MAX_CNT];
    for(i = 0; i < MAX_CNT; i++)
    {
        for(j = 0; j < MAX_CNT; j++)
        {
            for(k = 0; k < MAX_CNT; k++)
            {
                test_data[k][j][i] = 100;
            }
        }
    }
    return 0;
}
$ time ./main
real    0m0.017s
user    0m0.013s

sys     0m0.004s

 

힙 영역 사용시, 보다 더 큰 차이를 확인할 수 있다.

— Program 1
#include <stdio.h>
#include <stdlib.h>
#define MAX_CNT 500
int main(int argc, char** argv)
{
    int i, j, k;
//    int test_data[MAX_CNT][MAX_CNT][MAX_CNT];
    int*** test_data;
    test_data = calloc(MAX_CNT, sizeof(int**));
    for(i = 0; i < MAX_CNT; i++)
    {
        test_data[i] = calloc(MAX_CNT, sizeof(int*));
        for(j = 0; j < MAX_CNT; j++)
        {
            test_data[i][j] = calloc(MAX_CNT, sizeof(int));
        }
    }
    for(i = 0; i < MAX_CNT; i++)
    {
        for(j = 0; j < MAX_CNT; j++)
        {
            for(k = 0; k < MAX_CNT; k++)
            {
                test_data[i][j][k] = 100;
            }
        }
    }
    return 0;
}
Result
$ time ./main
real    0m0.539s
user    0m0.367s
sys     0m0.172s
— Program 2
#include <stdio.h>
#include <stdlib.h>
#define MAX_CNT 500
int main(int argc, char** argv)
{
    int i, j, k;
//    int test_data[MAX_CNT][MAX_CNT][MAX_CNT];
    int*** test_data;
    test_data = calloc(MAX_CNT, sizeof(int**));
    for(i = 0; i < MAX_CNT; i++)
    {
        test_data[i] = calloc(MAX_CNT, sizeof(int*));
        for(j = 0; j < MAX_CNT; j++)
        {
            test_data[i][j] = calloc(MAX_CNT, sizeof(int));
        }
    }
    for(i = 0; i < MAX_CNT; i++)
    {
        for(j = 0; j < MAX_CNT; j++)
        {
            for(k = 0; k < MAX_CNT; k++)
            {
                test_data[k][j][i] = 100;
            }
        }
    }
    return 0;
}
Result
$ time ./main
real    0m5.344s
user    0m5.202s
sys     0m0.147s

How to clear memory cache in Linux

Linux 에서 캐시된 메모리를 어떻게 정리할 수 있을까? 결론적으로 세 가지 방법으로 캐시된 메모리 삭제가 가능하다.

1. 페이지 캐시 해제

2. 해제 가능 오브젝트(dentry, inode) 해제
– dentry에 대해서는 여기(http://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-file)를 참조하자.

3. 해제 가능 오브젝트 + 페이지 캐시 해제

아래는 캐시된 메모리 해제와 관련한 자세한 내용이다.(https://www.kernel.org/doc/Documentation/sysctl/vm.txt)

drop_caches

Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes.  Once dropped, their
memory becomes free.

To free pagecache:
	echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
	echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
	echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync' prior to writing to /proc/sys/vm/drop_caches.  This will minimize the
number of dirty objects on the system and create more candidates to be
dropped.

This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...)  These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems.  Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use.  Because of this,
use outside of a testing or debugging environment is not recommended.

You may see informational messages in your kernel log when this file is
used:

	cat (1234): drop_caches: 3

These are informational only.  They do not mean that anything is wrong
with your system.  To disable them, echo 4 (bit 3) into drop_caches.

실제로 캐시를 줄이는 명령어는 원하는 모드에 따라 아래와 같은 방식으로 입력하면 된다.

$ echo 1 > /proc/sys/vm/drop_caches
$ echo 2 > /proc/sys/vm/drop_caches
$ echo 3 > /proc/sys/vm/drop_caches

다음은 테스트 결과이다. 확실히 캐시된 메모리가 줄어드는 것을 확인할 수 있다.

 

root@mywork:~# free -m
total       used       free     shared    buffers     cached
Mem:          7703       3618       4084        433        309       1239
-/+ buffers/cache:       2069       5633
Swap:         7627          0       7627

root@mywork:~# echo 1 > /proc/sys/vm/drop_caches

root@mywork:~# free -m
total       used       free     shared    buffers     cached
Mem:          7703       2700       5003        432          0        641
-/+ buffers/cache:       2058       5644
Swap:         7627          0       7627

 

참조: http://unix.stackexchange.com/questions/58553/how-to-clear-memory-cache-in-linux

why ps is wrong?

오늘 kldp 에 ps로 메모리를 확인하려고 하는데 잘 안된다는 질문 내용을 봤다.

https://kldp.org/node/152025

답글을 작성하다가 재미있는 내용을 알게 되어 이곳에도 같이 담아둔다.

 

질문 내용

프로그램 안에서 10 바이트씩 malloc 으로 메모리를 할당했습니다.
그리고 ps -eo user,size,cmd 명령으로 메모리 증가량으로 확인하려고 했지만 전혀 메모리 증가가 안되네요.

왜 그런거죠?

답변

두 가지 이유가 있습니다.

1. 확인하고자 하시는 메모리 증가량이 너무 작습니다.

man ps 에서 size 항목을 찾아봤는데 따로 단위는 안나오네요.
하지만 테스트 해보니 단위가 1K 였습니다.
프로그램을 수정해서 한번에 1M 씩 증가하도록 해봤는데, 증가량이 확인되네요.
할당하는 메모리 크기를 1M 단위로 하신다면 쉽게 증가 내역을 확인하실 수 있습니다.

2. ps 명령은 정확한 메모리 량을 확인하기에는 부족한 유틸리티 입니다.
출처 : http://stackoverflow.com/questions/131303/how-to-measure-actual-memory-u…

ps 명령은 사실 어플리케이션에서 사용하는 정확한 메모리 양을 나타내지 않습니다. 단지 예약된 메모리 양을 나타낼 뿐입니다.
달리 말하면 때에 따라(커널 레벨에서 사용되는 페이지가 공유 되거나 할 경우)변동될 소지가 있다는 것입니다. (예를 들면 여러개의 쓰레드나 동적 라이브러리를 사용하는 경우가 있습니다.)

그리고 만약 정확한 메모리 크기를 확인하고자 하신다면 다른 프로그램을 사용하셔야 합니다. valgrind 가 대표적이죠. 주로 메모리 누수 탐지에 사용되지만 메모리 사용량을 확인할 수도 있습니다.

————————————————————————————

실제로 위에 인용한 스택 오버플로우의 내용 말고도 ps 는 메모리 사용량을 확인하는데 부족하다는 내용의 많은 양의 문서를 확인할 수 있었다.

정확한 메모리 사용량을 확인하고자 한다면, 다음의 링크에 소개된 프로그램을 이용하자.

http://www.binarytides.com/linux-command-check-memory-usage/