Unity GC Optimization Essentials

Source: Internet
Author: User

Reference: http://blog.csdn.net/znybn1/article/details/76464896

Why the key? Because the focus of the talk.

Know?

The game runs to store data, and when the data is no longer being used, the memory that stores the data is freed to facilitate the word after the memory is reused.

Garbage (garbage) is the term used to store memory for these useless data, and GC (Garbage Collection garbage collection) is the process by which memory can be reused.

Problems caused by GC?

Performance problems are expressed as low frame rate, sharp frame rate fluctuations or intermittent lag.

Introduction to Unity Memory management?

Unity has access to two memory pools, stacks, and heaps (managed heaps). Stacks are used for short-term storage of small chunks of data, and heaps for long-term storage and large chunks.

Stacks are used for short-term storage of small chunks of data, and heaps for long-term storage and large chunks.

When the variable goes out of scope, the memory is no longer used and can be returned to the original memory pool. When the memory is returned to the original memory Chili, we

Said the memory was released. The existing variable in the stack is released in real time when it goes out of scope, and there are variables outside the scope of the heap that are not released and remain allocated.

Useless heaps are freed only when the GC is executed.

The GC recognizes and frees unused heap memory. The GC runs periodically to clean up the heap.

Heap allocation time step?

First, Unity checks if there is enough space memory on the heap, and if so, the memory of the variable is allocated.

If not, unity triggers the GC to attempt to free unused heap memory, and this operation can be slow. If the heap memory is sufficient after the GC, the memory of the variable is allocated.

If there is not enough free memory on the heap after the GC, Unity will request more memory from the operating system to enlarge the heap size. This operation may be slow.

Heap allocations can be slow, especially when the GC must be performed and the heap size is enlarged.

    (We reduce the GC, in fact, reduce the number of heap expansion)

GC is a time-consuming operation, the more objects on the heap, the more references in the code, the more time the GC takes.

When will the GC be triggered?

The GC is triggered when there is insufficient memory available on the heap when the heap is allocated.

The GC will run automatically. (frequency varies by platform)

Manually forcing the GC to be invoked.

GC problem?

If there are many objects on the heap and a large number of object references to check, the process of checking all these objects can be slow. This may cause the game to lag or run slowly.

Another problem is that the GC is triggered on an untimely occasion.

Heap fragmentation: When memory is allocated from the heap, memory is obtained from free space in blocks of different sizes based on the size of the data that must be stored.

When these memory returns to the heap, the heap may be divided into a number of small idle kuai that are allocated quickly. This means that although the total amount of available memory is high. But because

Fragmentation is severe and cannot allocate a contiguous amount of large memory. This means that the GC is triggered or has to enlarge the heap size.

(The game memory size will be higher than the actual size required, GC will be triggered frequently)

Unity GC Optimization Essentials

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.