C # coprocessor Waitforseconds generating GC (garbage Collection) problem

Source: Internet
Author: User

Sun Guangdong 2015.4.9 24:00

Let's take a look at the role of the use of the association a total of two points:
1) delay (wait) a period of time to execute code;
2) Wait for an operation to complete before executing the subsequent code. Summing up is a sentence: control code at a specific time to execute.
The process is not a thread, nor is it executed asynchronously. The Monobehaviour and the update function are also executed in Mainthread. Using the coprocessor you don't have to worry about syncing and locking issues.


It is not recommended to use the process to bring GC problems!.

Using the game I previously encapsulated simple control logic clock class can be perfectly solved

IEnumerator Myawesomecoroutine () {    while (true)    {        doawesomestuff ();        Yield return new waitforseconds (waitTime);    


What I want to point out is that using "yield return new Waitforseconds ()" will cause the garbage allocation gc,21 bytes per frame, because the "new" section (relative to the standard coprocessor "yield return null" yields only 9 bytes).

To avoid this problem, just set your wait wait time in advance ...

Waitforseconds shortwait = new Waitforseconds (0.1f); Waitforseconds longwait = new Waitforseconds (5.0f), IEnumerator Myevenawesomercoroutine () {while    (true)    {        if (ineedtodostufffast)        {            doawesomestuffreallyfast ();            Yield return shortwait;        }        else{            Dontdomuch ();            Yield return longwait;}}    }


Now you coroutine each call will only cause the lowest 9 bytes of GC allocations (not including other allocations allocations, of course you may be led through your other code!). )。


The list of things you can do to prevent GC may be longer.
-Do not use the string invoke or Startcoroutine.

-Do not use Guilayout and mark your GUI Monobehaviour to prevent a GC of 800bytes per frame from occurring. Http://docs.unity3d.com/ScriptReference/MonoBehaviour-useGUILayout.html

-Do not use Gameobject.tag or gameobject.name

-Do not use getcomponent in update, cache it if possible


-Do not use foreach

-Do not use string +----"Stringbuild or String. Format ()



??

C # coprocessor Waitforseconds generating GC (garbage Collection) problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.