Posts Tagged garbage

The Easy Power of Code Generation

Tags: , ,

C# generics are weak. The where clause allows you to know a little about the generic (“T“) types you’re given, but that’s just scratching the surface of what you can do with code generation. Today’s article will show you how easy it is to add a little code generation to a project and the power that brings.

Read the rest of this article »

4 Comments

Garbage Gotchas

Tags: , , , , , , ,

Sometimes it seems like Unity programming is a minefield. Plenty of innocuous-looking code secretly creates garbage and eventually the GC runs and causes a frame hitch. Today’s article is about some of those less-obvious ways to create garbage.

Read the rest of this article »

No Comments

Do Foreach Loops Still Create Garbage?

Tags: , , , ,

Over a year ago I wrote an article title Do Foreach Loops Create Garbage using Unity 5.2 and tested foreach with a variety of collections: List, Dictionary, arrays, etc. Since then Unity has a new C# compiler and version 5.6 has been released. Is it safe to use foreach now? Read on to find out!

Read the rest of this article »

6 Comments

Closures Without the GC

Tags: , , ,

Closures allow you to save the local variables of a function and access them later in a callback. Think of how lambdas can access the local variables of the function they’re declared in, even though the lambda itself is another function. Unfortunately, creating a lambda like this creates garbage for the GC to collect and you have no control over that process. Today’s article introduces an alternative that allows you to take control over the GC and still use nice, type-safe closures. Read on to learn how!

Read the rest of this article »

No Comments

Faster Memory Allocation with Memory Pools

Tags: , ,

One of the advantages we get when we use unmanaged memory is a huge increase in flexibility. For example, we can easily allocate a whole array of objects at once instead of one-at-a-time when we new a class instance. We can also create a memory pool with one allocation then divide it up ourselves. It turns out that can really speed up memory allocation and, at the same time, actually reduce memory fragmentation on top of the fragmentation we’re avoiding by not creating any garbage for the GC. Today’s article shows you how memory pools work and provides an implementation of one you can use in your own projects!

Read the rest of this article »

4 Comments

Delegates and Garbage Creation

Tags: , , , ,

Two facts are at odds in Unity programming. First, delegates like Action, Func, and EventHandler are extremely common with or without events. Second, the garbage collector is a huge source of CPU spikes and memory fragmentation in our games. Why are these facts at odds? Because code that uses delegates is almost always written in a way that creates garbage. It’s an extremely easy trap to fall into, but this article will show you how to get out of it!

Read the rest of this article »

2 Comments

An Alternative to Events

Tags: , , ,

C# has built-in events and they work fine in Unity projects. End of story, right? Not so fast! Have you ever wondered why the Unity API doesn’t have any C# events in it? Or why Unity made their own UnityEvent class for UGUI? Maybe there are some valid reasons to avoid C#’s events. Today’s article discusses an alternative with some serious upsides. Read on to learn more!

Read the rest of this article »

8 Comments

Memory Allocation Without the GC

Tags: , ,

Unity’s garbage collector is super slow and the bane of our programming life. It’s the reason we can’t use foreach, have to make pools of objects, and go to great lengths to avoid boxing. It’s also seemingly mandatory, but that’s not quite true. Today’s article shows you a way that you can skip the GC and still allocate memory!

Read the rest of this article »

16 Comments

How to Use Runtime Asserts to Find Bugs

Tags: , , , ,

Runtime asserts, not the asserts in unit tests, are a valuable debugging tool for any game developer. Today’s article shows you what they are, how to use them, how not to use them, and how they work. Read on to learn more!

Read the rest of this article »

No Comments

Which JSON Library Creates the Most Garbage?

Tags: , , , , , ,

Which JSON library creates the most garbage? That’s a common question I get in response to my JSON articles. Today’s article finds out the answer!

Read the rest of this article »

3 Comments