linux 에서 프로세스의 RSS 값이 바로 갱신이 안됩니다. 왜 그런가요?
글쓴이: trymp / 작성시간: 수, 2015/01/21 - 6:55오후
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;
}Forums:


RSS와 VSZ에 대해서 살펴봤습니다. 리얼
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 사용을 추천합니다.
---------------------------------
제일 왼쪽이 저입니다 :)
댓글 달기