-4

我正在读取一个二进制文件。如果我阅读一个文件,我的代码可以正常工作。但是当我读取文件夹中的文件时,我得到了错误,它粘贴在下面。我将添加一些用于读取文件的代码。我正在读取字节数组中的文件:

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: 
4

2 回答 2

4

这里有一些非常低效的部分;一个将整个文件加载到内存中,fff另一个将不清楚的数组加载到内存中。ArrayList保持double三分之一(涉及拳击每个-这里double非常糟糕)。

我强烈建议尝试简化和流式传输数据,例如:

byte[] buffer = new byte[4];
List<double> rawData = new List<double>();
foreach(var path in paths)
{
    using(var file = File.OpenRead(path))
    {
        while(TryRead(file, buffer, 4))
        {
            const uint MASK = 4095;
            var val = BitConverter.ToUInt32(buffer, 0) & MASK;
            // note that this line looks really dodgy
            rawData.Add((double)val);
        }
    }
}

使用实用程序方法填充整个 4 字节块(或失败):

static bool TryRead(Stream s, byte[] buffer, int bytes)
{
    int read = s.Read(buffer, 0, bytes), offset;
    if (read <= 0) return false; // no more data

    bytes -= read;
    offset = read;
    while (bytes > 0 && (read = s.Read(buffer, offset, bytes)) > 0)
    {
        bytes -= read;
        offset += read;
    }
    if (bytes != 0) throw new EndOfStreamException();
    return true; // success
}

不过,我必须强调:

  • uint转换double看起来不太可能;这不是您将双精度读为二进制的方式
  • 将所有数据存储为double听起来同样不太可能 - 我想不出任何有用的明显场景
于 2013-05-07T13:07:53.310 回答
1

我认为这将是由于:

while (true)
{
    rawData1.Add((double)(BitConverter.ToUInt32(b, curPos) & fff[i]));
    i++;
}

您没有终止条件,它将永远持续下去,并通过i增加数据并将数据添加到rawDataArrayList 中来继续消耗资源。

于 2013-05-07T12:48:04.727 回答