Author Topic: Any way to avoid garbage collection with BetterList AllocateMore()?  (Read 9376 times)

andreyd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
BetterList AllocateMore() creates a larger buffer every time it requires more elements than it can hold. This is a correct behavior, but unfortunately create a GC call most of the time. Is there a way to set the default size of the buffer (didn't see it anywhere) to avoid the constant resizing when my list grows? I'd like to avoid GC calls at any cost even if it mean I use more memory.

andreyd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: Any way to avoid garbage collection with BetterList AllocateMore()?
« Reply #1 on: July 07, 2014, 11:15:10 AM »
This is especially bad when number of UIWidgets grows to let's say from 255 to 256 in one UIPanel as I scroll that panel. The GC call takes about 20ms on iPad 3 which is very noticeable. The bad part is that it also trickles down to WriteToBuffers() call in UIGeometry(). Just by setting the default size of the BetterList to 256, all the calls to GC were removed. Yes, it's not the best solution, but it's the one that has significant advantage as it prevents memory fragmentation and spontaneous garbage collection calls. Any input on how to improve BetterList would be great, but for now can we just have a default value of size to be exposed somehow, so we can set it.

andreyd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: Any way to avoid garbage collection with BetterList AllocateMore()?
« Reply #2 on: July 07, 2014, 12:15:36 PM »
Of course, Trim() will now be called and do the GC call afterwards which is suboptimal.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Any way to avoid garbage collection with BetterList AllocateMore()?
« Reply #3 on: July 08, 2014, 11:00:08 AM »
You can just change the default value to 256 if you like (it's set to 32 by default). But this would just be a band-aid in your case. The allocation of memory isn't a big issue. Draw calls reuse buffers anyway. If the list gets trimmed, however, then yes next time it grows it will require reallocation. NGUI already tries to reduce trimming as much as possible as you can see in the UIDrawCall.

andreyd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: Any way to avoid garbage collection with BetterList AllocateMore()?
« Reply #4 on: July 08, 2014, 02:06:13 PM »
Hey Aren, thanks for the response.
My problem is that even with 256 as a default, I get random garbage collection calls when the better list is being resized (either inflated or trimmed). This usually happens on UIGeometry.WriteToBuffers() method. In the profiler looks like BetterList.AllocateMore() is called 36 times that frame. I can only assume what buffer copying does to memory fragmentation / garbage collection if that happens every frame. Also, do you think we can have a larger default vertex array for UILabels? We can pre-calculate the number of vertices it will have using the number of characters and any Effect (options) it has.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Any way to avoid garbage collection with BetterList AllocateMore()?
« Reply #5 on: July 09, 2014, 02:46:35 PM »
As I said, NGUI already makes some guesses, but it's never going to be possible to eliminate GCs fully. Just minimize them. You can suggest specific changes if you like -- but please test them first.