I have a rather large code, and I wanted to see, if it performs correctly memory wise, using the Chrome Developer Tools, Memory tab. So I did a little digging there, and I am experiencing something really strange, which I don’t know how exactly to interpret.
Basically, what I realized, is that I have a class, named Animator, which is responsible to watch for animation end events, when the code is animating an object, via CSS. It can do various different things, but in it’s most basic form it is to watch and wait for animations to perform, in jQuery.
Here’s the whole class:
class Animator {
protected settings: animatorOptions;
protected watchers: watcherData[] = [];
constructor(settings: animatorOptions) {
this.settings = settings;
if (this.settings.$watchers !== undefined) {
for (const $watch of this.settings.$watchers) this.watch($watch);
}
}
/** Add a new watch to the animator
*
*/
watch(option: JQuery<HTMLElement> | watcherOption) {
const $elemGroup: JQuery<HTMLElement> = utils.isjQuery(option) ? option : option.$watchedElem;
$elemGroup.each((_index, element) => {
const watchOption: watcherOption = jQuery.extend<any, watcherOption>(true, utils.isjQuery(option) ? {} : option, {
$watchedElem: jQuery(element)
});
if (watchOption.animationEvents === undefined) watchOption.animationEvents = this.getWatchEventsForGroup(watchOption.animationGroup ?? this.settings.animationGroup);
this.watchers.push(watchOption);
});
return this;
}
/** Initiate the animation. The function resolves to a boolean, indicating if the animation timed out or not. Meaning, that if this function resolves to "true", treat it as the animation timed out, if it resolves to "false", treat it as the animation is finished. This function will never reject.
*
*/
async animate(initer?: () => void): Promise<boolean> {
const promises: Promise<any>[] = [];
for (const watch of this.watchers) {
watch.Deferred = new Deferred();
watch.animationEnderFunction = (evt) => {
evt.stopPropagation();
if (evt.target !== evt.currentTarget) return;
watch.Deferred.resolve(true);
if (watch.onAnimationEnd) watch.onAnimationEnd.apply(this, [evt]);
watch.$watchedElem.off(watch.animationEvents, watch.animationEnderFunction);
}
watch.$watchedElem.on(watch.animationEvents, watch.animationEnderFunction);
promises.push(watch.Deferred.promise);
}
return new Promise((resolve, reject) => {
let isResolved: boolean = false;
let timeout: any;
const resolveSuccesfully = () => {
if (!isResolved) resolve(false);
isResolved = true;
if (timeout !== undefined) clearTimeout(timeout);
}
const resolveTimeout = () => {
if (!isResolved) resolve(true);
for (const watch of this.watchers) watch.$watchedElem.off(watch.animationEvents, watch.animationEnderFunction);
isResolved = true;
}
if (initer) initer.apply(this, []);
for (const watch of this.watchers) {
if (watch.redraw) watch.$watchedElem.yellow().redraw();
const animationGroupType = watch.animationGroup ?? this.settings.animationGroup;
if (animationGroupType !== null && watch.safeMode !== undefined && watch.safeMode === true) {
const computedStyles = window.getComputedStyle(watch.$watchedElem[0]);
const autoResolve = (
(animationGroupType === 'animation' && ['0s', ''].includes(computedStyles.getPropertyValue('animation-duration'))) ||
(animationGroupType === 'transition' && ['0s', ''].includes(computedStyles.getPropertyValue('transition-duration')))
);
if (autoResolve) {
watch.$watchedElem.off(watch.animationEvents, watch.animationEnderFunction);
watch.Deferred.resolve(true);
if (watch.onAnimationEnd) watch.onAnimationEnd.apply(this, [true]);
}
}
}
if (this.settings.timeout !== undefined) {
timeout = setTimeout(() => {
resolveTimeout();
}, this.settings.timeout);
}
Promise.all(promises).then(() => {
resolveSuccesfully();
});
});
}
/** Get the list of event listeners for a specific watch group
*
*/
private getWatchEventsForGroup(group: predefinedAnimationGroups): string {
return group === 'animation' ? 'animationend webkitAnimationEnd oanimationend MSAnimationEnd mozAnimationEnd' : 'transitionend webkitTransitonEnd otransitionend MSTransitionEnd mozTransitionEnd';
}
}
This class is usually used in my code, in the following way:
...
await new Animator({animationGroup: 'animation', $watchers: [$outScreen]}).animate(() => {
$outScreen.addClass(animationEffect.outClass);
});
What I was expecting, that when I take a snapshot of my page, in Chrome DevTools, I should see no Animator class instances at all, because after the animation finishes, nothing is referencing the class anymore, and it should be garbage collected, but this is not the case. Each and every instantiation of this class appears in the snapshot taken with Chrome.
Here is my first question: does this indeed mean, that I have some kind of memory leak here, and the instantiated Animator classes, are not garbage collected, or I am misunderstanding the usage of the Memory tab in Chrome Dev Tools?
Continuing with the assumption, that these instances are indeed not garbage collected, I followed along, and checked the Retainers tab. If I am not mistaken, this should show me other variables or places in my code, which are still referencing a specific instance, and why it can’t be garbage collected at the moment, but what I found here, is even stranger (if my assumption about how retainers work is correct). Here’s a screenshot of what I am seeing in the dev tools:

Based on what Chrome is showing me, there are still 10 instances of the Animator class, inside the memory, and what is holding them there, are themselves. The two retainers I see here, are pointing to the following lines of code:
// Animator.ts 80
watch.animationEnderFunction = (evt) => {
evt.stopPropagation();
if (evt.target !== evt.currentTarget) return;
watch.Deferred.resolve(true);
if (watch.onAnimationEnd) watch.onAnimationEnd.apply(this, [evt]);
watch.$watchedElem.off(watch.animationEvents, watch.animationEnderFunction);
}
This is the closure, which should fire when an animation ends, and it is indeed referencing itself, but the other one, points to the for loop, two lines above this code. Why is my for loop, keeping the whole instance in the memory?
So, basically, there are many things to which I am looking for an answer on:
- Am I correct, that if I can find a variable, with a class instance in the Chrome Dev Tools Memory tab, it means that the respective instance was not garbage collected?
- When checking for the retainers of that variable, it should show me what other variables are still referencing this one, and thus why it was not garbage collected yet?
- How can a for loop prevent something from garbage collection?