.remove() does remove the element from the document head or body, but that
doesn't destroy it. It just makes it available for garbage collection - if
there are no other references to the element.

For example:

var $div = $('<div>test</div>');
$div.appendTo('body');
$div.remove();

Here we've created a new DIV element, appended it to the body, and then
removed it. It is no longer a part of the document body.

But the element still exists in memory! After all, somewhere later we may
repeat this code:

$div.appendTo('body');  // re-use the same DIV

Jeff, perhaps there are other references outstanding to the elements that
are being .remove()'d, preventing them from being garbage collected.

-Mike

On Wed, Jan 13, 2010 at 8:46 AM, Karl Swedberg <k...@englishrules.com>wrote:

> Hmm. .remove() doesn't just hide an element; It removes it:
>
> if ( this.parentNode ) {
> this.parentNode.removeChild( this );
>  }
> (from the source)
>
> To prevent memory leaks, it also removes event handlers associated with it.
> As of jQuery 1.4, it also removes data associated with it.
>
> Maybe something else is going on? Or maybe something isn't being caught. In
> any case, it might help to see a test case that shows the memory leak. If
> you can reproduce the problem with a minimal test case and post it publicly,
> send a note to the jquery-dev google group so the devs can have a look.
>
> thanks,
>
> --Karl
>
> ____________
> Karl Swedberg
> www.englishrules.com
> www.learningjquery.com
>
>
>
>
> On Jan 12, 2010, at 6:52 PM, sophos707 wrote:
>
> Hi everyone,
> I'm running a script that processes text messages people send in, and
> then it displays them on a screen.  It will loop the text messages
> when there are too many to show on one screen.
>
> To accomplish this I add DIVs with a new ID for each new message.  The
> new messages push the old ones off the screen eventually.  But when
> this is the case the memory in the browser just keeps increasing to a
> frightening load!
>
> I tried using $("#MyDivId").remove() to get rid of the DIVs no longer
> on the screen.  Memory keeps increasing.  Found out in this forum
> that .remove() just hides an element in the DOM and makes in
> inaccessible rather than actually deleting it.
>
> How can we actually delete elements?
> Thanks!
> - Jeff
>
>
>

Reply via email to