Question

Updated after obvious error pointed out by John Palmer in the comments.

The following code results in OutOfMemoryException:

let agent = MailboxProcessor<string>.Start(fun agent ->

    let maxLength = 1000

    let rec loop (state: string list) i = async {
        let! msg = agent.Receive()

        try        
            printfn "received message: %s, iteration: %i, length: %i" msg i state.Length
            let newState = state |> Seq.truncate maxLength |> Seq.toList
            return! loop (msg::newState) (i+1)
        with
        | ex -> 
            printfn "%A" ex
            return! loop state (i+1)
    }

    loop [] 0
)

let greeting = "hello"

while true do
    agent.Post greeting
    System.Threading.Thread.Sleep(1) // avoid piling up greetings before they are output

The error is gone if I don't use try/catch block.

Increasing the sleep time only postpones the error.

Update 2: I guess the issue here is that the function stops being tail recursive as the recursive call is no longer the last one to execute. Would be nice for somebody with more F# experience to desugar it as I'm sure this is a common memory-leak situation in F# agents as the code is very simple and generic.

Was it helpful?

Solution

Solution:

It turned out to be a part of a bigger problem: the function can't be tail-recursive if the recursive call is made within try/catch block as it has to be able to unroll the stack if the exception is thrown and thus has to save call stack information.

More details here:

Tail recursion and exceptions in F#

Properly rewritten code (separate try/catch and return):

let agent = MailboxProcessor<string>.Start(fun agent ->

    let maxLength = 1000

    let rec loop (state: string list) i = async {
        let! msg = agent.Receive()

        let newState = 
            try        
                printfn "received message: %s, iteration: %i, length: %i" msg i state.Length
                let truncatedState = state |> Seq.truncate maxLength |> Seq.toList
                msg::truncatedState
            with
            | ex -> 
                printfn "%A" ex
                state

        return! loop newState (i+1)
    }

    loop [] 0
)

OTHER TIPS

I suspect the issue is actually here:

while true do
    agent.Post "hello"

All the "hello"s that you post have to be stored in memory somewhere and will be pushed much faster than the output can happen with printf

See my old post here http://vaskir.blogspot.ru/2013/02/recursion-and-trywithfinally-blocks.html

  • random chars in order to satisfy this site rules *

Basically anything that is done after the return (like a try/with/finally/dispose) will prevent tail calls.

See https://blogs.msdn.microsoft.com/fsharpteam/2011/07/08/tail-calls-in-f/

There is also work underway to have the compiler warn about lack of tail recursion: https://github.com/fsharp/fslang-design/issues/82

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top