Search Unity

Resources.Load used multiple times on same resources take more memory?

Discussion in 'Scripting' started by techmage, Aug 1, 2011.

  1. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Lets say you have texture1 in your resources folder.

    On one object you go Resources.Load("Texture1")

    then on another texture you Resources.Load the same texture

    and on another you Resources.Load the same texture.... on and on

    Will Unity copy the texture loaded from Resources.Load from memory each time, even if it's the same texture. Meaning, if you have already Rescources.Load'ed a texture, and you Resources.Load it again somewhere else, will unity automatically make a reference to the already loaded texture? Or will it load a copy of it into memory again?
     
  2. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    No one know?
     
  3. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    can't you just use it once and set it to a variable and use this variable as much as u want ?
    I don't have a complete idea of resources.load but it is just my opinion , I might be wrong
     
  4. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    I could... but I'm just wondering, do I need to?
     
  5. springfield2

    springfield2

    Joined:
    Nov 20, 2010
    Posts:
    27
    Great question. I would like to know this too.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    yes unity will keep reserving memory if you keep using resources load without a destroy().
     
    Midiphony-panda likes this.
  7. springfield2

    springfield2

    Joined:
    Nov 20, 2010
    Posts:
    27
    Yeah, but how about if you load the same texture twice using Resources.Load? Will it take 2x memory, even though you are loading the same texture?
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  9. springfield2

    springfield2

    Joined:
    Nov 20, 2010
    Posts:
    27
    ok. Thanks!
     
  10. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    I'm not so sure that's correct actually; watching memory usage in the profiler, I noticed absolutely no increase in memory usage when either:

    A) Adding a loaded copy of an already loaded Texture2D to a Generic List
    or
    B) Assigning a variable to a loaded copy of an already loaded Texture2D

    Code (csharp):
    1.  
    2. void LoadTextureFromResources(){
    3.         for (int i = 0; i < 100; i++){
    4.             _textureList.Add((Texture2D)Resources.Load("smoke"));
    5.             _sourceTexture = (Texture2D)Resources.Load("smoke");
    6.         }
    7.     }
    8.  
    What you do need to be care of, however, is loading different textures into the same variable. For example, let's say you have 3 textures - TextureA, TextureB, TextureC

    If you have a variable like:
    Texture2D _testVar;

    And you set it to Resources.Load(TextureA) - TextureA is loaded into memory.
    If you then set it to TextureB - a lot of people assume that TextureA will be GC'ed since there is no longer a reference to it - this is not the case. You need to manually Resources.UnloadUnusedAssets() to unload any textures that you are no longer using when appropriate.

    It's pretty easy to test the first thing though assuming you have access to the profiler.
     
    XCO and Pasixi like this.
  11. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Just as another real quick text - if you use Resources.Load() to load the same Texture2D into two different variables - and alter the Texture2D in one of the variables, it alters the Texture2D in both (so both variables are apparently pointing to the same object in memory).
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Interesting, I had different findings. Actually I think thats related to materials using the same texture but loading different materials.... need to test.