简体   繁体   中英

can calloc or malloc be used to allocate ONLY physical memory in OSX?

I am playing around with the c functions malloc and calloc and I have some questions.

I want to see if I can use these 2 functions to allocate only physical memory, my mac has 4gb or ram and when I use malloc I can allocate way more than 4gb, which means malloc allocate both physical and virtual memory.

I have a couple of questions:

  1. is there any function I can use, so that I can only allocate the physical memory (w/o allocating the virtual mem)

  2. when calling malloc and calloc and when the pointers return, is there any way I can use the pointers to determine how much physical memory are allocated and how much virtual memory are allocate?

a quick example will be really appreciated :)

thanks for your help :)

Short answer: no and no.

Long answer: "virtual memory" doesn't mean it's stored on disk, it means its actual location is abstracted away, so your program can't tell where it's actually stored. This means the VM system can move the "same" memory around to optimize the overall memory usage of the computer. It's entirely normal for a page of virtual memory to be initially stored in RAM, then (if it isn't used for a while) paged out to disk to make room for something else, then (when it's actually accessed) paged back into RAM, then back out to disk, then back into RAM, etc.

Userspace programs always access all memory through this abstraction layer; that is, your program cannot allocate or access anything except virtual memory.

There is a class of memory called "wired". It's still virtual, but the paging policy doesn't allow it to be moved out to disk. Your program cannot allocate this; only the kernel can.

I recommend reading the Apple developer site's discussion of memory management for more details.

Yes you can use mlock() to keep an area of memory only in RAM avoiding it to be paged out. It is usually used for encryption to avoid keys to go on the disk with the danger of being retrieved by attackers afterwards.

Of course the amount of memory you can lock in RAM is limited. It will still be virtual memory but will not go to the disk, which i guess is what you want.

malloc/calloc are libc wrappers around the OS real call for allocating memory (and i have no clue of what that might be on OSX), and they keep internal buffers to avoid too frequent system calls. You should see your libc implementation of these calls and see where they keep the data and in which format, so you can access it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM