For example, so-called generational GCs split their work into smaller chunks, by grouping all recent allocations in one place so they can be scanned and collected quickly. Newer garbage collectors have different ways for reducing those collection pauses. The length of the pause depends on the amount of data allocated by the program, and if this pause is long enough, it could result in noticeable stutter.
For example, a simple mark-and-sweep collector is a blocking collector – it would pause the entire program so that it can process the entire heap at once. Older garbage collectors have gained a reputation for introducing framerate “hiccups”. Let’s talk a bit about garbage collection and Mono.įinding and reclaiming data on the heap that’s no longer in use is the job of the GC, and different collectors can vary drastically in performance. Finally, value types can be promoted from the stack to the heap via boxing. Data of value type, such as primitives (int, string, etc) or instances of structs, can live on the stack, unless they’re inside a container that already lives on the heap (such as an array of structs).
Instances of reference types such as classes, or arrays such as int, always get allocated on the heap, to be GC’d later. NET also exposes two families of data types, which get allocated differently. NET and Mono specifically, heap memory gets reclaimed automatically by the garbage collector (GC), which is practically speaking a black box, and the user doesn’t have a lot of control over it. I assume you’re already familiar with the distinction between stack allocation and heap allocation, but in short: data on the stack is short-lived, but alloc/dealloc is practically free, while data on the heap can live as long as necessary, but alloc/dealloc becomes more expensive as the memory manager needs to keep track of allocations. But it makes the cost of allocation and deallocation even harder to predict.
UNITY ARRAY VS ARRAYLIST CODE
It features automatic memory management which fixes a lot of the safety problems, for example it’s no longer possible to use memory after it has been deallocated (ignoring unsafe code for now). NET, or rather its open source cousin, Mono.
UNITY ARRAY VS ARRAYLIST MANUAL
Third, in languages with manual memory management like C or C++, each allocation is an opportunity to introduce subtle bugs that can turn into huge problems, anywhere from memory leaks to full crashes.
UNITY ARRAY VS ARRAYLIST FREE
Second, allocation is not free – allocating and deallocating on the heap will cost you CPU cycles. First, it’s a limited resource, especially on mobile devices. One of the first things gamedevs always learn is to not allocate memory needlessly. Good idea for another post, though!)įirst, let’s start with a quick recap about memory allocation and garbage collection. The following specifically concentrates on perf improvements on the coding side, such as looking at different code constructs and see how they perform in both speed and memory usage. (There is another set of perf improvements that are also useful, such as optimizing your assets, compressing textures, or sharing materials, but I won’t touch those here. I’m hoping this post will add some a few more interesting details, collected from various sources as well as from my own optimization adventures, about ways to improve performance using this engine. There’s a lot of useful information out there about memory and performance optimizations in Unity. I have myself relied heavily on Wendelin Reich’s posts and Andrew Fray’s list when getting started – they are excellent resources worth studying.