DataTables destroy() memory retention: diagnosing lingering event listeners and DOM references
0 reputation · 15 Apr 2020, 03:12 UTC
0 reputation · 15 Apr 2020, 03:12 UTC
When an application repeatedly initializes and destroys DataTables instances, the documented destroy() method is the supported teardown path. The goal is to determine how completely destroy() releases internal event handlers and DOM node references, and what diagnostic criteria separate a DataTables-side retention issue from references held by surrounding application code.
Reported behavior varies across the 1.10–1.13 line: older releases are described as leaving event handlers and jQuery data references behind after destroy(), while newer 1.13 releases are said to improve listener cleanup, though DOM references may still persist in complex layouts. These claims need verification against the specific version in use. Custom extensions, third-party integrations, and framework wrappers can also hold references that destroy() does not manage, and browser differences complicate the interpretation of heap snapshots.
Specific questions:
35275 reputation · 15 Apr 2020, 08:21 UTC
1. Retained‑object patterns that point to DataTables internals
table element and expose a DataTable key in $.data(element) are almost always DataTables state.__events__ (jQuery) or __listener__ (native) structures and reference settings or api() closures are another hallmark of incomplete cleanup.__dt__ flag or whose __data__ contains a DataTable reference indicate that the table subtree was not fully detached.DataTable instance in window.DataTable or a global registry is a clear leak.2. Expected cleanup differences: 1.10.x vs 1.13.x
$.data(element, 'DataTable') and many event listeners in place; destroy() only removes the API‑attached handlers.DataTable key, removes most listeners, and detaches tbody nodes, but still requires manual unbinding of listeners added directly via on().settings objects, but 1.13.x does so earlier in the destroy flow.3. Checks that confirm garbage‑collection eligibility
destroy() and verify that $.data(table, 'DataTable') is undefined.__events__ (or __listener__) for remaining DataTables handlers; they should be absent.DataTable instance remains in window.DataTable or a custom registry.window.gc() if --js-flags=--expose-gc is enabled) and confirm that the snapshot size drops.var dt = $('#myTable').DataTable(); – create instance.dt.destroy(); – invoke the official teardown.DataTable or $.data to locate any remaining references.__events__ property for lingering handlers.destroy().Custom extensions or framework adapters can keep references that destroy() does not touch. If you’re using a wrapper that stores the DataTables instance in component state or a global store, you must clean that manually. Also, listeners added directly via $(element).on() are not automatically removed; they must be unbound explicitly.
Could you confirm whether your application uses any third‑party DataTables wrappers or custom extensions that might attach their own listeners or data to the table element? This information will help refine the cleanup checklist.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 15 Apr 2020, 05:54 UTC
destroy()In 1.13.x the core now auto‑unsubscribes all events in the .DT namespace, but extensions and your own handlers still survive. A minimal test is to:
'draw.dt' listener.table.destroy(true) (the true flag removes the table from the DOM).prototype contains DataTables.
'draw.dt' entry pointing to your closure.DataTables objects accumulate.Remember: if you’re using a framework wrapper, ensure it releases its reference to the instance as well.