这段简单的代码(一个10 000 * 10 000的矩阵相乘)会出现"System.OutOfMemoryException"异常:
#time
#r "Microsoft.Office.Interop.Excel"
#r "FSharp.PowerPack.dll"
open System
open System.IO
open Microsoft.FSharp.Math
open System.Collections.Generic
let mutable Matrix1 = Matrix.create 10000 10000 0.
let matrix4 = Matrix1 * Matrix1
我有以下错误:
System.OutOfMemoryException: An exception 'System.OutOfMemoryException' has been raised
Microsoft.FSharp.Collections.Array2DModule.ZeroCreate[T](Int32 length1, Int32 length2)
Microsoft.FSharp.Math.DoubleImpl.mulDenseMatrixDS(DenseMatrix`1 a, DenseMatrix`1 b)
Microsoft.FSharp.Math.SpecializedGenericImpl.mulM[a](Matrix`1 a, Matrix`1 b)
<StartupCode$FSI_0004>.$FSI_0004.main@() dans C:\Users\XXXXXXX\documents\visual studio 2010\Projects\Library1\Library1\Module1.fs:line 92
Stop due to an error
我有两个问题:
我的电脑上有8 GB的内存,根据我的计算,一个10 000 * 10 000的矩阵应该只占用381 MB。计算方法如下:
10 000 * 10 000 = 100 000 000
个32位整型数字,共占用100 000 000 * 4 bytes = 400 000 000
。转换为MB, 即400 000 000 / (1024*1024) = 381 MB
。因此,我不理解为什么会出现“OutOfMemoryException”错误。更一般地说(我认为这里不是这种情况),我感觉 F# interactive 会注册所有数据,从而导致内存负载过高。你知道有没有一种方式可以在不退出 F# 的情况下释放 F# interactive 注册的所有数据?