Linux 获取内存页大小

Linux 获取内存页大小
如何查看Linux系统的内存页大小:
1. getconf PAGESIZE/PAGE_SIZE 返回值单位为Bytes

点击(此处)折叠或打开

  1. [root@localhost nginx-1.8.0]# getconf PAGESIZE
  2. 4096
  3. [root@localhost nginx-1.8.0]# getconf PAGE_SIZE
  4. 4096

2. 通过c函数获取
#include
int getpagesize(void);返回值单位为Bytes


点击(此处)折叠或打开

  1. The function getpagesize() returns the number of bytes in a page, where a "page" is the thing used where it says in the description of mmap(2) that files are mapped in page-sized units.

  2. The size of the kind of pages that mmap() uses, is found using

  3. #include <unistd.h>
  4. long sz = sysconf(_SC_PAGESIZE);

  5. (where some systems also allow the synonym _SC_PAGE_SIZE for _SC_PAGESIZE), or

  6. #include <unistd.h>
  7. int sz = getpagesize()

pagesize.c

点击(此处)折叠或打开

  1. [root@localhost test]# more pagesize.c
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. int main(int argc, char *argv[])
  5. {
  6. printf("linux page size is %d bytes",getpagesize());
  7. return 0;
  8. }

编译运行:

点击(此处)折叠或打开

  1. [root@localhost test]# gcc -g pagesize.c -o pagesize -Wall
  2. [root@localhost test]# ./pagesize
  3. linux page size is4096 bytes

推荐阅读