Greetings I have a javascript problem concerning a possible memory leak. I am using Ajax.Request to get a JSON object from a server and then passing the data from the JSON object to a few div''s. This is done every second, but over time it seems my code leaks somewhere (in the browser). I have racked my brain trying to figure this one out...can anyone tell from this snippet what might be wrong: new Ajax.Request(''getData?time='' + dateTime, { method:''get'', onSuccess: function(transport){ var json = transport.responseText.evalJSON(); $(''statusBanner'').update(); $(''Month1'').update(responseJSON.Month1); $(''Month1V'').update(json.Month1V); $(''Month2'').update(json.Month2); $(''Month2V'').update(json.Month2V); $(''Month3'').update(json.Month3); $(''Month3V'').update(json.Month3V); $(''L1V'').update(json.L1V); $(''L2V'').update(json.L2V); $(''L3V'').update(json.L3V); $(''Spread1'').update(json.Spread1); $(''Spread1V'').update(json.Spread1V); $(''Spread2'').update(json.Spread2); $(''Spread2V'').update(json.Spread2V); $(''Spread3'').update(json.Spread3); $(''Spread3V'').update(json.Spread3V); delete json; }, onFailure: function(){ $(''statusBanner'').update("<h1>Loading</h1>"); } }); Many many thanks in advance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Where is that Ajax.Request object created? In a function somewhere that gets called over and over? If so, is there any particular reason the success handler has to be an inline function? (I don''t immediately see it referencing anything outside of itself that it would need to be a closure, for instance.) It shouldn''t be a problem provided nothing''s holding on to the function or the Ajax.Request after it completes, but that doesn''t mean that the GC is going to be perfect in every case and in fact Firefox 2 has several known leaks around this stuff. I''d try this: At global (page/script) scope: function handleUpdateSuccess(transport){ var json = transport.responseText.evalJSON(); $(''statusBanner'').update(); $(''Month1'').update(responseJSON.Month1); $(''Month1V'').update(json.Month1V); $(''Month2'').update(json.Month2); $(''Month2V'').update(json.Month2V); $(''Month3'').update(json.Month3); $(''Month3V'').update(json.Month3V); $(''L1V'').update(json.L1V); $(''L2V'').update(json.L2V); $(''L3V'').update(json.L3V); $(''Spread1'').update(json.Spread1); $(''Spread1V'').update(json.Spread1V); $(''Spread2'').update(json.Spread2); $(''Spread2V'').update(json.Spread2V); $(''Spread3'').update(json.Spread3); $(''Spread3V'').update(json.Spread3V); delete json; } function handleUpdateFailure() { $(''statusBanner'').update("<h1>Loading</h1>"); } And then where your request is created: new Ajax.Request(''getData?time='' + dateTime, { method:''get'', onSuccess: handleUpdateSuccess, onFailure: handleUpdateFailure }); FWIW, might make no difference, might help. -- T.J. Crowder tj / crowder software / com On Mar 8, 3:03 pm, abehrens <abehr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Greetings > > I have a javascript problem concerning a possible memory leak. I am > using Ajax.Request to get a JSON object from a server and then passing > the data from the JSON object to a few div''s. This is done every > second, but over time it seems my code leaks somewhere (in the > browser). > > I have racked my brain trying to figure this one out...can anyone tell > from this snippet what might be wrong: > new Ajax.Request(''getData?time='' + dateTime, > { > method:''get'', > onSuccess: function(transport){ > var json = transport.responseText.evalJSON(); > $(''statusBanner'').update(); > $(''Month1'').update(responseJSON.Month1); > > $(''Month1V'').update(json.Month1V); > $(''Month2'').update(json.Month2); > $(''Month2V'').update(json.Month2V); > $(''Month3'').update(json.Month3); > $(''Month3V'').update(json.Month3V); > > $(''L1V'').update(json.L1V); > $(''L2V'').update(json.L2V); > $(''L3V'').update(json.L3V); > > $(''Spread1'').update(json.Spread1); > $(''Spread1V'').update(json.Spread1V); > $(''Spread2'').update(json.Spread2); > $(''Spread2V'').update(json.Spread2V); > $(''Spread3'').update(json.Spread3); > $(''Spread3V'').update(json.Spread3V); > delete json; > > }, > > onFailure: function(){ > $(''statusBanner'').update("<h1>Loading</h1>"); > > } > }); > > Many many thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Oh, and forgot to say: Do you *really* mean to be saying "Loading" when the load fails? ;) On Mar 8, 3:03 pm, abehrens <abehr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Greetings > > I have a javascript problem concerning a possible memory leak. I am > using Ajax.Request to get a JSON object from a server and then passing > the data from the JSON object to a few div''s. This is done every > second, but over time it seems my code leaks somewhere (in the > browser). > > I have racked my brain trying to figure this one out...can anyone tell > from this snippet what might be wrong: > new Ajax.Request(''getData?time='' + dateTime, > { > method:''get'', > onSuccess: function(transport){ > var json = transport.responseText.evalJSON(); > $(''statusBanner'').update(); > $(''Month1'').update(responseJSON.Month1); > > $(''Month1V'').update(json.Month1V); > $(''Month2'').update(json.Month2); > $(''Month2V'').update(json.Month2V); > $(''Month3'').update(json.Month3); > $(''Month3V'').update(json.Month3V); > > $(''L1V'').update(json.L1V); > $(''L2V'').update(json.L2V); > $(''L3V'').update(json.L3V); > > $(''Spread1'').update(json.Spread1); > $(''Spread1V'').update(json.Spread1V); > $(''Spread2'').update(json.Spread2); > $(''Spread2V'').update(json.Spread2V); > $(''Spread3'').update(json.Spread3); > $(''Spread3V'').update(json.Spread3V); > delete json; > > }, > > onFailure: function(){ > $(''statusBanner'').update("<h1>Loading</h1>"); > > } > }); > > Many many thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the help TJ. A couple things... The code calls a function called cycle() on window load, which has the request in it called once per second (using setTimeout(cycle(), 1000)). That Ajax.Request is inside cycle(). I am not averse to moving the heavy lifting outside the Ajax.Request, I will give that a shot. I could not find any examples of how to easily take a bunch of attributes on a JSON object and plop them in a page from the prototype examples. I was hoping somebody could point me in that direction, as well. As for the "Loading" text, the page this is served on will only display that at program startup, as the backend giving up the data will error out for a bit until a connection is made to a datasource. So the failure will only happen in one case (except if my backend goes down), but I can see situations where the text will be misleading. It is intentional for lack of better options. Thanks! On Mar 8, 11:16 am, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh, and forgot to say: Do you *really* mean to be saying "Loading" > when the load fails? ;) > > On Mar 8, 3:03 pm, abehrens <abehr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Greetings > > > I have a javascript problem concerning a possible memory leak. I am > > using Ajax.Request to get a JSON object from a server and then passing > > the data from the JSON object to a few div''s. This is done every > > second, but over time it seems my code leaks somewhere (in the > > browser). > > > I have racked my brain trying to figure this one out...can anyone tell > > from this snippet what might be wrong: > > new Ajax.Request(''getData?time='' + dateTime, > > { > > method:''get'', > > onSuccess: function(transport){ > > var json = transport.responseText.evalJSON(); > > $(''statusBanner'').update(); > > $(''Month1'').update(responseJSON.Month1); > > > $(''Month1V'').update(json.Month1V); > > $(''Month2'').update(json.Month2); > > $(''Month2V'').update(json.Month2V); > > $(''Month3'').update(json.Month3); > > $(''Month3V'').update(json.Month3V); > > > $(''L1V'').update(json.L1V); > > $(''L2V'').update(json.L2V); > > $(''L3V'').update(json.L3V); > > > $(''Spread1'').update(json.Spread1); > > $(''Spread1V'').update(json.Spread1V); > > $(''Spread2'').update(json.Spread2); > > $(''Spread2V'').update(json.Spread2V); > > $(''Spread3'').update(json.Spread3); > > $(''Spread3V'').update(json.Spread3V); > > delete json; > > > }, > > > onFailure: function(){ > > $(''statusBanner'').update("<h1>Loading</h1>"); > > > } > > }); > > > Many many thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
No problem. Let the list know if making those anonymous inline functions named page-scope functions improves things; useful info... Happy coding! -- T.J. :-) On Mar 8, 9:18 pm, abehrens <abehr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for the help TJ. A couple things... > > The code calls a function called cycle() on window load, which has the > request in it called once per second (using setTimeout(cycle(), > 1000)). That Ajax.Request is inside cycle(). > > I am not averse to moving the heavy lifting outside the Ajax.Request, > I will give that a shot. > > I could not find any examples of how to easily take a bunch of > attributes on a JSON object and plop them in a page from the prototype > examples. I was hoping somebody could point me in that direction, as > well. > > As for the "Loading" text, the page this is served on will only > display that at program startup, as the backend giving up the data > will error out for a bit until a connection is made to a datasource. > So the failure will only happen in one case (except if my backend goes > down), but I can see situations where the text will be misleading. It > is intentional for lack of better options. > > Thanks! > > On Mar 8, 11:16 am, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Oh, and forgot to say: Do you *really* mean to be saying "Loading" > > when the load fails? ;) > > > On Mar 8, 3:03 pm, abehrens <abehr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Greetings > > > > I have a javascript problem concerning a possible memory leak. I am > > > using Ajax.Request to get a JSON object from a server and then passing > > > the data from the JSON object to a few div''s. This is done every > > > second, but over time it seems my code leaks somewhere (in the > > > browser). > > > > I have racked my brain trying to figure this one out...can anyone tell > > > from this snippet what might be wrong: > > > new Ajax.Request(''getData?time='' + dateTime, > > > { > > > method:''get'', > > > onSuccess: function(transport){ > > > var json = transport.responseText.evalJSON(); > > > $(''statusBanner'').update(); > > > $(''Month1'').update(responseJSON.Month1); > > > > $(''Month1V'').update(json.Month1V); > > > $(''Month2'').update(json.Month2); > > > $(''Month2V'').update(json.Month2V); > > > $(''Month3'').update(json.Month3); > > > $(''Month3V'').update(json.Month3V); > > > > $(''L1V'').update(json.L1V); > > > $(''L2V'').update(json.L2V); > > > $(''L3V'').update(json.L3V); > > > > $(''Spread1'').update(json.Spread1); > > > $(''Spread1V'').update(json.Spread1V); > > > $(''Spread2'').update(json.Spread2); > > > $(''Spread2V'').update(json.Spread2V); > > > $(''Spread3'').update(json.Spread3); > > > $(''Spread3V'').update(json.Spread3V); > > > delete json; > > > > }, > > > > onFailure: function(){ > > > $(''statusBanner'').update("<h1>Loading</h1>"); > > > > } > > > }); > > > > Many many thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
All that repetition doesn''t look good at all. Maybe something like this? function handleUpdateSuccess(r) { var json = r.responseText.evalJSON(), element; $(''statusBanner'').update(); for (var name in json) { element = $(name); element && element.update(json[name]); } json = null; } - kangax On Mar 8, 1:15 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Where is that Ajax.Request object created? In a function somewhere > that gets called over and over? If so, is there any particular reason > the success handler has to be an inline function? (I don''t > immediately see it referencing anything outside of itself that it > would need to be a closure, for instance.) It shouldn''t be a problem > provided nothing''s holding on to the function or the Ajax.Request > after it completes, but that doesn''t mean that the GC is going to be > perfect in every case and in fact Firefox 2 has several known leaks > around this stuff. I''d try this: > > At global (page/script) scope: > > function handleUpdateSuccess(transport){ > varjson= transport.responseText.evalJSON(); > $(''statusBanner'').update(); > $(''Month1'').update(responseJSON.Month1); > > $(''Month1V'').update(json.Month1V); > $(''Month2'').update(json.Month2); > $(''Month2V'').update(json.Month2V); > $(''Month3'').update(json.Month3); > $(''Month3V'').update(json.Month3V); > > $(''L1V'').update(json.L1V); > $(''L2V'').update(json.L2V); > $(''L3V'').update(json.L3V); > > $(''Spread1'').update(json.Spread1); > $(''Spread1V'').update(json.Spread1V); > $(''Spread2'').update(json.Spread2); > $(''Spread2V'').update(json.Spread2V); > $(''Spread3'').update(json.Spread3); > $(''Spread3V'').update(json.Spread3V); > deletejson; > > } > > function handleUpdateFailure() > { > $(''statusBanner'').update("<h1>Loading</h1>"); > > } > > And then where your request is created: > > new Ajax.Request(''getData?time='' + dateTime, > { > method:''get'', > onSuccess: handleUpdateSuccess, > > onFailure: handleUpdateFailure > > }); > > FWIW, might make no difference, might help. > -- > T.J. Crowder > tj / crowder software / com > > On Mar 8, 3:03 pm, abehrens <abehr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Greetings > > > I have a javascript problem concerning a possible memoryleak. I am > > using Ajax.Request to get aJSONobject from a server and then passing > > the data from theJSONobject to a few div''s. This is done every > > second, but over time it seems my code leaks somewhere (in the > > browser). > > > I have racked my brain trying to figure this one out...can anyone tell > > from this snippet what might be wrong: > > new Ajax.Request(''getData?time='' + dateTime, > > { > > method:''get'', > > onSuccess: function(transport){ > > varjson= transport.responseText.evalJSON(); > > $(''statusBanner'').update(); > > $(''Month1'').update(responseJSON.Month1); > > > $(''Month1V'').update(json.Month1V); > > $(''Month2'').update(json.Month2); > > $(''Month2V'').update(json.Month2V); > > $(''Month3'').update(json.Month3); > > $(''Month3V'').update(json.Month3V); > > > $(''L1V'').update(json.L1V); > > $(''L2V'').update(json.L2V); > > $(''L3V'').update(json.L3V); > > > $(''Spread1'').update(json.Spread1); > > $(''Spread1V'').update(json.Spread1V); > > $(''Spread2'').update(json.Spread2); > > $(''Spread2V'').update(json.Spread2V); > > $(''Spread3'').update(json.Spread3); > > $(''Spread3V'').update(json.Spread3V); > > deletejson; > > > }, > > > onFailure: function(){ > > $(''statusBanner'').update("<h1>Loading</h1>"); > > > } > > }); > > > Many many thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
@Kangax: Er, quite. :) I was so focussed on the memory thing that I completely missed the elements having the same name as their JSON counterparts! Nice one. -- T.J. :-) On Mar 11, 2:51 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> All that repetition doesn''t look good at all. > Maybe something like this? > > function handleUpdateSuccess(r) { > var json = r.responseText.evalJSON(), element; > $(''statusBanner'').update(); > for (var name in json) { > element = $(name); > element && element.update(json[name]); > } > json = null; > > } > > - kangax > > On Mar 8, 1:15 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Where is that Ajax.Request object created? In a function somewhere > > that gets called over and over? If so, is there any particular reason > > the success handler has to be an inline function? (I don''t > > immediately see it referencing anything outside of itself that it > > would need to be a closure, for instance.) It shouldn''t be a problem > > provided nothing''s holding on to the function or the Ajax.Request > > after it completes, but that doesn''t mean that the GC is going to be > > perfect in every case and in fact Firefox 2 has several known leaks > > around this stuff. I''d try this: > > > At global (page/script) scope: > > > function handleUpdateSuccess(transport){ > > varjson= transport.responseText.evalJSON(); > > $(''statusBanner'').update(); > > $(''Month1'').update(responseJSON.Month1); > > > $(''Month1V'').update(json.Month1V); > > $(''Month2'').update(json.Month2); > > $(''Month2V'').update(json.Month2V); > > $(''Month3'').update(json.Month3); > > $(''Month3V'').update(json.Month3V); > > > $(''L1V'').update(json.L1V); > > $(''L2V'').update(json.L2V); > > $(''L3V'').update(json.L3V); > > > $(''Spread1'').update(json.Spread1); > > $(''Spread1V'').update(json.Spread1V); > > $(''Spread2'').update(json.Spread2); > > $(''Spread2V'').update(json.Spread2V); > > $(''Spread3'').update(json.Spread3); > > $(''Spread3V'').update(json.Spread3V); > > deletejson; > > > } > > > function handleUpdateFailure() > > { > > $(''statusBanner'').update("<h1>Loading</h1>"); > > > } > > > And then where your request is created: > > > new Ajax.Request(''getData?time='' + dateTime, > > { > > method:''get'', > > onSuccess: handleUpdateSuccess, > > > onFailure: handleUpdateFailure > > > }); > > > FWIW, might make no difference, might help. > > -- > > T.J. Crowder > > tj / crowder software / com > > > On Mar 8, 3:03 pm, abehrens <abehr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Greetings > > > > I have a javascript problem concerning a possible memoryleak. I am > > > using Ajax.Request to get aJSONobject from a server and then passing > > > the data from theJSONobject to a few div''s. This is done every > > > second, but over time it seems my code leaks somewhere (in the > > > browser). > > > > I have racked my brain trying to figure this one out...can anyone tell > > > from this snippet what might be wrong: > > > new Ajax.Request(''getData?time='' + dateTime, > > > { > > > method:''get'', > > > onSuccess: function(transport){ > > > varjson= transport.responseText.evalJSON(); > > > $(''statusBanner'').update(); > > > $(''Month1'').update(responseJSON.Month1); > > > > $(''Month1V'').update(json.Month1V); > > > $(''Month2'').update(json.Month2); > > > $(''Month2V'').update(json.Month2V); > > > $(''Month3'').update(json.Month3); > > > $(''Month3V'').update(json.Month3V); > > > > $(''L1V'').update(json.L1V); > > > $(''L2V'').update(json.L2V); > > > $(''L3V'').update(json.L3V); > > > > $(''Spread1'').update(json.Spread1); > > > $(''Spread1V'').update(json.Spread1V); > > > $(''Spread2'').update(json.Spread2); > > > $(''Spread2V'').update(json.Spread2V); > > > $(''Spread3'').update(json.Spread3); > > > $(''Spread3V'').update(json.Spread3V); > > > deletejson; > > > > }, > > > > onFailure: function(){ > > > $(''statusBanner'').update("<h1>Loading</h1>"); > > > > } > > > }); > > > > Many many thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---