x86_64 환경에서 mallinfo() 함수로 malloc()한 크기를 구하려고 합니다.

ukyoukyo의 이미지

mallinfo() 함수로 동적할당된 메모리의 크기를 구하려고 합니다.

장비에 설치된 리눅스는 아래와 같습니다.

[root@SexyGirl:/root/MM] # uname -a
Linux SexyGirl 2.6.32-24-generic #38-Ubuntu SMP Mon Jul 5 09:20:59 UTC 2010 x86_64 GNU/Linux

64비트 리눅스라서 램을 4[GB] 장착했구요, 시스템은 아래와 같이 인식합니다.

[root@SexyGirl:/root/MM] # free -m
             total       used       free     shared    buffers     cached
Mem:     3951         570       3381             0            6            85
-/+ buffers/cache:  478      3473
Swap:         0             0           0

사용하려는 mallinfo 구조체는 아래와 같구요.

struct mallinfo
{
   int arena;    // non-mmapped space allocated from system
   int ordblks;  // number of free chunks
   int smblks;   // number of fastbin blocks
   int hblks;    // number of mmapped regions
   int hblkhd;   // space in mmapped regions
   int usmblks;  // maximum total allocated space
   int fsmblks;  // space available in freed fastbin blocks
   int uordblks; // total allocated space
   int fordblks; // total free space
   int keepcost; // top-most, releasable (via malloc_trim) space
};

위 멤버중에서 'ublkhd'가 malloc()하면 내부적으로 mmap()이 실행되어 증가되는 값으로 알고 있습니다.

#free -m의 결과중에 'free' 필드에서 나타내는 용량만큼(위 예제에서는 3381[MB]) malloc()한다고 가정하면,

RAM의 전체용량이 늘어나서 8[GB], 16[GB]가 되면 'free' 필드의 용량도 증가할텐데,

int ublkhd 변수로 표현할 수 있는 범위를 넘어서지 않는지요 ?




범위를 넘어선다면 어떻게 malloc()으로 할당된 사이즈를 파악할 수 있는지요 ? 이게 목적이긴합니다만.

동적할당된 메모리의 크기를 구하는 API나 방법은 없는지요 ?

mac040의 이미지

또 64bit를 위한 방법이 딱히 없다는 글도 있습니다.
HP-UX에서 같은 문제를 질문한 글과 답인데 다른 글을 찾아봐도 딱히 다른의견은 없는 것 같습니다.

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=943026

pastime의 이미지

malloc_stats, malloc_info 등을 사용할 수 있지만
파일 스트림에 쓰는 형태라 좀 불편할 수도 있습니다.

malloc 소스를 보면 malloc_get_state()를 이용할 수도 있겠습니다만
호환성 같은 건 전혀 고려할 수 없겠지요..
(위의 API도 어차피 호환되지는 않을 수 있겠군요.. ;;)

여기도 참조해 보세요.. ;;
http://studyfoss.egloos.com/5209389

ukyoukyo의 이미지


답변감사합니다.

malloc_stats() 의 소소를 한번 뒤져봐야 겠네요...

------------------ System programmer...^^


------------------ System programmer...^^