- Home /
Assets never unloading after loading next scene
I'm having an issue in an iOS app where my assets for each different scene are not being unloaded once I load another scene. Here's some background on what I'm doing:
I was having memory issues when loading a scene with Application.LoadLevel(), so I changed to loading a blank scene between each level and using Application.LoadLevelAdditively() as discussed in this topic. This helped and I'm able to actually load the scenes now on an iPad1, however I noticed after loading a few scenes I was receiving the low memory warning again. Come to find out my loaded object count was never dropping based on this printout from XCode:
Unloading 18 unused Assets to reduce memory usage. Loaded Objects now: 221.
This amount steadily increases as I change scenes, and rarely removes any unused assets. These scenes share very few common assets, so nearly all of them should be being restored after I change scenes, or at least I assumed they should.
I'm using SpriteManager1 in this project to manage/draw sprites and I've heard of a few others stating they had issues releasing assets from SpriteManager. However, I have several other assets (Camera objects, GameObjects with just scripts attached, etc) that are persisting through these scenes as well, so mine issue seems to be related to every Object in my scenes.
I'm also monitoring how much memory I'm using through a simple prinout in objective-c by using:
- (void) report_memory:(NSTimer *)timer {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
//NSLog(@"Memory in use (in bytes): %u", info.resident_size);
printf_console("Memory in use (in bytes): %u\n", info.resident_size);
} else {
//NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
printf_console("Error with task_info(): %s\n", mach_error_string(kerr));
}
}
I print this information every 2 seconds just to quickly gauge how much memory my scenes are requiring. Of course, this increases in size as my assets continue to pile up and never unload as I change scenes.
I've tried manually calling the garbage collector as I load levels and I call Resources.UnloadUnusedAssets() during my blank scene just to be sure we're trying to clear the unused assets, but these proved unsuccessful.
I'm using the Unity Pro version 3.4.2 with the iOS Pro license if that makes any difference.
Thanks
Sounds like there are other objects that are holding references to those that are supposed to be unloaded. $$anonymous$$aybe you have a level engine or similar stuff that keeps them within an array?
Answer by Rod-Green · Nov 18, 2011 at 07:19 PM
Do you have any "#if"s i.e "#if UNITY_ENGINE" around any serialized properties? I found this causes unpredictable asset retention.
You could also do a find query in the blank level to see what behaviors are remaining.
FindSceneObjectsOfType(typeof(Behavior));
Then see which of these might be retaining connections.
I did have quite a few #if PLATFOR$$anonymous$$ checks in my code, I commented those out to see if it would make a difference but unfortunately it did not.
I've printed what assets still exist between level loads ($$anonymous$$eshes, $$anonymous$$aterials, scripts, etc) and it just doesn't seem to be deleting any of them. I am still looking through these for now though to see if any of these objects, for whatever reason, would be holding their references between loading these scenes.
What about hideflags? Are they set to 'don't save'? Anything set to 'dontdestroyonload' ?