Search Unity

Memory Profiler - Take Sample doesn't total up to Total Allocated.

Discussion in 'Editor & General Support' started by Zergling103, Apr 4, 2013.

  1. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    Hi there,

    I'm trying to find memory bottlenecks in our game but I'm having a tough time figuring out what to tackle.

    - Our biggest usage area, Assets, takes up 0.9 GB, yet the total allocated is 1.3 GB. Where is this extra bulk coming from?
    - What is AssetDatabase? It is the biggest consumer under Assets. If it is editor-only, as said in the documentation, should I be concerned with it?
    - This is the second time I'm asking this - How does unity manage memory taken by assets (like textures) that will not be used for long periods of time? Our game is procedural, and as a consequence it is possible for any given mesh, texture, audio clip, etc. to be used at any given time. But obviously it is not a viable option to dump every single thing into RAM upon loading the level.

    For example, Unity loads a texture for every single weapon, item, character, enemy, etc. even if the player will not encounter said objects even after hours of gameplay. For example, the player might require 10,000 metal to build a super death missile, but the player won't necessarily have those resources for a long time. Yet since the possibility of them being built is there they are ALL loaded onto memory, all at once. Is there any way to get around this? Where instead I load it into memory when I KNOW it will be used, and discard it when I KNOW it won't be?

    Perhaps more importantly - Shouldn't Unity be doing this for me? Surely it is the role of the engine to know what textures, meshes, audio clips, animations, etc. are being rendered, displayed, played, etc. at any given time. Surely it is the role of the engine to know the reference count for a given asset and to purge it from memory when it is not being referenced anymore. Surely it is the role of the engine to track when GameObjects use or stop using a particular asset, so that the reference count of a given asset can be known. Yet consistently it seems that assets that are not being used at the time are wasting space as if none of this was happening. Please tell me that I am doing something wrong, at that there is actually a simple solution for this.

    Thanks to anyone who might be able to help.
     
    Last edited: Apr 4, 2013
  2. beck

    beck

    Joined:
    Nov 23, 2010
    Posts:
    294
    You're partially right that it's the role of the engine to know when to unload assets - it knows that when it's not being referenced in code or in the scene, it doesn't need to be loaded. Unity automatically unloads unused assets when a scene is changed or when you call Resources.UnloadUnusedAssets.
    If you're referencing an asset at all, Unity can't know that you won't use the asset. The way around this is to use Resources.Load. Stick the assets which have variable accessibility and then you can be explicit about when you load them into runtime. Unfortunately, there's no magic "manage all my assets for me" solution - if you want to keep your runtime memory footprint down, you need to be explicit about loading/unloading assets. If Unity were to unload assets when you didn't ask it to, you'd see lots of runtime performance chugs - resource management should only happen when performance is non-critical, and it's up to you to define that.

    Edit: Oh, just realized this was posted in 2013, not 2014. Necro post!