7 Replies - 11999 Views - Last Post: 01 July 2011 - 11:50 PM

#1 salindor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 301
  • Joined: 10-November 06

flex as3 memory leak troubles

Posted 27 June 2011 - 04:33 PM

I am currently using a trial version of flash builder professional and I figured before my trial ran out I would try out the profiler. That is when I noticed I had a lot more objects around than I should. After doing some tests the following will leave 200 copies of ComplexNum laying around:
protected function initApp():void
{
   var i:int;
   for (i=0; i<200; i++)
   {
      new ComplexNum(4);
   }
}



Is there something I am missing here? I thought on function exit those should all be cleaned by the garbage collector.

Salindor

Is This A Good Question/Topic? 0
  • +

Replies To: flex as3 memory leak troubles

#2 jase81  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 02-March 09

Re: flex as3 memory leak troubles

Posted 28 June 2011 - 09:45 PM

You're creating 200 new instances of your object ComplexNum.

Have you tried running it without the "new".
Was This Post Helpful? 1
  • +
  • -

#3 salindor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 301
  • Joined: 10-November 06

Re: flex as3 memory leak troubles

Posted 29 June 2011 - 03:43 PM

Thank you jase for responding.

The code provided above is an example I simplified as much as possible to highlight my problem.

I tried removing the new and at first it seemed like it would be good; but when I started going everywhere I had used a temporary ComplexNum similar to what you see there I ran acorss one instance where I used two parametes instead of one. The compiler threw an error "Incorrect number of arguments. Expected no more than 1".

What does your suggestion mean to the compiler, I was under the assumption that actionscript acted like Java here and you had to new everything.

Modifying the example it would now look like:

protected function initApp():void
{
   var i:int;
   for (i=0; i<200; i++)
   {
      ComplexNum(4, 5); //does not compile
   }
}



Salindor

This post has been edited by salindor: 29 June 2011 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 brandon_v  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 29-May 08

Re: flex as3 memory leak troubles

Posted 30 June 2011 - 07:14 AM

If there are any strong references to these addresses, then the garbage collector will not collect them. What you may have is these objects referencing each other or themselves, in which case there will be such references. As the programmer, you will need to know when you want to free up the space, and you should look to deallocate the memory, possibly by setting the object (really the object's address, though Flash implies that) to equal null, or by using the delete operator.

The garbage collector accounts for strong circular references, but it's not immediate. It has to do a sweep of sorts. I would highly suggest reading up on how the garbage collection system works with Flash.

The new operator is used to allocate memory at an address. Without it, you code will not make sense since your instances need to be stored somewhere. I would think that the memory is automatically deallocated at the end of each for loop iteration, or at least the end of the function (Flash might be like that), since that's the scope of the instances. I don't understand why you are creating these instances but not saving them to a variable.
Was This Post Helpful? 1
  • +
  • -

#5 salindor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 301
  • Joined: 10-November 06

Re: flex as3 memory leak troubles

Posted 30 June 2011 - 03:36 PM

Brandon, thank you for responding.

So first the ComplexNum class:

public class ComplexNum
{
   static public const negOne:ComplexNum = new ComplexNum(-1);
   static public const one:ComplexNum = new ComplexNum(1);
   static public const zero:ComplexNum = new ComplexNum();
		
   public function ComplexNum(real:Number = 0, imag:Number = 0)
   {
      this.real = real;
      this.imag = imag;
   }
   public var real:Number;
   public var imag:Number;

   //other functions below but as you can see from the code above not calling any functions in this test
}



So I don't see anything that would fit the circular references.

Your right using new like this is pointless; it is only a test to understand flash a bit better. I normally only use new like this when passing it to a temporary variable. Maybe that is the problem; while Java's gc is smart enough to handle this Flash's isn't.

I looked up the delete operator but it looks like a carry over from as2. From "What’s new for the PrintJob class using ActionScript 3.0"

Quote

In ActionScript 2.0, you could use delete to remove an object or on an object property. In ActionScript 3.0, the delete operator is now ECMAScript compatible, meaning delete can only be used on a dynamic property of an object


Salindor
Was This Post Helpful? 0
  • +
  • -

#6 brandon_v  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 29-May 08

Re: flex as3 memory leak troubles

Posted 30 June 2011 - 04:44 PM

You're right, there doesn't seem to be any circular references going on. So if there is in fact a memory leak, then it means it's caused by a reference count of 1 or more that's never handled. A strong reference (versus weak) will be created whenever you are assigning an object to have a complex value (anything not int, uint, boolean, etc.) and adds to that object's reference count.
While the delete keyword does stem from AS2, I do believe it's still a valid way of deallocating memory (it's used in C/C++) by taking away a reference, and thus lowering its reference count. Either way, those references should be deleted once the program leaves that scope.

However, since in the example you provided there is no circular referencing or pointers, it should be deallocated anyway. It probably is just the GC taking some time to get around to it, and isn't actually a real memory leak. Check out this, if you haven't already.

I've had experience with this seemingly unintuitive system when I started AS3, but I think you'll find it makes sense.
Was This Post Helpful? 1
  • +
  • -

#7 salindor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 301
  • Joined: 10-November 06

Re: flex as3 memory leak troubles

Posted 30 June 2011 - 09:38 PM

Yup I had just found that site. I posted the same question in another forum. It was suggested there that because I only created 200 objects the gc doesn't run until about 20-30k of memory has been used. I started thinking about it, and even when I was creating a ton of objects in the real version I hadn't reached 30k yet.

C++ is my primary langauge, their delete is different than action script. If you look at his first example:
// create a new object, and put a reference to it in a:
var a:Object = {foo:"bar"}
// copy the reference to the object into b:
var b:Object = a;
// delete the reference to the object in a:
delete(a);
// check to see that the object is still referenced by b:
trace(b.foo); // traces "bar", so the object still exists.


This could crash and burn in c/c++. That is why java,c#, and actionscript all with garbage collectors. Personally I like managing my own memory better. *sigh*

Salindor
Was This Post Helpful? 0
  • +
  • -

#8 brandon_v  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 29-May 08

Re: flex as3 memory leak troubles

Posted 01 July 2011 - 11:50 PM

View Postsalindor, on 30 June 2011 - 10:38 PM, said:

the gc doesn't run until about 20-30k of memory has been used.


Oh yea, that didn't occur to me. If you wanted, there is a way of forcing collection by calling System.gc() twice, though this only works with the debug player.

I agree, I like managing my own memory too.
But the decision to include a GC is probably there, I think, for people who aren't advanced programmers, rather coders, since Flash is also for designers.

Anyway, I'm glad we learned something here!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1