On Nov 10, 2:56 pm, "David Parks" <[EMAIL PROTECTED]> wrote:
> Thanks for the response.
> I was not aware of the delete operator in JS (I'm still coming up to speed
> on some of the languages particulars). So I read up on it and have now
> performed a new test to see if:
> 1) delete will work on a simple numerical variable attribute
> 2) delete will work on an object (since one definition of the delete
> operator suggests that it is designed to clean up objects created with the
> 'new' operator).
>
> I updated the script slightly (as shown below), the interesting variables
> are x and d (x is an instance of a simple numeric variable, d is an instance
> of the object goHere created with the 'new' operator).
>
> However, when I run this test I am left with the same problem.
>
> After each line of JS code, I execute System.gc, run Thread.yield(), wait 5
> seconds (understanding that System.gc() runs concurrently in another
> thread), and write out all variables obtained from the
> top_level_scope.getAllIds() method. I can still see the variables "x" and
> "d" returned from top_level_scope.getAllIds(), and in fact they still have
> their original values (x=1, and d=Object).
>
> In response to your other comments, yes, I would be instantiating a separate
> scope per script (following the shared scope model documented on the Rhino
> projects page), however in my application a script may execute for a very
> long time (on the order of weeks or months), so I need it to be able to
> clean up after its self (as well as for some other reasons), relying on
> cleanup at the time the scope is discarded isn't enough.
>
> Any thoughts? Thanks!
> David
>
> =======
> Script
> =======
> static String js = "var x=1;
> " + "\n" + // 1
> "delete x;
> " + "\n" + // 2
> "var y=2;
> " + "\n" + // 3
> "z=3;
> " + "\n" + // 4
> "function goHere(a){
> " + "\n" + // 5
> " var b=5;
> " + "\n" + // 6
> " c=6;
> " + "\n" + // 7
> " return (a+b+c);
> " + "\n" + // 8
> "}
> " + "\n" + // 9
> "var d = new goHere();
> " + "\n" + // 10
> "y = null;
> " + "\n" + // 11
> "var myCars=new Array(\"Saab\",\"Volvo\",\"BMW\");
> " + "\n" + // 12
> "delete d;
> " + "\n" + // 13
> "var f = c;
> " + "\n" + // 14
> "var g=true;
> "; // 15
>
> =======
> Output
> =======
> myCars: [EMAIL PROTECTED]
> y: [EMAIL PROTECTED]
> d: [EMAIL PROTECTED]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: [EMAIL PROTECTED]
>
> At Line #: 1 : var x = 1;
> myCars: [EMAIL PROTECTED]
> y: [EMAIL PROTECTED]
> d: [EMAIL PROTECTED]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: 1.0
>
> At Line #: 2 : delete x;
> myCars: [EMAIL PROTECTED]
> y: [EMAIL PROTECTED]
> d: [EMAIL PROTECTED]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: 1.0
>
> At Line #: 3 : var y = 2;
> myCars: [EMAIL PROTECTED]
> y: 2.0
> d: [EMAIL PROTECTED]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: 1.0
>
> At Line #: 4 : z = 3;
> myCars: [EMAIL PROTECTED]
> y: 2.0
> d: [EMAIL PROTECTED]
> z: 3.0
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: 1.0
>
> At Line #: 10 : var d = new goHere();
> myCars: [EMAIL PROTECTED]
> y: 2.0
> d: [EMAIL PROTECTED]
> z: 3.0
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: 1.0
>
> At Line #: 5 : function goHere(a) {
> myCars: [EMAIL PROTECTED]
> y: 2.0
> d: [EMAIL PROTECTED]
> z: 3.0
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: 1.0
>
> At Line #: 6 : var b = 5;
> myCars: [EMAIL PROTECTED]
> y: 2.0
> d: [EMAIL PROTECTED]
> z: 3.0
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
> x: 1.0
>
> At Line #: 7 : c = 6;
> myCars: [EMAIL PROTECTED]
> x: 1.0
> y: 2.0
> z: 3.0
> c: 6.0
> d: [EMAIL PROTECTED]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
>
> At Line #: 8 : return (a + b + c);
> myCars: [EMAIL PROTECTED]
> x: 1.0
> y: 2.0
> z: 3.0
> c: 6.0
> d: [object Object]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
>
> At Line #: 11 : y = null;
> myCars: [EMAIL PROTECTED]
> x: 1.0
> y: null
> z: 3.0
> c: 6.0
> d: [object Object]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
>
> At Line #: 12 : var myCars = new Array("Saab", "Volvo", "BMW");
> myCars: [EMAIL PROTECTED]
> x: 1.0
> y: null
> z: 3.0
> c: 6.0
> d: [object Object]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
>
> At Line #: 13 : delete d;
> myCars: [EMAIL PROTECTED]
> x: 1.0
> y: null
> z: 3.0
> c: 6.0
> d: [object Object]
> f: [EMAIL PROTECTED]
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
>
> At Line #: 14 : var f = c;
> myCars: [EMAIL PROTECTED]
> x: 1.0
> y: null
> z: 3.0
> c: 6.0
> d: [object Object]
> f: 6.0
> g: [EMAIL PROTECTED]
> goHere: [EMAIL PROTECTED]
>
> At Line #: 15 : var g = true;
> myCars: [EMAIL PROTECTED]
> * x: 1.0
> y: null
> z: 3.0
> c: 6.0
> * d: [object Object]
> f: 6.0
> g: true
> goHere: [EMAIL PROTECTED]
>
> Process finished with exit code 0
>
> -----Original Message-----
> From:
>
> [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]
> a.org] On Behalf Of Norris Boyd
> Sent: Monday, November 10, 2008 7:48 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Garbage collection of JS variables
>
> On Nov 10, 11:26 am, "David Parks" <[EMAIL PROTECTED]> wrote:
> > Hi, I've been playing with the very simple script shown below. I've set up
> > an embedded debugger application so I can watch the script execution line
> by
> > line.
>
> > What I have been trying to answer is exactly how garbage collection works
> in
> > Rhino.
>
> > In this example I have defined a variable "x" on line 1, then, on line 2 I
> > decide I no longer need the variable, so I set it to null.
>
> > At each line of the script, the debugger pauses and I am performing the
> > following steps:
> > 1) run System.gc()
> > 2) this.wait(5000)
> > 3) printing out a list of all variables stored in the top-level
> > scope
>
> > At some point I was hoping that x would be de-allocated (removed from the
> > scope), however it never happens, it always remains visible in the top
> level
> > scope.
>
> > What is necessary for a variable to be garbage collected? I will be
> > executing a large number of concurrent scripts, and memory is of concern
> to
> > me.
>
> > Thanks!
> > David
>
> > ______________________________________________________
>
> > var x=1;
> > x=null;
> > var y=2;
> > z=3;
> > function goHere(a){
> > var b=5;
> > c=6;
> > return (a+b+c);}
>
> > var d = goHere(10);
> > y = null;
> > var f = c;
> > var g=true;
>
> > ______________________________________________________
>
> > Results (with only outputting the value of "x"):
>
> > At Line #: 1 : [var x = 1;]
> > x: [EMAIL PROTECTED]
> > At Line #: 2 : [x = null;]
> > x: 1.0
> > At Line #: 3 : [var y = 2;]
> > x: null
> > At Line #: 4 : [z = 3;]
> > x: null
> > At Line #: 10 : [var d = goHere(10);]
> > x: null
> > At Line #: 5 : [function goHere(a) {]
> > At Line #: 6 : [ var b = 5;]
> > At Line #: 7 : [ c = 6;]
> > At Line #: 8 : [ return (a + b + c);]
> > At Line #: 11 : [ y = null;]
> > x: null
> > At Line #: 12 : [var f = c;]
> > x: null
> > At Line #: 13 : [var g = true;]
> > x: null
>
> > Process finished with exit code 0
> > ______________________________________________________
>
> If you want to remove variable "x" from the scope, try "delete x".
> Otherwise Rhino must keep "x" defined--people would be surprised if
> setting a variable to null resulted in the variable itself
> disappearing!
>
> More generally, you'll presumably want to have a different scope for
> each script execution. When you're done with each script you can then
> release the reference to the scope and the entire scope should be
> available for collection.
>
> ...
>
> read more »
Variables are defined to be DONTDELETE by ECMA, so
var x;
delete x;
will not delete x. You can see this by the return value of the delete
operator which is false in this case.
Creating the variable by assignment makes it possible to delete. For
example:
js> var x = 3;
js> x
3
js> delete x
false
js> x
3
js> y = 7
7
js> y
7
js> delete y
true
js> typeof y
undefined
--Norris
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino