Forced garbage collection flash memory release for Flash

Source: Internet
Author: User
Tags addchild garbage collection

I got a picture. Loop scrolling effect memory is high and low but the resources released are very strange.
Check out the Flash garbage collection original Flash recycle every time garbage free memory and the resources of the image directly delete
Null dropped and not released
Need to be forced to recycle of course also pay a price CPU

All that's left is to turn




Essence, the garbage collector forces the call
Private Function doclearance (): void {
Trace ("clear");
try{
New LocalConnection (). Connect ("foo");
New LocalConnection (). Connect ("foo");
}catch (error:error) {

}
}

/////////////////////////////////////////////////////////////

Overview: AVM2 theoretically comes with a garbage collector, but when it comes to garbage collection, it's theoretically non-human to control, and if we want to enforce the garbage collector, we need some informal means, such as the ones mentioned below.

Keywords: garbage collection

Core Hack Technology
try{
New LocalConnection (). Connect ("Moonspirit");
New LocalConnection (). Connect ("Moonspirit");
}catch (error:error) {

}

Run the above code to force the garbage collector to execute once. Specific tests:

10k elements are created using Initnobitmapdataview () and Initbitmapdataview () respectively


Test results:
1.1 Not using BitmapData memory consumption 40M

2.1 Use BitmapData still memory consumption 40m!
Cause Analysis: Although _sqrlist is set to NULL, 1w sprite data that was previously referenced by _sqrlist still exists in memory, although AMV2 comes with a garbage collector, when garbage collection is not deterministic. To enforce the garbage collection machine, use the Hack method mentioned above
2.2 Using BitmapData + garbage collection hack memory consumption 12M

Package {
Import Flash.display.Bitmap;
Import Flash.display.BitmapData;
Import Flash.display.Sprite;
Import flash.net.LocalConnection;

public class Moonspirit extends Sprite {
Private Const SQR_AMOUNT:INT = 10000; Number of blocks
private Var _container_sp:sprite; Container Sprite
private Var _sqrlist:array; References to all squares

Public Function Moonspirit () {
Init ();
}

Private Function init (): void{
_container_sp = new Sprite ();
AddChild (_CONTAINER_SP);
Initnobitmapdataview ();
Initbitmapdataview ();
}

Initialization is shown by means of the usual method
Private Function Initnobitmapdataview (): void {
LAYOUTTENTHOUSANDSQR ();
}

Initialization via BitmapData snapshot display
Private Function Initbitmapdataview (): void {
LAYOUTTENTHOUSANDSQR ();
var = new BitmapData (Mybitmapdataobject:bitmapdata, 0xFF0000, false);
var myimage:bitmap = new Bitmap (mybitmapdataobject);
AddChild (MyImage);
UNLAYOUTTENTHOUSANDSQR ();
_sqrlist = null;
Doclearance ();
}

Private Function Layouttenthousandsqr (): void {
_sqrlist = new Array ();
for (var i:int = 0; i < Sqr_amount; i++) {
_sqrlist.push (New Sprite ());
_sqrlist[i].graphics.beginfill (0xff0000);
_sqrlist[i].graphics.drawrect (0,0,100,100);
_sqrlist[i].graphics.endfill ();
_container_sp.addchild (_sqrlist[i]);
}
}

does not display
Private Function Unlayouttenthousandsqr (): void {
for (var i:int = 0; i < Sqr_amount; i++) {
_container_sp.removechild (_sqrlist[i]);
Delete _sqrlist[i];
}
}

Essence, the garbage collector forces the call
Private Function doclearance (): void {
Trace ("clear");
try{
New LocalConnection (). Connect ("foo");
New LocalConnection (). Connect ("foo");
}catch (error:error) {

}
}
}

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.