ios - 如何在iOS中将NSObject分配到其自己的虚拟内存页面上?

标签 ios memory memory-management allocation virtual-memory

我想在自己的虚拟内存页面上分配一个NSObject。这可能吗?如果我们仍然可以使用NSZone,这似乎很琐碎,但已弃用了它们。我尝试使用NSZoneFromPointer,但始终返回nil。根据Tips for Allocating Memory:

对于较大的内存分配,其中的大小不止几个
虚拟内存页面,malloc自动使用vm_allocate
例程获取请求的内存。

因此,看来我应该能够使我的对象像这样大到几页:

@interface MyObject : NSObject {
int[40960]; // 4096 is the default page size, so this is 10 pages.
}

@implementation MyObject
@end

我知道这是一个hack,但是会持续工作吗?有没有更好的办法?

最佳答案

twitter:

objc_constructInstance(…)可以让您做您想做的...

<objc/runtime.h>:

/** 
 * Creates an instance of a class at the specific location provided.
 * 
 * @param cls The class that you wish to allocate an instance of.
 * @param bytes The location at which to allocate an instance of \e cls.
 *  Must point to at least \c class_getInstanceSize(cls) bytes of well-aligned,
 *  zero-filled memory.
 *
 * @return \e bytes on success, \c nil otherwise. (For example, \e cls or \e bytes
 *  might be \c nil)
 *
 * @see class_createInstance
 */
OBJC_EXPORT id objc_constructInstance(Class cls, void *bytes) 
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0)
    OBJC_ARC_UNAVAILABLE;

看来objc_constructInstance是答案。

关于ios - 如何在iOS中将NSObject分配到其自己的虚拟内存页面上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362430/

相关文章:

ios - 谷歌API PHP : Wrong audience in jwt for iOS only

JAVA GC : ParNew (promotion failed) , 并发模式失败

.net - 新 .Net 流程的成本

c - 无法从结构数组中释放内存,valgrind 显示内存泄漏

c - 释放指向指针结构的指针

ios - 单击时更新标签

ios - 为什么这个 Objective-C Block 会泄漏?

ruby-on-rails - Ruby 中的内存模型

ios - 平滑地加速正在运行的旋转动画

objective-c - 从同一个方法中调用一个方法会造成内存泄漏吗?