This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Using $timeout causes a memory leak #1522
Closed
Description
After much fun learning about memory leaks and how to use chrome's profiler and timeline. I finally found out the cause of my memory leak.
It appears that natively, a memory leak is created when passing a string to setTimeout
instead of a function reference: http://pavel.kuzub.com/settimeout-memory-leak
An example of the code I was using that was faulty:
doTime = ->
now = new Date()
hour = if now.getHours().toString().length == 1 then "0" + now.getHours() else now.getHours()
minute = if now.getMinutes().toString().length == 1 then "0" + now.getMinutes() else now.getMinutes()
second = if now.getSeconds().toString().length == 1 then "0" + now.getSeconds() else now.getSeconds()
$scope.clock = "#{hour}:#{minute}:#{second}"
$timeout doTime, 2000
doTime()
changing
$timeout doTime, 2000
to
setTimeout doTime, 2000
Fixed the issue.
I assume that there is probably a call to $scope.$eval
somewhere in $timeout
which is what is causing the issue.
Metadata
Metadata
Assignees
Labels
No labels