Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document Dom tree vs Detached Dom tree?

Am finding memory leaks using chrome(newbie :))

How to identify where the memory is leaking?

And what is Document DOM tree and Detached Dom tree?

Can any one explain me?

like image 816
NkS Avatar asked Apr 14 '14 10:04

NkS


People also ask

What is detached DOM tree?

Detached DOM elements are the elements which have been removed from the DOM but their memory is still retained because of JavaScript. This means that as long the element have a reference to any variable or an object anywhere, it does not garbage collected even after destroyed from the DOM.

What is detached internal node?

A node is said to be "detached" when it's removed from the DOM tree but some JavaScript still references it. Detached DOM nodes are a common cause of memory leaks. This section teaches you how to use DevTools' heap profilers to identify detached nodes.

What type of tree is DOM?

The Document Object Model (DOM) is a cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document with a logical tree.

How is tree structure formed in DOM?

An example of the DOMTags are element nodes (or just elements) and form the tree structure: <html> is at the root, then <head> and <body> are its children, etc. The text inside elements forms text nodes, labelled as #text . A text node contains only a string. It may not have children and is always a leaf of the tree.


1 Answers

Steps to identify memory leaks.

  1. Start with incognitive mode Chrome.
  2. Bring up your application Open
  3. Open Chrome Dev tool ( I like to open it full maximized in its own window)
  4. Click on Profile Chrome tab - Profile
  5. Use Take Heap Snapshot option and Click Snapshot
  6. Do some specific steps with your app
  7. Take another snapshot by clicking on the black circle on the top left corner.
  8. Repeat step 5 to 7 for 2 more times
  9. Check the Retained Size - if it keeps on increasing - you have a memory problem
  10. Under Timeline tab click on Garbage Collector button.
  11. Take another snapshot and see if memory comes down to a reasonable level.

Steps to identify Detached DOM Trees

  1. To detect Detached DOM trees click on the Profile tab on one of the Snapshots
  2. You will see lot of objects - try filtering them by typing DOM as shown filtering objects DOM
  3. If you see any Detached DOM Trees then you "may" have a problem. It is possible that what you are doing might require you to keep some DOM aside for cloning etc. So this must be ruled out. If you see any DOMs beyond this you need to worry about them, especially if this is a Single Page Application, because other apps reload the whole thing soon and that may clear the memory.
  4. You can inspect the detached DOM trees as shown below. Detached DOM Trees

  5. To check what DOM is it you may want to hover over the Red HTML elements as shown below. This helps in debugging once you located the DOM showing a HTMLFormElement

What are "Document DOM Trees" ? The whole Document is a big DOM Tree inside . Document is XML and the tags are nested thus forming a tree. (DOM - Document Object Model.)

So what are "Detached DOM Trees" ?

HTML elements are instance of objects that consume memory. Each such element can store on them event listeners and data associated with it. Now "Detached DOM Trees" are nothing but DOMs that are in the Browser's memory but are NOT attached to the main DOM tree aka "Document DOM Trees".

By examining these hanging objects we can detect issues and avoid memory leaks.

Solving this problem is a vast topic as you may see some varied solutions out there. Follow following posts for what some people have done to remove the problem.

Can't seem to cleanup detached DOM elements

Javascript Memory Leaks: Detached DOM tree

like image 200
bhantol Avatar answered Oct 21 '22 19:10

bhantol