views:

50

answers:

2

I'm working on a realtime media browsing/playback application that uses <video> objects in the browser for playback, when available.

I'm using a mix of straight javascript, and jQuery,

My concern is specifically with memory. The application never reloads in the window, and the user can watch many videos, so memory management becomes a large concern over time. In testing today, I see the memory profile jumping by the size of the video to be streamed with each subsequent load, and never dropping back down to the baseline.

I've tried the following things with the same result:

1 - Empty the parent container containing the created element, eg:

$(container_selector).empty();

2 - Pause and remove children matching 'video', and then empty the parent container:

$(container_selector).children().filter("video").each(function(){
    this.pause();
    $(this).remove();
});
$(container_selector).empty();

Has anyone else run into this issue, and is there a better way to do this?

+2  A: 

Don't know this would help, but perhaps it would make those video container objects available for garbage collection. In any case your results are likely to vary by browser type:

$(container_selector).children().filter("video").each(function(){
    this.pause(); // can't hurt
    delete(this); // @sparkey reports that this did the trick!
    $(this).remove(); // not sure if this works after null assignment
});
$(container_selector).empty();
Ken Redler
I'll give this a shot -- thanks!
sparkey0
Okay, so you put me on the right track -- this = null;did not work, but delete(this);did!
sparkey0
Glad to hear it. I'll update the answer to include your findings.
Ken Redler
Thanks again! Btw, this=null threw an error -- should be avoided.
sparkey0
The `delete` statement simply won't do anything because it only deletes properties. It would be exactly the same, I believe, if it were removed.
Casey Hope
Yeah, that sounds familiar. I had some recollection that delete only acted on variables set with var -- but he said it worked, so...
Ken Redler
Oddly enough it worked - might just be a combination of quirks in Safari or WebKit. Removing that line causes memory to creep up continuously, with it in, it will increase, then drop suddenly, i'm assuming when the gc runs periodically
sparkey0
+1  A: 

Just to clarify for anyone trying this later, the solution was this: (confirmed with h264 videos in Safari 5.0, untested in FF/opera yet)

$(container_selector).children().filter("video").each(function(){
    this.pause();
    delete(this);
    $(this).remove();
});
$(container_selector).empty();
sparkey0