Forums > Javascript

javascript memory usage?

May 12, 2007 | 4:44 am

Hi,

The following simple patch highlights a weird problem. If you run ‘top’ at the same time as you run this patch (turn on the metro), you’ll see the RSIZE, RPRVT, and %CPU start to increase steadily. There seems to be some sort of memory leak. I can’t locate which lines of the javscript cause the problem. Does anyone know what’s causing this?

MAX:

max v2;
#N vpatcher -13 44 438 462;
#P origin 10 7;
#P window setfont "Sans Serif" 9.;
#P message 120 304 214 196617 "pan 0.2 500";
#P newex 114 252 62 196617 prepend set;
#P toggle 107 75 15 0;
#P button 116 196 15 0;
#P newex 120 142 58 196617 metro 500;
#P newex 960 -76 45 196617 r envq1;
#P newex 116 222 58 196617 js test2.js;
#P connect 0 0 5 0;
#P connect 2 0 3 0;
#P connect 3 0 0 0;
#P connect 4 0 2 0;
#P connect 5 0 6 0;
#P pop;

JavaScript:

autowatch =1;

function mess(tgt, str, val, time) {
var time2 = time | 0;
var tmp = "";
tmp += "target " + tgt + ", " + str + " " + val + " " + time;
//tmp += str + " " + val + " " + time;
//outlet(0, "target "+tgt);
outlet(0,tmp);
}

var val = 0;
function randrange(range){
val = Math.random()
return val* range;
}

function randvoice(tgt,cf){
var freq = 220+randrange(60);
var amp = -40+randrange(20);
var cf2 = cf+randrange(100);
var bw = 150+randrange(75);
var pan = 0.5+randrange(0.5);

mess(tgt,"freq", freq,500);
mess(tgt,"amp",amp,500);
mess(tgt,"cf",cf2,500);
mess(tgt,"bw",bw,500);
mess(tgt,"pan",pan,500);
}

function bang(){
for(var i = 0; i < 3; i++){
var cf = i*300;
for(var j = 0; j < 6; j++){
randvoice(i,cf );
}
}
}

Isaac


May 12, 2007 | 4:59 am

On May 11, 2007, at 9:44 PM, isaac wrote:

> The following simple patch highlights a weird problem. If you run
> ‘top’ at the same time as you run this patch (turn on the metro),
> you’ll see the RSIZE, RPRVT, and %CPU start to increase steadily.
> There seems to be some sort of memory leak. I can’t locate which
> lines of the javscript cause the problem. Does anyone know what’s
> causing this?

Looks like you’re bloating the symbol table inside mess(). Do you
need to use strings/symbols? Why not an atom list? Commas and args
don’t take the same effect as message boxes as JS strings anyway.
Common confusion, but you typically don’t want to use strings for
composite messages anyway, but rather a list of items. See my message
to Barry earlier today for one of many examples.

-Joshua


May 12, 2007 | 5:51 pm

Joshua-

Thanks, it looks as though that fixes it. Makes programming easier too, since I don’t need a ‘fromsymbol.’ Interestingly, RZISE and RSHRD still go up, but it looks like they level off after enough time, so its probably nothing.

Thanks again,
Isaac


May 12, 2007 | 7:31 pm

On May 12, 2007, at 10:51 AM, isaac wrote:

> Interestingly, RZISE and RSHRD still go up, but it looks like they
> level off after enough time, so its probably nothing.

Yes, the JS memory heap can grow to 16-32MB or something like that
until the JS VM starts garbage collecting, which would explain what
you’re seeing there.

-Joshua


Viewing 4 posts - 1 through 4 (of 4 total)