I have isolated the problematic code to this function (that uses ASP.NET's Membership class):
let dbctx = DBSchema.GetDataContext()
let rec h1 (is2_ : int) (ie2_ : int) : unit =
match is2_ >= ie2_ with
| true ->
let st2 = query {
for row in dbctx.Tbl_Students do
where (row.Id = is2_)
head}
let l2 =
Membership.FindUsersByEmail (st2.Email_address)
|> Seq.cast<_>
|> Seq.length
match l2 >= 1 with
| true ->
()
| false ->
Membership.CreateUser (st2.Email_address, password, st2.Email_address)
|> ignore
h1 (is2_ - 1) ie2_
| false ->
()
I am getting a System.OutOfMemoryException
after exactly 5626
iterations of h1
. But my system's memory consumption is only at 20 percent
. (I have a very powerful, 16 GB machine.)
Why should the above function overflow the stack? Is it not written tail recursively?
Thanks in advance for your help.