0

I have a question related to memory. I will give an example to make it clear how everything works now.

I have 2 arrays:

var ArrayNew:Array = new Array();
var ArrayOld:Array = new Array();

Also i have a class to store my objects (3 properties). For example:

public Id {get; set;}
public Name {get; set;}
public Type {get; set;}

The thing is, that i'm filling the ArrayNew with new objects every (for example 12 hours):

ArrayNew.push(x, x, x)
.....
ArrayNew.push(x, x, x)

It may be about ~200 records or even more. After that i make this:

ArrayOld = ArrayNew;
ArrayNew = null;

So the thing is, how memory works in this situation and what happens with objects? Does ArrayOld = ArrayNew make a copy of objects (cause now it works)? Does ArrayNew=null delete created objects? I wish you undearstand the situation. :)

3 Answers 3

1

The objects stored in arrayOld get garbage collected if there are no other references to them. The ones from arrayNew are not copied - they are referenced by arrayOld after the assignment.

It's to say that that after:

arrayNew[0].name = 'a random silly text';
arrayOld = arrayNew;
arrayOld[0].name = 'another silly string';
trace(arrayNew[0]);

You'd get:

another silly string

Style note: Normally you don't start variable/object names with capitals, it's reserved for classes.

Sign up to request clarification or add additional context in comments.

1 Comment

I know. I jsut wrote as the example. Didn't event noticed that i started with Capitals :)
1

If I understand you correctly you want to know what happened to ArrayOld.

My code:

var arr_1:Array = ["Hello world!"];
var arr_2:Array = ["My name is Stas!"];

arr_2 = arr_1;
arr_1 = null;

trace(arr_2);// Hello world!

If I made a mistake with the understanding of the issue do explain it properly.

Comments

0

ArrayOld = ArrayNew is simply making ArrayOld reference the same thing as ArrayNew at that point. The actual data in memory is not copied.

ArrayNew = null is simply assigning null value to the ArrayNew reference. It doesn't delete the data ArrayNew previously referenced, nor does it affect other references to that data (such as ArrayOld).

At this point, the original data that ArrayNew used to reference has not changed in any way, you've just handed off what variable refers to it.

At this point if you did ArrayOld = null, then the original data in memory no longer has any reference to it and it will eventually be purged by garbage collection, but not right away. It will happen "later" at a time the runtime decides is convenient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.