Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Memory Leak when displaying sprites?

Discussion in 'Scripting' started by Snicky, Aug 3, 2015.

  1. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    Hello, again here in need for advice.
    This function is loading my memory:
    Code (CSharp):
    1. public List<MyBaseCompanion> creatures;
    2. public SpriteRenderer C1;
    3. public SpriteRenderer C2;
    4. public SpriteRenderer C3;
    5.  
    6. public void  DisplayCreature(){
    7.             if (level <= 0) {
    8.             level = 1;
    9.         }else {
    10.             int temp1 =Random.Range ((int)(level / 10)*10, (int)(level/10+1)*10);
    11.             int temp2 =Random.Range ((int)(level / 10)*10, (int)(level/10+1)*10);
    12.             int temp3 =Random.Range ((int)(level / 10)*10, (int)(level/10+1)*10);
    13.    
    14.             C1.sprite = creatures [temp1].StandingSprite as Sprite;
    15.             C2.sprite = creatures [temp2].StandingSprite as Sprite;
    16.             C3.sprite = creatures [temp3].StandingSprite as Sprite;
    17.             }
    MyBaseCompanion is ScriptableObject and holds my references to my Sprites.
    When I call this function often it basicly loads all sprites into memory (+300 mb). ( At least thats what im thinking based on researching)
    Threads about asset managment and garbage collection left me confused wheter im on the right track or not.
    So if someone could hint me what methods or system come in handy for my poblem I will gladly look into them.
    Thank you.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'm confused, all the "database" scriptable objects I've seen (and I'm fairly new to them) are a single object that contains a list to store the data and a bunch of accessor functions... if that's the case why are you setting up a list of scriptable objects like that? :confused:
     
  3. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    because I havent seen code where they where used as single object, containing a List. I am also new to them and in general programming.They were recommended to me for my task. How to utulize them best I couldnt possibly know right.
    So I wanted to make each creature and companion a single script and then add them if known/needed which occured legit to me .Gave them an Id number and wrote a function that sorts them to the right index , to access them. Seemed natural to me and it works just fine for me.
    But thats a lil bit offtopic.I added that as Info as it might be related, but I think its not.
    My concern is that when I render a sprite1 it will be load into RAM. And when I now load sprite2 in the renderer it will again add to memory without realising the unused space of sprite1. So when I swapp trough 100 sprites I have a huge usage of RAM and I basicly wanna know how to free unused memory after changing sprite.Cannot find statisfying Info that address my problem.:(
     
  4. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Can you post your companion class and btw you can remove (level /10)*10
     
  5. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyBaseCompanion : ScriptableObject {
    5.  
    6.     public int ID;
    7.     public string Name;
    8.     public string Description;
    9.     public Sprite StandingSprite;
    10.     public Sprite faceSprite;
    11.     public Sprite attackSprite;
    12.  
    13.     public float DmgRatio;
    14.     public float SpeedRatio;
    15.        
    16.     public NewBehaviourScript ActiveSkill;
    17.     public NewBehaviourScript PassiveSkill;
    18.        
    19.     }
     
  6. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    You can replicate this problem by simply putting a sprite into your scene and then swap Sprites in Spriterenderer in playmode.Must be different Sprites of course.Adds up if you have many.
     
  7. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    I just have to guess but what happen with the data when you do a simple

    C1 = new sprite renderer; and so on before adding a new sprite.

    I did some google search but can't find a dispose function for sprite renderer
     
  8. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    When I new them I lose the reference to the gameobject they where on. NullReferenceException.
     
  9. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Sorry only idea I have for now is to destroy/new the old one in some intervals and I can't tell if it will work :)
     
    Snicky likes this.
  10. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    I will try that, thanks so far :D
     
  11. Snicky

    Snicky

    Joined:
    Mar 5, 2015
    Posts:
    36
    Code (CSharp):
    1. public class LoadRespurces : MonoBehaviour {
    2.     int i = 150;
    3.     public SpriteRenderer untzi;
    4.     public void LoadMySprites(){
    5.         i++;
    6.         untzi.sprite = Resources.Load ("character_" + i,typeof(Sprite)) as Sprite;
    7.         Resources.UnloadUnusedAssets ();
    8.     }
    I tried now to load them from resource and then call UnloadUnusedAssets();
    I monitored RAM and it didnt add up memory as oposed when you simply pass a Reference in the spriterenderer.

    Guess for a large quantity of sprites/images you should rather use Resource as it offers of a method to unload them from your RAM.
    Somebody pls correct me there if I talk nonsence.:p