我已分离出有问题的代码,这个功能(使用ASP.NET的Membership类):的System.OutOfMemoryException一个尾递归函数
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 ->
()
我得到一个确切后5626
System.OutOfMemoryException
迭代h1
。但是我的系统内存消耗只有在20 percent
。 (我有一个非常强大的16GB机器。)
为什么上面的函数应该溢出栈?它是不是递归地写尾?
在此先感谢您的帮助。
你在Debug模式下运行吗?如果是这样,尾巴呼叫被禁用。在发布模式下试用您的代码。 – Daniel
我会用'Seq.isEmpty'重写最后一部分。没有必要列举整个序列。 – pad
@丹尼尔是这样吗?我在项目中有许多其他的尾递归函数,它们在相同的调试模式下遍历更大的深度而不会引发错误。不过,我会检查是否是这个问题。感谢您的建议。 – Shredderroy