我正在读取一个二进制文件。如果我阅读一个文件,我的代码可以正常工作。但是当我读取文件夹中的文件时,我得到了错误,它粘贴在下面。我将添加一些用于读取文件的代码。我正在读取字节数组中的文件:
byte[] b = File.ReadAllBytes(args);
并将数据存储在数组中(指示性代码):
len=400000000;
public ArrayList rawData1 = new ArrayList();
UInt32[] fff = Enumerable.Repeat((UInt32)4095, len/4).ToArray();
public ReadRawFiles{
while (true){
rawData1.Add((double)(BitConverter.ToUInt32(b, curPos) & fff[i]));
i++;
}
}
b=null; //clear array
如果我只读取一个文件,它就可以工作,虽然速度很慢(大小约为 40 MB)。但是当我添加一个文件夹的路径并再次阅读时,我得到了错误。
List<ReadRawFiles> list = new List<ReadRawFiles>();
ReadRawFiles rawFiles;
foreach (var f in sFiles)
{
rawFiles = new ReadRawFiles(f.File,true,true);
list.Add(rawFiles);
//rawFiles=null; //clear??
}
我不是很清楚垃圾收集器是如何工作的。如果我错过了什么,请告诉我。
提前非常感谢。
编辑:代码: http: //www.codesend.com/view/4aadd067dfd26ea88396afbd3cd3fc22/ http://www.codesend.com/view/f8f798224e54c28a00865ca9aff514e5/
System.OutOfMemoryException was unhandled
Message=Exception of type 'System.OutOfMemoryException' was thrown.
Source=System.Core
StackTrace:
at System.Linq.Buffer`1.ToArray()
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at program.ReadRawFiles..ctor(String args, Nullable`1 flagraw, Nullable`1 dualChannel) in E:\projects\development\vs_test\WpfApplication1\WpfApplication1\param.cs:line 83
at SePSI.MainWindow.Add_folder(Object sender, RoutedEventArgs e) in E:\projects\development\vs_test\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 196
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at SePSI.App.Main() in E:\projects\development\vs_test\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: