Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:30:41+00:00 2026-05-24T23:30:41+00:00

Would the following piece of code create a memory leak. According to the jQuery

  • 0

Would the following piece of code create a memory leak.

According to the jQuery documentation use of the data function avoids memory leaks. It would be useful to confirm whether the following is safe.

var MyClass = function(el) {
    // Store reference of element in object.
    this.element = $(el);
};

// Store reference of object in element.
$('#something').data('obj', new MyClass('#something'));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:30:41+00:00Added an answer on May 24, 2026 at 11:30 pm

    Obviously the code as it stands would take up extra memory as long as the DOM element is still connected to the DOM. But I’m guessing you’re asking whether it would continue using extra memory after the DOM element is no longer in use.

    Update: Thanks to Joey’s answer (which he has since deleted), I spent some time reading up on memory leaks in javascript, and it appears my assumptions in the paragraph below are incorrect. Because DOM elements don’t use pure garbage collection, a circular reference like this would normally prevent both the DOM element and the javascript object from ever being released. However, I believe the remainder of this answer is still correct.

    Without a deep knowledge of how javascript engines implement garbage collection, I can’t speak authoritatively on the topic. However, my general understanding of garbage collection makes me think that your code would be “safe” in the sense that after the #something element is removed from the DOM, the resulting MyClass object would only have a reference to an object that has no other connections. The graph algorithms of the garbage collector should be able to identify that the DOM element and its MyClass object are “floating in space” and unconnected to everything else.

    Furthermore, jQuery goes out of its way to strip data and events that are associated with a given DOM element once it is removed from the DOM. From the documentation:

    jQuery ensures that the data is removed when DOM elements are removed via jQuery methods, and when the user leaves the page.

    So assuming you use jQuery consistently, you would only have a one-way reference once the object is removed from the DOM anyway, which makes it that much easier possible for the garbage collector to know it can get rid of these objects.

    So as long as you don’t have something else referencing the MyClass object once the DOM element is removed, you shouldn’t have a memory leak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C, something like the following would be a disaster (ie, a memory leak)
I am using the following piece of code to create a gradient background for
Part of my spreadsheet's script runs the following piece of code: temp.getRange(1, 1).setValue(=QUERY(data!A1:H125, \Select
I want to indent the following piece of code. How would a lisper indent
I need help with the following piece of code, I would like to see
I have the following piece of code: var blah = function(x, y){ var e
Would the following SQL statement automatically create an index on Table1.Table1Column, or must one
Why would the following query return Error converting data type varchar to bigint? Doesn't
I want to create an alias for a class name. The following syntax would
Consider the following piece of code. <html> <body> <script> var x = 5; //globally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.