【问题标题】:How do I reduce the memory usage in this c# File Transfer?如何减少此 c# 文件传输中的内存使用量?
【发布时间】:2011-08-07 15:20:25
【问题描述】:

我有一个用 c# 编写的简单文件传输应用程序,使用 TCP 发送数据。

这是我发送文件的方式:

Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


        byte[] fileName = Encoding.UTF8.GetBytes(fName); //file name
        byte[] fileData =  new byte[1000*1024];
        byte[] fileNameLen = BitConverter.GetBytes(fileName.Length); //length of file name
        FileStream fs = new FileStream(textBox1.Text, FileMode.Open);            
        try
        {
            clientData = new byte[4 + fileName.Length];
        }
        catch (OutOfMemoryException exc)
        {
            MessageBox.Show("Out of memory");
            return;
        }

        fileNameLen.CopyTo(clientData, 0);
        fileName.CopyTo(clientData, 4);
        clientSock.Connect("172.16.12.91", 9050);
        clientSock.Send(clientData, 0, clientData.Length, SocketFlags.None);                       
        progressBar1.Maximum = (int)fs.Length;
        while (true)
        {
            int index = 0;
            while (index < fs.Length)
            {
                int bytesRead = fs.Read(fileData, index, fileData.Length - index);
                if (bytesRead == 0)
                {
                    break;
                }

                index += bytesRead;
            }
            if (index != 0)
            {
                clientSock.Send(fileData, index, SocketFlags.None);
                if ((progressBar1.Value + (1024 * 1000)) > fs.Length)
                {
                    progressBar1.Value += ((int)fs.Length - progressBar1.Value);
                }
                else
                    progressBar1.Value += (1024 * 1000);
            }

            if (index != fileData.Length)
            {
                progressBar1.Value = 0;
                clientSock.Close();
                fs.Close();

                break;

            } 
        }            

    }

在任务管理器中,当我使用 OpenFileDialog 时,此应用程序的发布版本的内存使用量变为 13 MB,然后在发送数据时上升到 16 MB,然后停留在那里。有什么办法可以减少内存使用吗?或者有什么好工具可以用来监控应用中的总分配内存?

虽然我们在那里,16 MB 真的那么高吗?

【问题讨论】:

  • 16MB 不算高。为什么要减少内存占用?有什么需要吗?似乎是一个过早的优化。

标签: c# file memory allocation transfer


【解决方案1】:

16MB 的内存使用量听起来并不多。 您可以使用内置的 Visual Studio 分析器来查看性能消耗最大的因素。有关探查器的更多信息,请参见下面的链接: http://blogs.msdn.com/b/profiler/archive/2009/06/10/write-faster-code-with-vs-2010-profiler.aspx

【讨论】:

    【解决方案2】:

    我注意到,当我最小化任何应用程序时,内存使用量会显着下降。我最终寻找了一种以编程方式复制这种效果的方法,这就是我发现的:

    [DllImport("kernel32.dll")]
            public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
            public void ReleaseMemory()
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
                }
            }
    

    我不知道使用它的缺点,但到目前为止,它设法节省了至少 13MB 的内存。

    【讨论】:

    • 我能问一下为什么吗?我是新手
    • 您永远不想强制系统进行 GC,因为您现在引入了不必要的对象集合。应该允许垃圾收集器在它想要这样做时对事物进行 GC。通过强制它,您不仅会影响应用程序的性能,还会影响其运行的系统。让运行时做它的事情,让它在它认为合适的时候做它。
    • 编辑:我不鼓励使用这种方法(除非你确实需要它的作用),因为它根本不会“减少”内存。
    猜你喜欢
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2018-07-12
    • 2021-12-12
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多