Core Data Memory Management - Reducing Memory Overhead

使用Core Data 中经常遇到的一个很重要的问题就是内存问题,因此需要很谨慎地考虑内存问题,否则在工程规模比较大或者操作大量数据的时候就显得尤为重要,下面是Apple官方文档的说明。

OverView

It is sometimes the case that you want to use managed objects on a temporary basis, for example to calculate an average value for a particular attribute. This causes your object graph, and memory consumption, to grow. You can reduce the memory overhead by re-faulting individual managed objects that you no longer need, or you can reset a managed object context to clear an entire object graph. You can also use patterns that apply to Cocoa programming in general.

1、You can re-fault an individual managed object using NSManagedObjectContext‘s refreshObject:mergeChanges: method. This has the effect of clearing its in-memory property values thereby reducing its memory overhead. (Note that this is the same as setting the property values to nil - the values will be retrieved on demand if the fault is fired).

2、When you create a fetch request you can set includesPropertyValues to NO to reduce memory overhead by avoiding creation of objects to represent the property values. You should typically only do so, however, if you are sure that either you will not need the actual property data or you already have the information in the row cache, otherwise you will incur multiple trips to the persistent store.

3、You can use the reset method of NSManagedObjectContext to remove all managed objects associated with a context and "start over" as if you‘d just created it. Note that any managed object associated with that context will be invalidated, and so you will need to discard any referenes to and re-fetch any objects associated with that context in which you are still interested.

4、If you iterate over a lot of objects, you may need to use local autorelease pool blocks to ensure temporary objects are deallocated as soon as possible.

5、If you do not intend to use Core Data‘s undo functionality, you can reduce your application‘s resource requirements by setting the context‘s undo manager to nil. This may be especially beneficial for background worker threads, as well as for large import or batch operations.

6、Finally, Core Data does not by default keep strong references to managed objects (unless they have unsaved changes). If you have lots of objects in memory, you should determine the owning references. Managed objects maintain strong references to each other through relationships, which can easily create strong reference cycles. You can break cycles by re-faulting objects (again by using the refreshObject:mergeChanges: method of NSManagedObjectContext).

时间: 2024-10-15 02:12:01

Core Data Memory Management - Reducing Memory Overhead的相关文章

Automatic Tuning of Memory Management

4.2.2 Automatic Tuning of Memory Management Two memory management initialization parameters, MEMORY_TARGET and MEMORY_MAX_TARGET, enable automatic management of the System Global Area (SGA), Program Global Area (PGA), and other memory required to run

《modern operating system》 chapter 3 MEMORY MANAGEMENT 笔记

MEMORY MANAGEMENT The part of the operating system that manages (part of) the memory hierarchy is called thememory manager 这章感觉有点多...80 多页..看完都看了两天多,做笔记就更有点不想...有点懒了..但是要坚持下去,可以自己较劲 对于内存的抽象,最简单的抽象就是...没有抽象 和第一次看不一样,把summary放在最前面,对整个mamory management的

Android Memory Management, OutOfMemoryError

A Android框架强制每个进程的24 MB内存限制.在一些旧的设备,如在G1,限制为16 MB 更低,更重要的是,由位图使用的内存限制.处理图像的应用程序,它是很容易达到此限制,并获得与OOM 异常死亡 的过程:E / dalvikvm堆(12517):1048576字节外部分配这个 过程中过大的E / GraphicsJNI(12517): VM将不会让我们分配1048576字节 / AndroidRuntime(12517):关闭VM / dalvikvm(12517):主题ID = 1

dx12 memory management

https://msdn.microsoft.com/en-us/library/windows/desktop/dn508285(v=vs.85).aspx Map with D3D11_MAP_WRITE_DISCARD, the runtime returns a pointer to a new region of memory instead of the old buffer data. This allows the GPU to continue using the old da

Fixed Partition Memory Management UVALive - 2238 建图很巧妙 km算法左右顶点个数不等模板以及需要注意的问题 求最小权匹配

/** 题目: Fixed Partition Memory Management UVALive - 2238 链接:https://vjudge.net/problem/UVALive-2238 题意:lv 思路:lrjP352. 来自lrj训练指南. n个程序作为左边结点, n*m个结点在右边:由于只要求n个程序在右边能找到的匹配点,km算法可以求解.修改nx,ny的值. if(f[i][j]==-1){ for(int k = 1; k <= n; k++) love[i][j*n+k-

Introduction to C Memory Management and C++ Object-Oriented Programming

C is used when at least one of the following matters: Speed Memory Low-level features(moving the stack pointer, etc.). Level of abstraction Languages Directly manipulate memory Assembly(x86,MIPS) Access to memory C,C++ Memory managed Java,C#,Scheme/L

Objective -C Memory Management 内存管理 第一部分

Objective -C Memory Management??内存管理??第一部分 Memory management is part of a more general problem in programming called resource management. 内存管理是资源管理的一部分. Every computer system has finite resources for your program to use. These include memory, open fi

2015.12.21 内存管理(memory management)

Memory Management 1.什么是内存管理? 程序在运行过程中管理内存分配的过程,当需要内存的时候就申请一片内存空间,不需要就释放掉. 2.如何去管理内存 站在分配对象拥有权的角度来操作内存. 3.内存管理的两种办法 a. MRR(Manual Retain Release)手动管理,实现的机制:reference counting(引用计数机制). b. ARC(Auto Reference Counting)自动引用计数,实现机制:系统在程序编译阶段自动添加了释放对象的办法. 4

Objective-C Memory Management Being Exceptional 异常处理与内存

Objective-C Memory Management ? ?Being Exceptional ?异常处理与内存 3.1Cocoa requires that all exceptions must be of type NSException cocoa?需要所有的异常是NSException类型的. so even though you can throw an exception from other objects, Cocoa isn't set up to deal with