简体   繁体   English

calloc或malloc可用于在OSX中仅分配物理内存吗?

[英]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. 我在玩c函数malloc和calloc,我有一些问题。

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. 我想看看是否可以使用这2个功能来仅分配物理内存,但我的Mac具有4gb或ram,当我使用malloc时,我可以分配的方式大于4gb,这意味着malloc可以分配物理内存和虚拟内存。

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? 当调用malloc和calloc以及指针返回时,有什么方法可以使用指针来确定分配了多少物理内存以及分配了多少虚拟内存?

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. 这意味着VM系统可以移动“相同”内存,以优化计算机的整体内存使用率。 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. 最初将虚拟内存页面最初存储在RAM中,然后(如果一段时间不使用)将页面调出到磁盘以腾出空间以容纳其他东西,然后(当实际访问时)将页面调回到RAM中,这是完全正常的。 ,然后退回磁盘,然后退回RAM等。

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. 我建议阅读Apple开发人员站点有关内存管理讨论以获取更多详细信息。

Yes you can use mlock() to keep an area of memory only in RAM avoiding it to be paged out. 是的,您可以使用mlock()仅在RAM中保留一个内存区域,以避免将其分页。 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. 当然,您可以锁定在RAM中的内存数量是有限的。 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. malloc / calloc是围绕OS进行内存分配的真实调用的libc包装器(并且我不知道OSX上可能发生了什么),并且它们保留内部缓冲区以避免太频繁的系统调用。 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. 您应该看到这些调用的libc实现,并查看它们在何处保存数据以及采用哪种格式,以便您可以访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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