Search code examples
f#websharperwebsharper.ui.next

Slowing performance in Websharper application, possible memory leak


The sample code below in a single page Websharper application exhibits the issue encountered in my project.

Over time, there is some action that progressively takes longer. It occurs every few seconds. After an extended period, 20 mins or more, Chrome starts alerting that setTimeout and requestAnimationFrame are taking longer than 50ms.

Observing memory graphs in Chrome, there appears to be a memory leak as the usage increases even when garbage collection is manually activated. My suspicion is that this is causing load on the regular garbage collection and causing the extended execution times.

Any ideas how to find and fix this problem?

open WebSharper
open WebSharper.JavaScript
open WebSharper.JQuery
open WebSharper.UI.Next
open WebSharper.UI.Next.Client
open WebSharper.UI.Next.Html
open WebSharper.UI.Next.Notation

[<JavaScript>]
module Client =    
    type IndexTemplate = Templating.Template<"index.html">

    type T = {
        i : int
        n : float
        d : float
    }

    let Main =
        JQuery.Of("#main").Empty().Ignore

        let v = Var.Create {
            i = 0
            n = 0.0
            d = 0.0
        }

        let rec f (n : float) =
            let w = !v
            v :=
                {w with
                    i = w.i + 1
                    n = n
                    d = n - w.n
                }
            s()
        and s () =
            JS.RequestAnimationFrame f |> ignore

        s()

        div [
            div [v.View |> View.Map (fun t -> "Frame " + string t.i) |> textView]
            div [v.View |> View.Map (fun t -> sprintf "Started: %.1f" t.n) |> textView]
            div [v.View |> View.Map (fun t -> sprintf "Duration: %.1fms" t.d) |> textView]
        ]
        |> Doc.RunById "main"

I am using Websharper 3.6.20.6, WebSharper.UI.Next 3.6.18.2 and Chrome 59.0.3071.115.


Solution

  • Thanks for the report, I have linked it to this ticket: https://github.com/intellifactory/websharper.ui.next/issues/129

    There will be a WebSharper 4 beta stack release with a fix today, and we will look into back-porting some important improvements like this to WebSharper 3.