Actionscript3 memory management-Garbage Collector

Source: Internet
Author: User

The text is as follows:

First, let's look at the recovery mechanism of.

Garbage collection in Flash is used to automatically clear unnecessary variables in the memory. There are two algorithms:

Reference count and identify and clear.
The reference count is used to track all references of objects in the memory. When we create a reference pointing to this object, the reference count of this object is increased by 1.
VaR A: object = new object (); // new object in memory given reference count of 1
VaR B: Object = A; // object now has reference count of 2

When there is no reference to this object in the memory, the useless unit recycler (GC) will remove this object from the memory.

Delete A; // object has reference count of 1
Delete B; // object has reference count of 0, removed from memory

Note that the delete operator is only used to delete related variables. However, it does not remove objects from the memory, which is a useless unit recycler, the delete operator cannot delete a class member variable.
The reference count does not work normally in some cases. for example, if you define two objects, they only reference themselves, and no other objects reference them, so that their reference count is greater than 0, but there is no way to access them.

VaR A: object = new object (); // reference (a) Count 1
VaR B: Object = new object (); // reference (B) Count 1
A. B = B; // reference (B) Count 2
B. A = A; // reference (a) Count 2
Delete A; // reference (a) Count 1
Delete B; // reference (B) Count 1

Objects A and B have been deleted from the current scope, so they cannot be accessed. however, object A can still access object B, and object A can also access object B. objects A and B seem to have been manually deleted by programmers. In fact, they still exist in the memory and their reference count is greater than 0, in this case, the reference count has a major defect, and the tag-clearing algorithm can solve this problem.
The mark-clearing algorithm is a process in which the program scans all objects and references in a basic area (such as root or stage) to mark the discovered objects, which cannot be accessed or deleted. taking the example of object A and object B above for example, because object A and object B cannot be accessed by any objects in the root directory, they will not be marked, it will be cleared by the useless unit recycler (GC.

// Scan process description
[Root] <-scan...
[Objectref (marked)] <-scan...
[Objectref (marked)] <-scan...
[Objectref (marked)] <-scan...
...
[Delete all objects not marked]

The mark-clearing algorithm may spend a lot of time relative to the reference count, and will not be executed as frequently as the latter. in fact, sometimes it is almost not executed in a video. When the mark is cleared, the timeline has already passed through many frames. That is to say, sometimes you think that the variable has been deleted from the memory, but in fact it will still exist in the memory for a period of time. for scripts with objects, such as enterframe events, these events are still being executed before the objects are actually deleted, so you should always remember to clear your events when they are not used.

What is a weak reference)

 

If you want to use a reference that is not counted by GC, you can define a weak reference. the weak reference is ignored by the reference counter. Even if the referenced object is deleted, the weak reference can still exist.
Weak references in as3.0 cannot be used everywhere and can only be used in the following two places: one is for a dictionary object, the dictionary constructor uses an optional parameter to determine whether weak references are used for the key value of the dictionary instance. the default value is false. Strong references are used. If it is set to true, weak references are used.

VaR dict: dictionary = New Dictionary (true); // use weak references as keys

If you perform the preceding operations, the key value you use to store data will not be counted as an object reference, that is, the object reference count will not increase.

VaR OBJ: Object = new object ();
Dict [OBJ] = true;
Delete OBJ; // OBJ can be garbage collected since the dict reference isnt counted

In addition, you can specify a weak reference in the addeventlistener method of the eventdispatcher class. The addeventlistener method has a parameter that can be used to specify the listener reference as a weak reference.

// Addeventlistener (type: String, listener: function, usecapture: Boolean = false, priority: Int = 0, useweakreference: Boolean = false): void
Addeventlistener (mouseevent. Click, clickhandler, false, 0 true); // use weak references

By default, this parameter is also set to false. A strong reference is used. Here, the listener reference is specified as a weak reference, and useless unit recycler (GC) is called to recycle useless listeners, is a good habit.

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.