Posted By

scottwatkins on 03/30/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Praimen


Garbage Collection


 / Published in: ActionScript 3
 

URL: http://cookbooks.adobe.com/post_Garbage_Collection-18750.html

  1. // *******************************************************************************
  2. // Garbage Collection
  3. // *******************************************************************************
  4.  
  5.  
  6. private var gcTimer:Timer;
  7.  
  8.  
  9. public function creationCompleteHandler():void {
  10. startMemoryMonitoring(1000 * 60 * 30);
  11. }
  12.  
  13. public function startMemoryMonitoring(garbageCollectEvery:Number):void
  14. {
  15. // Timer for garbage collection at regular intervals. Runs until the application exits.
  16.  
  17. this.gcTimer = new Timer(garbageCollectEvery, 0);
  18. gcTimer.addEventListener(TimerEvent.TIMER, onTriggerGC);
  19. gcTimer.start();
  20. }
  21.  
  22. private function onTriggerGC(event:TimerEvent):void
  23. {
  24. trace("System Total Memory BEFORE Garbage Collection: " + System.totalMemory );
  25.  
  26. try
  27. {
  28. /**
  29.   * Force garbage collection
  30.   */
  31.  
  32. trace("Forcing Garbage Collection...");
  33.  
  34. new LocalConnection().connect('_noop');
  35. new LocalConnection().connect('_noop');
  36. }
  37. catch (e:Error)
  38. {
  39. // The following error is expected: Error #2082: Connect failed because the
  40. // object is already connected.
  41. Application.application.callLater(showTotalMemory);
  42. }
  43. }
  44.  
  45. private function showTotalMemory():void {
  46. trace("System Total Memory AFTER Garbage Collection: " + System.totalMemory );
  47. }

Report this snippet  

You need to login to post a comment.