Swift的UnsafeMutablePointer< Float> .allocate(...)是否实际分配内存? [英] Does Swift's UnsafeMutablePointer<Float>.allocate(...) actually allocate memory?

查看:232
本文介绍了Swift的UnsafeMutablePointer< Float> .allocate(...)是否实际分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图了解Swift的不安全指针API,以处理音频样本.

I'm trying to understand Swift's unsafe pointer API for the purpose of manipulating audio samples.

非可变指针变量(UnsafePointer,UnsafeRawPointer,UnsafeBufferPointer)对我来说很有意义,它们都用于以只读方式引用先前分配的内存区域.这些变体没有类型方法分配"

The non-mutable pointer variants (UnsafePointer, UnsafeRawPointer, UnsafeBufferPointer) make sense to me, they are all used to reference previously allocated regions of memory on a read-only basis. There is no type method "allocate" for these variants

但是,可变变量(UnsafeMutablePointer,UnsafeMutableRawPointer)被记录为实际上在分配基础内存.来自UnsafeMutablePointer的文档中的示例(此处):

The mutable variants (UnsafeMutablePointer, UnsafeMutableRawPointer), however, are documented as actually allocating the underlying memory. Example from the documentation for UnsafeMutablePointer (here):

静态函数分配(容量:整数)

static func allocate(capacity: Int)

为指定数量的Pointee类型的实例分配未初始化的内存

Allocates uninitialized memory for the specified number of instances of type Pointee

但是,没有提到UnsafeMutablePointer.allocate(size)可能会失败,因此实际上无法分配内存.反之,如果确实分配了实际内存,您怎么知道它是否失败了?

However, there is no mention that the UnsafeMutablePointer.allocate(size) can fail so it cannot be actually allocating memory. Conversely, if it does allocate actual memory, how can you tell if it failed?

任何见解都会受到赞赏.

Any insights would be appreciated.

推荐答案

我决定对此进行测试.我在CodeRunner中运行了该程序:

I decided to test this. I ran this program in CodeRunner:

import Foundation

sleep(10)

在执行sleep函数时,CodeRunner报告说这占用了我计算机上5.6 MB的RAM,成为我们的基准.

While the sleep function was executing, CodeRunner reported that this was taking 5.6 MB of RAM on my machine, making our baseline.

然后我尝试了该程序:

import Foundation

for _ in 0..<1000000 {
    let ptr = UnsafeMutablePointer<Float>.allocate(capacity: 1)
}

sleep(10)

现在,CodeRunner报告了5.8 MB的RAM使用量.比以前多了一点,但肯定不是应该占用的额外4 MB.

Now, CodeRunner reports 5.8 MB of RAM usage. A little more than before, but certainly not the extra 4 MB that this should have taken up.

最后,我给指针分配了一些东西:

Finally, I assigned something to the pointer:

import Foundation

for _ in 0..<1000000 {
    let ptr = UnsafeMutablePointer<Float>.allocate(capacity: 1)
    ptr.pointee = 0
}

sleep(10)

突然,该程序占用了21.5 MB的RAM,最终使我们的预期RAM使用量增加了,尽管数量超出了我的预期.

Suddenly, the program is taking up 21.5 MB of RAM, finally giving us our expected RAM usage increase, although by a larger amount than what I was expecting.

在CodeRunner中创建配置文件以编译启用的优化似乎并没有改变我所看到的行为.

Making a profile in CodeRunner to compile with the optimizations turned on did not seem to make a difference in the behavior I was seeing.

因此,令人惊讶的是,确实出现了对UnsafeMutablePointer.allocate的调用实际上并没有立即分配内存.

So, surprisingly enough, it does appear that the call to UnsafeMutablePointer.allocate actually does not immediately allocate memory.

这篇关于Swift的UnsafeMutablePointer&lt; Float&gt; .allocate(...)是否实际分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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