Skip to content
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
@jaitaiwan

Description

@jaitaiwan

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions