GC.Collect 21ms?

Hello,

Here my script:

public Image myimg;

void Update(){
myimg.fillAmount -= 1f/30f*Time.deltaTime;
}

What do you think about this? What is the problem with fillAmount? How can I fix? Thanks

Well, changing fillAmount will cause a recreation of the internal Mesh. For that the Image class creates a temporary instance of the VertexHelper class which has quite a few generic Lists to build the actual mesh. Since you change fillAmount every frame you get quite a bit of garbage each frame.

How large is your Image? Are you sure you need to update it every frame? It seems you only change it by a tiny amount each frame.

As SciGuy2000 said you did not provide much useful information. That tiny strip from the profiler is pretty useless. How often does the GC has such spikes?

How to solve it? Either

  • use a smaller image
  • get a better PC
  • Call GC.Collect manually more frequently to make the spikes smaller

The third point generally makes the overall performance worse but can reduce large spikes.