NSMutableData如何分配内存? [英] How does NSMutableData allocate memory?

查看:141
本文介绍了NSMutableData如何分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,它会慢慢地消耗我的记忆,甚至开始使用掉期:

When I run the following code, it slowly eats up my memory and even starts using swap:

 long long length = 1024ull * 1024ull * 1024ull * 2ull; // 2 GB

 db = [NSMutableData dataWithLength:length];

 char *array = [db mutableBytes];

 for(long long i = 0; i < length - 1; i++) {
      array[i] = i % 256;
 }

如果我运行它没有循环,根本没有使用内存: / p>

If I run it without the for cycle no memory is used at all:

 long long length = 1024ull * 1024ull * 1024ull * 2ull;
 db = [NSMutableData dataWithLength:length];
 char *array = [db mutableBytes];
 /* for(long long i = 0; i < length - 1; i++) {
      array[i] = i % 256;
 } */

我只能得出结论,NSMutableData只是保留内存,何时它被访问,那么它真的分配它。它是如何完成的?

I can only conclude that NSMutableData is only "reserving" memory and when it is accessed then it really "allocates" it. How is it done exactly?

这是通过硬件(CPU)完成的吗?

Is this done via hardware (CPU)?

NSMutableData是否有一种方法来捕获其保留内存中的内存写入,然后再进行分配?

Is there a way for NSMutableData to catch memory writes in its "reserved" memory and only then do the "allocation"?

这是否也意味着调用 [NSMutableData dataWithLength:length] 永远不会失败?如果需要,可以使用交换机分配任何大小的内存吗?

Does this also mean that a call to [NSMutableData dataWithLength:length] can never fail? Can it allocate any size of memory using swap to get it if needed?

如果可以失败,我的db变量将为null?

If it can fail will my db variable be null?

在苹果的NSMutableData类参考中,我仅看到关于这些主题的模糊句子。

In apple's "NSMutableData Class Reference" I have seen only vague sentences about these topics.

推荐答案

不是一个NSMutableData问题,而是一个内核/操作系统问题。如果进程请求一个(大)内存块,内核通常会说没关系,这里你去。但只有在实际使用它的时候,才真正(物理地)分配。这是确定的,因为如果您的程序以2 GB的malloc开始(正如您在这里所做的那样),否则会立即推出其他程序进行交换,而在实践中,您通常不会立即使用2 GB。

This is not so much an NSMutableData issue but a kernel/OS issue. If the process requests a (big) chunk of memory, the kernel will normally just say "that's ok, here you go". But only on actually using it, it is really ("physically") allocated. This is ok since if your program starts with a 2 GB malloc (as you are doing here) it would otherwise instantly push out other programs to swap, while in practice you will often not use the 2 GB right away.

当访问物理内存中实际不存在的内存页时,内核将从CPU中获取一个信号。如果页面应该在那里(因为它在你的2 GB块内),它将被放置到位(可能是从交换),你甚至不会注意到。如果该页面不在那里(因为该地址未在虚拟内存中分配),您将收到一个分段错误(SIGSEGV或EXC_BAD_ACCESS种错误)。

When accessing a memory page that is not actually present in physical memory, the kernel will get a signal from the CPU. If the page should be there (because it is within your 2 GB chunk) it will be put in place (possibly from swap) and you will not even notice. If the page shouldn't be there (because the address is not allocated within your virtual memory) you will get a segmentation fault (SIGSEGV or EXC_BAD_ACCESS kind of error).

其中一个相关主题是过度投入(kernel),其中内核承诺比实际可用更多的内存。如果所有进程开始使用其承诺的内存,它可能会导致严重的问题。这是操作系统的依赖。

One of the related topics is "overcommit(ment)", where the kernel promises more memory than is actually available. It can cause serious problems if all processes start using their promised memory. This is OS dependent.

互联网上有很多页面更好地解释了这一点。我只是想给一个简短的介绍,所以你有条款放在谷歌。

There are a lot of pages on the internet explaining this better and in more detail; I just wanted to give a short intro so you have the terms to put in google.

编辑刚刚测试,linux将很容易答应我4 TB存储器,而 - 我向你保证 - 在该机器中,总共存储1 TB的磁盘存储空间。你可以想象,如果不照顾,在建立任务关键系统时,会引起一些困扰。

edit just tested, linux will easily promise me 4 TB of memory, while - I assure you - there is not even 1 TB of total disk storage in that machine. You can imagine that this, if not taken care of, can cause some headaches when building mission critical systems.

这篇关于NSMutableData如何分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆