Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Sliced sprite memory bug + workaround.

Discussion in 'Editor & General Support' started by SevenPointRed, Feb 7, 2018.

  1. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    113
    I'm posting this as it caused 4 days of headaches to resolve:

    When setting the size of a sprite in sliced mode Unity allocates a new mesh, I had been doing it every frame for around 20 sprites, this was enough that the device (android) couldn't keep up with the memory allocation rate and caused an out of memory error after around 15-30min.

    This had been a huge issue for weeks, so I'm hoping someone else benefits from knowing this.

    Also Unity really shouldn't be allocating a new mesh, as the vertex/uv/triangle count would not change on a sliced sprite, all it really needs to do is update the verts. So I would consider this a bug, hopefully it's nice and easy for Unity to fix.

    Unity 2017.3.0p3

    Code (CSharp):
    1. //Progress bar.
    2. //ClampedSoft 0.1 - 1 ( 0 causes error )
    3. //Without the Approximately line it was updated every frame and caused huge memory issues. This slows it down and gives enough time for system GC to kick in, but it's not ideal that the mesh is recreated.
    4.  
    5. if (!Mathf.Approximately(ClampedSoft * background.size.x, bar.size.x))
    6. {
    7.                 bar.size = new Vector2(ClampedSoft * background.size.x, background.size.y);
    8.                 bar.enabled = bar.size.x >= 0.01f;
    9. }
     
  2. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    113
    Just bumping this so someone from unity can reply.
    It seems like using the detailed memory profiler, that the memory used by each spriterenderer also increases constantly.
     
  3. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    113
    I've created a bug report: 1006814
     
    vexe and Peter77 like this.
  4. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,665
    Hi,
    Thanks a lot for reporting this issue. We were able to reproduce it. The devs will take care of it. For future references, please don't post issues that are not specific to the beta in the beta forum.
     
    Peter77 likes this.
  5. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    113
    I was desperate for help, it's causing crashes after long gaming periods.
     
  6. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    636