Posts Tagged GC

Do Foreach Loops Create Garbage?

Tags: , , , , ,

We know that we should reduce the garbage our code produces to lighten the load on Unity’s garbage collector. The trouble is that many of the ways we’re creating garbage are hidden from us. One such way to inadvertently create a lot of garbage is to use a foreach loop… at least that’s what we’ve been told. Do foreach loops really create garbage for all types of arrays, lists, dictionaries, and the rest of the collections? Do they create garbage for every loop or just the first one? Today’s article investigates to put these questions to rest. Are you safe using foreach loops or should you re-write everything to use for. Read on to find out!

Read the rest of this article »

8 Comments

Using Object Pooling to Reduce Garbage Collection

Tags: , ,

Unity’s garbage collector is old and slow. As garbage from your scripts piles up, the garbage collector will eventually run. When it does, it runs all at once instead of spreading the work out over multiple frames. This means you get a big spike on one frame causing your game to momentarily freeze. One of the best ways to get around this is to use an “object pool” to reduce the number of objects eligible for garbage collection. After all, if there’s no garbage then there’s nothing to collect! For more on this strategy as well as a class you can use to implement it, read on!

Read the rest of this article »

10 Comments

String Concatenation Performance

Tags: , , ,

As programmers, we concatenate strings all the time. Should we worry about the performance? How about the amount of garbage we’re producing for the garbage collector? Today’s article runs a quick test to find out!

Read the rest of this article »

No Comments

Keeping Bitmaps Decompressed

Tags: , , ,

It came to my attention in the comments of Preloading Bitmap Decompression that Flash Player would actually free the decompressed bitmap memory if you didn’t make active use of it, similar to garbage collection. So if you followed my strategy from that article to preload a bitmap, it may have been un-preloaded for you by Flash Player! Today’s article shows you how to work around this little problem.

Read the rest of this article »

22 Comments

Top 10 Performance Tips for 2012

Tags: , , ,

It’s a new year and it’s time to make some New Years resolutions for Flash performance. Today’s article is a collection of what i consider 10 top tips for improving the performance of your Flash apps. Read on for the list!

Read the rest of this article »

17 Comments

Dictionary Memory Leak

Tags: , ,

The Dictionary class provides perhaps the most useful support for weak references—and therefore garbage collection control—in the AS3 Flash API. However, due to a subtle error in its documentation, you may inadvertently be leaking a lot of memory. Today’s article shows you how this can happen and how you can easily fix the leak.

Read the rest of this article »

23 Comments

Even Faster Linked Lists

Tags: , , , , ,

Linked lists can be much faster than AS3’s Array and Vector classes if you use them under the right circumstances. It’s been over a year and a half since I last visited the topic, but today it’s time to update my LinkedList class. Read on for the freshly-optimized code and all-new performance testing and analysis!

Read the rest of this article »

47 Comments

Hidden Object Allocations

Tags: , , , , , , , , , , , ,

During some recent memory profiling I was reacquainted with just how many ways there are to unknowingly allocate an object in AS3. The problem is seldom the allocation itself, but rather the later garbage collection (GC) to delete those objects. Ever used a Flash profiler only to see a huge chunk of your CPU time going to [reap], [mark], or [sweep]? Yeah, that’s the GC at work. Today’s article talks about some of the ways you end up allocating objects in AS3 without using the new keyword. These subtle errors can end up costing you!

Read the rest of this article »

28 Comments

Improving Vector3D

Tags: , , , , ,

The Vector3D class debuted in Flash Player 10.0 as Adobe’s official implementation of, well, a 3D mathematical vector (not the pseudo-Array class Vector). Weirdly, it has a w component and is therefore technically a 4D vector, but its API inconsistently make use of the fourth dimension. There are also strange oversights, inefficiencies, and functionality it really should have always had. Read on for my custom Vector3D derivative—Vector3DExt—that fixes all of these problems by extending and improving on the original.

Read the rest of this article »

5 Comments

The Importance of Pre-Allocation

Tags: , , , ,

When you construct an Array or a Vector, you can specify an initial size. Why would you do this?There are various reasons you may want to reserve initially-unused slots for logic reasons, but are there any performance gains to be had by pre-allocating this space for an Array or Vector you intend to completely fill right away? Today I’ll take a look to see just how much performance can be gained by pre-allocating Arrays and Vectors.

Read the rest of this article »

3 Comments