gridster.js
gridster.js copied to clipboard
Memory Leaks
I found a few memory leaks in the main branch of gridster, and thought I would send the info here since this one is maintained.
There are two memory leaks issues I found that aren't already fixed in this repo.
- Saving the result of jQuery calls to map, eq, not, add, appendTo, etc... Many of these jQuery calls store a reference the the previous selection via .prevObject . For instance, you remove a widget, and this call happens:
this.$widgets = this.$widgets.not($el);
The widget you just removed sticks around in the prevObject array on the new $widgets object. That dom object will not be garbage collected. I believe this affects this.$style_tags as well, and potentially other objects.
- the faux_grid object is expanded unlimitedly, and is never retracted. For instance, I add a 1 row tall widget. faux_grid grows by 1 to make room for it. I remove that widget, faux_grid stays the same. I add the widget back, faux_grid grows by 1 again. faux_grid is now 1 row bigger than it needs to be. Again this affects applications that add/remove widgets repeatedly. The effect is even more pronounced on the resize callback, because rows are added every time you resize larger, even if it is the same size as when you started the drag.
Possible solutions:
- Wrap all of those leaky calls with $(). This destroys the prevObject. Or just delete the prevObject property off of the selection
this.$widgets = $(this.$widgets.not($el));
//or
delete this.$widgets.prevObject;
//or
this.$widgets.prevObject = undefined;
- Add remove_faux_rows and remove_faux_columns functions, and call them when something is resized smaller, and when widgets are removed.
I found another one in the 'remove_style_tags' function. this.$style_tags.remove() removes the selection from the dom, but it does not remove them from the $style_tags object. The styles are regenerated and added to this array, but the old ones are still there. These old ones stay and memory and won't be garbage collected. Setting $style_tags to $() removes the old references.
this.$style_tags.remove();
this.$style_tags = $();
Wow.. Great information. Do you have code fixes you could submit as pull requests?
I fixed these at work, so I won't be able to share that code. I can redo it at home when I get some time though.
:+1: @KFanPrecalcMan : Would love to see these changes implemented.