7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

物理メモリの使用状況を取得 - vm_statistics

Last updated at Posted at 2012-06-19

例によって例の如く,無責任かつ雑なので参考程度に.
もう少しサクっと取得できないものだろうか.vm_stat をパースする以外の手法で!

vmstatistics.c
#include <stdio.h>
#include <stdlib.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>

#define MB  / 1024 / 1024.0
#define GB  MB / 1024.0

int main(int argc, char **argv)
{
  struct vm_statistics   vm_stat;
  struct host_basic_info host_info;

  host_priv_t        host_priv = mach_host_self();
  mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
  kern_return_t            err = KERN_SUCCESS;

  err = host_statistics(host_priv, HOST_VM_INFO, (host_info_t)&vm_stat, &count);

  if( err != KERN_SUCCESS ) { 
    perror("host_statistics");
    exit(err);
  }

  double free_size     = (vm_stat.free_count     * vm_page_size) GB; 
  double active_size   = (vm_stat.active_count   * vm_page_size) GB; 
  double inactive_size = (vm_stat.inactive_count * vm_page_size) GB; 
  double wire_size     = (vm_stat.wire_count     * vm_page_size) GB; 

  printf("page size  ; %ld bytes\n", vm_page_size);
  printf("free       ; %lf GB\n",    free_size);
  printf("active     ; %lf GB\n",    active_size);
  printf("inactive   ; %lf GB\n",    inactive_size);
  printf("wire       ; %lf GB\n",    wire_size);
  printf("total      ; %lf GB\n",    free_size + active_size + inactive_size + wire_size);

  return 0;
}
% clang vmstatistics.c -o vmstatistics && ./vmstatistics
page size  ; 4096 bytes
free       ; 4.442173 GB
active     ; 1.930828 GB
inactive   ; 0.878677 GB
wire       ; 0.746510 GB
total      ; 7.998188 GB
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?