Author Topic: Use of "foreach" loops in NGUI  (Read 15510 times)

Ironic Chef

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Use of "foreach" loops in NGUI
« on: June 27, 2015, 10:01:24 PM »
Hey there!

There's a long-standing and well documented bug in the version of Mono that Unity 4.x is using which results in a memory leak in "foreach" loops (as I understand it, each iteration burns 24 bytes of RAM). I've been cleaning up my app to eliminate those leaks, but there seem to be a few in the NGUI code. While I'm not concerned by memory leaks in the editor tools, it'd be awesome if any foreach loops in NGUI's runtime code could be addressed.

While this may be a non-issue with Unity's switch to IL2CPP, it remains a problem for apps that are locked to a pre-Unity5 dev environment (to be fair, I'm not even sure that Unity5 has actually switched to the IL2CPP system - I'm locked at version 4.6.x for my current project, and it's not clear if they've actually "pulled the trigger" on that change).

I can certainly tweak the version of NGUI I'm using to remove the foreach loops, but it would be more optimal if they were removed from the NGUI distribution. My understanding is that foreach is less efficient than simple "for" loops, so swapping them out might also provide a small performance improvement.

Just a suggestion - if it's something do-able then that would be fantastic. If not, I can do my best to sort it out.

An FYI - I just want to say that NGUI is pretty freakin' amazing. It's not easy to sort out initially, but once you understand the relationships between panels, depths, and so on, it's a near perfect toolkit for building GUIs. It's awesome!

                    Nick

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
Re: Use of "foreach" loops in NGUI
« Reply #1 on: June 28, 2015, 01:34:58 PM »
Can you post a link to the research on this? As far as I can tell, the memory "leak" is by design. It can be avoided by using struct based enumerators, but then again...  garbage collection for a class-based enumerator isn't going to be that big a deal unless its done as part of your Update calls.

If the GC issue is what you're speaking of, that is not a leak. Though I'm just assuming its that since there's no real hits for an actual leak when searching for the issue.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Use of "foreach" loops in NGUI
« Reply #2 on: June 28, 2015, 03:14:55 PM »
I seem to remember a similar finding. As far as I remember, certain foreach loops instantiate an enumerator which leaves those 24 bytes for garbage collection.

Here's some info on it http://stackoverflow.com/questions/18718399/every-iteration-of-every-foreach-loop-generated-24-bytes-of-garbage-memory

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
Re: Use of "foreach" loops in NGUI
« Reply #3 on: June 28, 2015, 07:26:28 PM »
Yeah thats basically what I was finding too, Nicki. Though the OP should know that a memory leak is BUG that is NOT by design. The phenomenon happening with foreach is not a bug, and it is by design.

Games are about garbage collection far more than normal apps do. So yeah its a bigger issue, sure. Still not a bug though.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Use of "foreach" loops in NGUI
« Reply #4 on: June 29, 2015, 04:25:03 AM »
Splitting hairs, I think. It leaks, that should be it. :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Use of "foreach" loops in NGUI
« Reply #5 on: June 29, 2015, 07:26:06 AM »
NGUI doesn't use any foreach loops in real-time code. Just editor code, and a few places where it cannot be avoided, such as iterating through an Animation -- and these pieces of code are rarely used.

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
Re: Use of "foreach" loops in NGUI
« Reply #6 on: June 29, 2015, 03:36:51 PM »
Nicki, the reason I think its important to maintain the distinction is that a leak is a bug and very possibly wont be noticed by the garbage collector - so it'll eventually cause a crash.

Whereas this phenomenon will, at worst, cause a tiny bit more work for the garbage collector.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Use of "foreach" loops in NGUI
« Reply #7 on: July 01, 2015, 02:19:25 PM »
@wisteso: Sure, that is an important distinction. Such a leak is almost impossible to make in C#/Unity though, so when we say "leak" it generally means "generating garbage memory". I see your point, though.