Force Garbage Collection in AS3?

前端 未结 8 1915
轻奢々
轻奢々 2020-11-30 07:33

Is it possible to programmatically force a full garbage collection run in ActionScript 3.0?

Let\'s say I\'ve created a bunch of Display objects with eventListeners a

相关标签:
8条回答
  • 2020-11-30 08:14

    If you have to, calling the gargabe collector could be useful... so, you have to be carefull how and when you do it, but there is no doubt that there are times when is neccesary.

    for example, if you have an app that is modular, when you change from one view to the other, all the deleted objects could represent a large amount of memory that should be available as faster as possible, you just need to have control of the variables and references you are disposing.

    0 讨论(0)
  • 2020-11-30 08:16

    recycling doesn't really help. I used one loader that repeatedly loaded the same jpg every 500ms. task manager still reported a non stop increase in memory.

    tried and proven solution here.

    http://simplistika.com/as3-garbage-collection/

    0 讨论(0)
  • 2020-11-30 08:19
    try {
        new LocalConnection().connect('foo');
        new LocalConnection().connect('foo');
    } catch (e:*){
        trace("Forcing Garbage Collection :"+e.toString());
    }
    
    0 讨论(0)
  • 2020-11-30 08:31

    There is a new API for telling the GC that it might be a "relatively good moment" to collect.

    See the Adobe API docs for System.pauseForGCIfCollectionImminent

    And also this Adobe blog post from shortly after the method was introduced in Player version 11

    The method takes an "imminence" argument; basically, you feed in a low number (near 0.0) if you really want the collector to run, even if there has not been much activity (currently measured by bytes-allocated) since the last collection, and you feed in a large number (near 1.0) if you only want the collection pause to happen if we were already near the point where a collection would happen anyway.

    The motivation here is for situations in e.g. games where you want to shift the point where GC's happen by a small amount, e.g. do the GC during a change of level in the game, rather than two seconds after the player started exploring the level.

    One very important detail: This new API is supported by both the Release and the Debugger Flash Runtimes. This makes it superior to calling System.gc().

    0 讨论(0)
  • 2020-11-30 08:35

    As others said: do not try to GC manually, there are hacks but it's not safe.

    You should try recycling objects when you can - you'll save a lot of memory.

    This can be applied for instance to BitmapDatas (clear and reuse), particles (remove from display and reuse).

    0 讨论(0)
  • 2020-11-30 08:36

    For all currently released versions, System.gc() only works in the debug version of the Flash player and ADL (the debug environment for AIR apps). Flash player 10 beta currently does work in all flavors.

    I agree with Davr, it's a bad idea to do. The runtime will usually have a better idea than you do.

    Plus, the specifics of how the garbage collector works is an implementation detail subject to change between flash player versions. So what works well today has no guarantee to work well in the future.

    0 讨论(0)
提交回复
热议问题