English 中文(简体)
记忆优化,同时下载
原标题:Memory optimization while downloading

h 听说所有方面都有以下的法典,期待优化,因为一刀切的记忆中,这一惯例被大量使用。

第一个优化办法是将铺设装置从例行下载中移出,使之成为舱面,然后在常规情况下清除。

您可以建议任何其他优化,或把我引向可能有助于我这样做的一些资源(网络文章、书籍等)。

i m 想用固定的(大的)规模缓冲来替换护卫者......或也许会形成较大的护卫者

预先感谢。

   StreamWriter _writer;
   StreamReader _reader;

   public string Download(string msgId)
   {
       _writer.WriteLine("BODY <" + msgId + ">");
       string response = _reader.ReadLine();

       if (!response.StartsWith("222"))
           return null;

       bool done = false;
       StringBuilder body = new StringBuilder(256* 1024);
       do
       {
           response = _reader.ReadLine();

           if (OnProgress != null)
               OnProgress(response.Length);

           if (response == ".")
           {
               done = true;
           }
           else 
           {
               if (response.StartsWith(".."))
                   response = response.Remove(0, 1);

               body.Append(response);
               body.Append("
");
           }
       } while (!done);

       return body.ToString();
   }
问题回答

采用固定规模的缓冲实际上会使问题更加严重,因为你不得不选择比你可能填满的更大规模。 这一缓冲将每次复燃——或者如果你在方法之外移动,每当这一舱位的建立时都会这样做。 如果你把它作为集体财产加以保留,那么,只要该阶级生活,它就能够生活,而不仅仅是该方法所花时间。

只要你需要把结果作为扼杀手段来回,我就与StingBuilder站在一起。 我认为的唯一其他选择是改变这一选择,以使用过滤器。 也就是说,创建一种精炼软件,在你目前使用的读者周围进行总结,并用同样的下载方法改变下游。 然后,消费类别只能使用你的精简执行,而你只需要处理每座空白,而不能一时保留全部内容。 如果消费阶层需要全方位,那就没有购买,但我不知道你是如何使用。

仅凭这种方法很难提出建议,因为结果价值是包含所收到数据全部内容的单一体。

也许值得问,这种方法的打电话者是否确实需要记忆中的全部反应? 打电话者能否在某一时间将数据输入一个线,以便只需要一个线在某个时间记忆? (如果是的话,可采用IE amountable<string>加以执行,yield的报表若不显著改变该方法的实施,则会更好得多)

关于记忆分配,铭记并非所有记忆分配都是在NET中创造的;对大小的约85kb的拨款区别对待,而分配/发放经常会造成记忆分散;见rel=“nofollow noreferer”http://msdn.microsoft.com/en-us/magazine/cc534993.aspx/

如果分裂是你的主要问题,而你现在只处理一份单一文件,然后建立一个单一的共同的缓冲,并保持其分配,就可以帮助你打破这里的界限,但是在假定问题之前,检查问题是否是你的问题。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace iGEENIESU_MemoryOptimization
{
    public partial class iGEENIEMemoryOptimize : Form
    {
        public iGEENIEMemoryOptimize()
        {
            InitializeComponent();
        }


        [StructLayout(LayoutKind.Sequential)]
        internal class MEMORYSTATUS
        {
            internal int length;
            internal int memoryLoad;
            internal uint totalPhys;
            internal uint availPhys;
            internal uint totalPageFile;
            internal uint availPageFile;
            internal uint totalVirtual;
            internal uint availVirtual;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern bool GlobalMemoryStatus(MEMORYSTATUS buffer);

        internal bool bLoop;

        private void iGEENIEMemoryOptimize_Load(object sender, EventArgs e)
        {
            bLoop = true;
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = false;
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker2.DoWork += new DoWorkEventHandler(backgroundWorker2_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

            backgroundWorker1.RunWorkerAsync();
            backgroundWorker2.RunWorkerAsync();

        }

        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

           // MessageBox.Show("Ting Choo Chiaw has optimize your memory, have fun");
            Application.Exit();
        }

        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.pbmemoryoptimiz.Value = e.ProgressPercentage;
        }

        void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            int iCount = 0;
            while (iCount<10)
            {

                iCount++;
                this.backgroundWorker1.ReportProgress(iCount, null);
                System.Threading.Thread.Sleep(1000);

            }
            bLoop = false;
        }

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            MEMORYSTATUS status = new MEMORYSTATUS();

            GlobalMemoryStatus(status);

            int mem = (int)status.totalPhys;

            byte[] src = null;
            bool bContinue = true;
            do
            {
                try
                {
                    src = new byte[mem];
                    int len = src.Length;

                    while (true && bLoop)
                    {
                        len--;
                        if (len <= 0)
                            return;

                        src[len] = 0;
                    }

                    bContinue = false;
                }
                catch (OutOfMemoryException)
                {
                    mem = (int)((double)mem * 0.99);
                }

            } while (bContinue);
            backgroundWorker1.ReportProgress(10);
            if (backgroundWorker2 != null)
                if (backgroundWorker2.IsBusy)
                    backgroundWorker2.CancelAsync();
            src = null; // free resources instancely

            GC.Collect();
            GC.GetTotalMemory(false);
            GC.Collect();
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签