我有以下情况: 我从Xcode在IPad上运行我的应用程序。 Unity版本为4.3.1 Pro。 使用以下工具监视内存: Xcode Debug Navigator Xcode Instruments Unity Profiler
我执行以下步骤: 0) Unity从空场景开始。 使用的内存 XCODE Debug Navigator: 11 mb Instruments: 26.4 mb Unity Profiler: used total 13.2 reserved total 13.7
1) 从资源文件夹中加载预制件。
prefab = Resources.Load("Room1");
XCODE调试导航器使用内存:30.8 mb,Instruments使用内存:49.54 mb,Unity Profiler使用内存:已用总量19.8,保留总量20.4
2)实例化预设
go = (GameObject)Instantiate(prefab);
go.name = "Room1";
XCODE调试导航器: 31.2兆字节 仪器: 50兆字节 Unity分析器: 使用总计20个保留总计20.7
3) 销毁场景上的所有对象 - 内存未改变
Transform[] tr = FindObjectsOfType<Transform>();
for (int i = 0; i < tr.Length; i++)
{
GameObject goo = tr.gameObject;
if (goo.name != "Main Camera")
{
Destroy(goo);
goo = null;
}
}
XCODE Debug Navigator: 31.1 MB Instruments: 49.93 MB Unity Profiler: used total 19.8 reserved total 20.4
4) 调用 Resources.UnloadUnusedAssets();
- 内存没有改变
5) 调用 System.GC.Collect();
- 内存没有改变
6) 对预制体调用 UnloadAsset:Resources.UnloadAsset(prefab);
- 内存没有改变
7) 调用 Resources.UnloadUnusedAssets();
- 内存部分清理
XCODE Debug Navigator: 21.9 MB Instruments: 40.79 MB Unity Profiler: used total 13.2 reserved total 13.9
在分析器中,我看到删除了所有在预制体中使用的纹理
8) System.GC.Collect();
- 内存没有改变
9) 加载空场景 - 内存没有改变
这里有另一个有趣的时刻:
当应用程序进入后台并启动另一个应用程序时,已使用的RAM大小大大减小,当我调用Unity应用程序时,内存返回到带有空场景的第一个大小。
我的问题如下:
1)为什么在删除预制体并调用UnloadUnusedAssets后内存没有完全清除 - 在Instruments和Xcode中可以看到,但在Profiler中我们看到内存几乎完全释放了?
2)清理内存回到初始大小是真实的吗?
3)我所有的步骤都正确吗,还是我做错了什么?
您可以在此处下载测试项目: http://gfile.ru/aa5on
非常感谢您的回复。