Memory Leak and Garbage Collection in AS3

1. Memory leak

Refers to a situation in which an oversight or error causes a program to fail to free memory that is no longer in use. After an application allocates a certain segment of memory, due to a design error, it loses control of the segment of memory, resulting in a waste of memory (data from Baidu search). What does this sentence mean? For example, a cattle herder and a cow, the cattle herder is like a reference in programming, and the cow is like an object. We control the object by reference, the herder leads the cow by the rope, and when the cow finishes eating grass, he should lead it home instead of letting it go. A "memory leak" occurs when we lose control of that object (a block of memory).

 

2. Garbage recycling

Simply put, unused memory is garbage, and garbage collection is to reclaim unused memory to free up memory space.

There are two main methods of garbage collection in AS3: "reference counting method" and "identification clearing method".

 

Reference counting method: When a reference to a memory object is added, the counter is incremented by 1, and when a reference to a memory object is removed, the counter is decremented by 1. When the FP judges that the counter is 0, it means that the object has no reference, and there is no way to control it without a reference, which meets the conditions of the garbage collection mechanism. But there is one case where it will be different! When there are multiple objects referencing each other, all counters are always 1, and a memory leak occurs. for example:

var a:Object = {}
var b:Object = {foo:a};

a.foo = b;

a=null;

b=null;

In the above code, all references to objects are deleted. There is no way to access these two objects in the program anymore, but the reference counters for both objects are 1 because they refer to each other. That is, the two memory blocks are indeed useless, but the garbage collector does not clean them up. It is conceivable that this method is still good under normal circumstances, but it will not work in this case! So consider the second

method.

 

Identification clearing method: When this method is executed, it will start from the root object of FLASH, that is, root, to each referenced object as an identification. Then FLASH will traverse all the references in the current program and clear the objects without identification. In this case, the clearing is accurate! But there will be a problem. Traversal is very CPU-intensive. Although fp9 clears and reduces the CPU usage by adjusting the iteration flag, the consumption is still very large! So another more negative approach was adopted! It is a segmented execution, one part at a time, and then once in a while. This reduces CPU consumption. So we usually know that the garbage collector of FP9 and 10 is executed occasionally!

 

Third, the function method of garbage collection

System.disposeXML()
BitmapData.dispose()
Loader.unloadAndStop()
System.gc() , this is the most useful function, but only for debug version of FP

removeChild()

removeEventListener()

set reference to null

System.totalMemory, the return value is the number of bytes occupied by the memory. This has nothing to do with memory recovery. It is only used to view the memory usage. It is commonly used in debugging.

……

 

The above functions are all methods that will be used in garbage collection, but generally it is necessary to determine the comprehensive use of several methods according to the specific situation. This requires you to really understand the memory recycling mechanism of FP to know how to use it.

 

(Some of the content refers to some articles on the Internet, and cannot be listed one by one. I hope the original author will forgive me)

 

recommend:

AS3.0 Object Replication

==================================================

Author: Green Garden

 

Original address: http://www.chenlinsheng.com/archives/1957

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326804141&siteId=291194637