malloc()을 통해 heap 영역의 크기를 키워보기(1MB이상)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int *arr;
// for(int i=0;i<1000;i++)
// arr=malloc(1024);
sleep(1000);
free(arr);
return 0;
}
아래와 같이 heap 영역이 없는 것을 알 수 있다
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int *arr;
for(int i=0;i<1000;i++)
arr=malloc(1024);
sleep(1000);
free(arr);
return 0;
}
- 아래와 같이 heap 영역이 생성된 것을 확인할 수 있다.
- heap 메모리 주소공간을 계산해보면 대략 1MB가 나오는 것을 확인할 수 있다.
- 전부 malloc(1024) 인 것을 확인 할 수 있다.
- 첫 brk() 시스템 콜을 통해 heap 영역을 생성하고, 계속해서 brk() 시스템 콜을 호출하여 필요에 따라 heap 영역을 키워가는 것을 확인할 수 있다.
touch test.txt; echo "hello world!" > test.txt
- mmap()으로 test.txt 를 mapping 하고 파일의 내용을 읽고 printf()로 출력
- 파일의 내용을 "byebye world!!!!!" 로 바꾸고 close()
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
const char* file_path = "test.txt";
const char* new_content = "byebye world!!!!!\n";
int file_descriptor;
struct stat file_info;
char* file_data;
//open the file
file_descriptor = open(file_path, O_RDWR);
if(file_descriptor == -1){
perror("Error opening file");
return 1;
}
//Get file information
if(fstat(file_descriptor, &file_info)== -1){
perror("Error getting file information");
close(file_descriptor);
return 1;
}
// Map the file into memory = mmap()
file_data = mmap(NULL, file_info.st_size, PROT_READ | PROT_WRITE,
MAP_SHARED, file_descriptor,0 );
if(file_data == MAP_FAILED){
perror("Error mapping file to memory");
close(file_descriptor);
return 1;
}
//Output file content
printf("File contents:\n%s\n", file_data);
//Change file content
strcpy(file_data, new_content);
//Clean up
munmap(file_data, file_info.st_size);
close(file_descriptor);
return 0;
}
- 변경할 내용의 뒷부분이 잘리는 것을 확인 할 수 있다.
posix_fallocate(file_descriptor, 0, 늘리고자 하는 크기)
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
const char* file_path = "test.txt";
const char* new_content = "byebye world!!!!!\n";
int file_descriptor;
struct stat file_info;
char* file_data;
//open the file
file_descriptor = open(file_path, O_RDWR);
if(file_descriptor == -1){
perror("Error opening file");
return 1;
}
//Get file information
if(fstat(file_descriptor, &file_info)== -1){
perror("Error getting file information");
close(file_descriptor);
return 1;
}
// Map the file into memory = mmap()
file_data = mmap(NULL, file_info.st_size, PROT_READ | PROT_WRITE,
MAP_SHARED, file_descriptor,0 );
if(file_data == MAP_FAILED){
perror("Error mapping file to memory");
close(file_descriptor);
return 1;
}
//Output file content
printf("File contents:\n%s\n", file_data);
//Change file content
strcpy(file_data, new_content);
//fallocate()
posix_fallocate(file_descriptor,0,1024);
//Clean up
munmap(file_data, file_info.st_size);
close(file_descriptor);
return 0;
}
- 변경 내용이 전부 입력 된 것을 확인 할 수 있다.
sudo su echo 3 > /proc/sys/vm/drop_caches free
- MAP_PRIVATE, MAP_ANNONYMOUS 플래그 사용
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define B 1024
#define MB (B * B)
#define GB (MB * B)
int main(int argc, char *argv[])
{
int* ptr, i;
ptr = mmap(NULL, 4L * GB, PROT_READ | PROT_WRITE, MAP_PRIVATE |
MAP_ANONYMOUS, -1, 0);
if(ptr == MAP_FAILED){
perror("Error mapping memory");
return 1;
}
printf("4GB Allocated\n");
sleep(60);
return 0;
}
- free memory의 양에 변화 가 없다
- free 명령을 통해 free 메모리의 감소 확인
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define B 1024
#define MB (B * B)
#define GB (MB * B)
int main(int argc, char *argv[])
{
int* ptr, i;
ptr = mmap(NULL, 4L * GB, PROT_READ | PROT_WRITE, MAP_PRIVATE |
MAP_ANONYMOUS, -1, 0);
if(ptr == MAP_FAILED){
perror("Error mapping memory");
return 1;
}
printf("4GB Allocated!\n");
if(mlock(ptr, 1L * GB)<0){
perror("Error mlock memory");
return 1;
}
printf("1GB pinned!\n");
sleep(60);
if(munlock(ptr, 1L * GB)<0){
perror("Error unlock memory");
return 1;
}
munmap(ptr, 4L * GB);
return 0;
}
- MAD_DONTNEED 사용
- free 명령어를 통해 free 메모리의 증가 확인
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define B 1024
#define MB (B * B)
#define GB (MB * B)
int main(int argc, char *argv[])
{
int* ptr, i;
ptr = mmap(NULL, 4L * GB, PROT_READ | PROT_WRITE, MAP_PRIVATE |
MAP_ANONYMOUS, -1, 0);
if(ptr == MAP_FAILED){
perror("Error mapping memory");
return 1;
}
printf("4GB Allocated!\n");
if(mlock(ptr, 1L * GB)<0){
perror("Error mlock memory");
return 1;
}
printf("1GB pinned!\n");
if(madvise(ptr, 512L * MB, MAD_DONTNEED) < 0) {
perror("failed to advise memory");
return 1;
}
printf("512MB dont't need\n");
sleep(60);
if(munlock(ptr, 512L * MB) < 0) {
perror("failed to unlock memory");
return 1;
}
munmap(ptr, 4L * GB);
return 0;
}
-MAD_DONTNEED 가 안됨..