即使清除了所有对象列表,我的应用程序使用内存仍然很高

时间:2015-11-18 12:09:36

标签: wpf

所以我有Base Class

public abstract class WiresharkFile : IDisposable
{
    private string _fileName;
    private int _packets;
    private int _packetsSent;
    private string _duration;
    private double _speed;
    private int _progress;
    protected abstract WiresharkFilePacket ReadPacket();
    public abstract IEnumerator<WiresharkFilePacket> GetEnumerator();
    public abstract void Rewind();

    public string FileName
    {
        get { return _fileName; }
        set { _fileName = value; }
    }

    public int Packets
    {
        get { return _packets; }
        set { _packets = value; }
    }

    public void Dispose()
    {
        // implemented inside sub class.
    }
}

具体的Wireshark格式(libpcap):

public class Libpcap : WiresharkFile, IDisposable, IEnumerable<WiresharkFilePacket>
{
    private BinaryReader binaryReader;
    private Version version;
    private uint snaplen;
    private int thiszone;
    private uint sigfigs;
    private LibpcapLinkType linktype;
    private long basePos;
    private bool byteSwap;
    private static uint MAGIC = 0xa1b2c3d4;
    private static uint MAGIC_ENDIAN = 0xd4c3b2a1;

    public Libpcap(string path)
        : this(new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        FileName = path;
    }


    private Libpcap(Stream fileStream)
    {
        ...
    }

    public override void Rewind()
    {
        binaryReader = new BinaryReader(new FileStream(FileName, FileMode.Open, FileAccess.Read));
        binaryReader.BaseStream.Position = basePos;
    }

    public void Dispose()
    {
        if (binaryReader != null)
            binaryReader.Close();
    }
  

我删除了几乎所有阅读此文件的部分

将文件添加到我的应用程序

我有这个对象列表:

public ObservableCollection<WiresharkFile> wiresharkFiles { get; set; }

此列表绑定到我的ListView

当用户选择要添加到我的应用程序中的文件时:

string[] files = openFileDialog.FileNames;

我通过另一个班级检查这些文件:

public class FileValidation
{
    public static void DoWork(IEnumerable<string> files)
    {
        CancellationTokenSource tokenSource = new CancellationTokenSource();
        CancellationToken token = tokenSource.Token;
        Task task = Task.Factory.StartNew(() =>
        {
            try
            {
                Parallel.ForEach(files,
                new ParallelOptions
                {
                    MaxDegreeOfParallelism = 3
                },
            file =>
            {
                ProcessFile(file);
            });
            }
            catch (Exception)
            { }

        }, tokenSource.Token,
           TaskCreationOptions.None,
           TaskScheduler.Default).ContinueWith
            (t =>
            {
                if (FinishValidationEventHandler != null)
                    FinishValidationEventHandler();
            }
    , TaskScheduler.FromCurrentSynchronizationContext()
    );
    }

    private static void ProcessFile(string file)
    {
         ReadWiresharkFormat(file);
         using (WiresharkFile wiresharkFile = new Libpcap(file))
         {
             WiresharkFileInfo.ReadInfo(wiresharkFile);
             // Add file into my list.
         }
    }

    private static WiresharkFileFormat ReadWiresharkFormat(string file)
    {
        using (BinaryReader binaryReader = new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read)))
        {
            // Open file and read first 4 bytes in order to verify file type.
        }
    }

    private static void ReadInfo(WiresharkFile wiresharkFile)
    {
        foreach (WiresharkFilePacket packet in wiresharkFile)
        {
            // Collect file information (number of packets...)
        }                
    }
}

好的,直到这里一切都好。 现在当添加许多文件时,让我们说(1000 + - )我可以看到我的内存使用量在200MB内增长但是在清除此列表之后内存使用量没有改变。 知道是什么原因造成的吗?

0 个答案:

没有答案
相关问题