开发者

High memory Usage problem for wpf application

I made a WPF application that opens the CSV file and does some operation that includes webscraping and gets some values that has type long.(0-10000000)

Now the issue is that when large list of about 2000 is opened then memory usage for software raises above 700MB in some cases 1G.

I am shocked to see this.

some things I think is that

  1. If each entry of csv file has long value associated with it it will take much memory.and single entry has approx 10-12 column each is long in type.now when there are huge row count then memory shoots

  2. There are certain places in code that has a loop (on all csv rows) that creates a instance of custom class.i thought of having destructor then came to know that dot net manages memory automatically.

here goes code for loading CSV

    try
    {
        StreamReader sr = new StreamReader(path,Encoding.Default);
        labelRankCheckStatus.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
        {
            labelRankCheckStatus.Content = "Loading Data";
        }));

        string strline = "";
        string[] _values = null;
        int x = 0;

        while (!sr.EndOfStream)
        {
            x++;
            strline = sr.ReadLine();
            _values = strline.Split(',');
            if (x == 1)
            {
                textBoxKw1.Text = _values[12];
                textBoxKw2.Text = _values[14];
                textBoxKw3.Text = _values[16];
                textBoxKw4.Text = _values[18];
            }
            else if (x != 1)
            {
                if (_values[0] != "")
                {
                    Url info = new Url();
                    srNo++;
                    info.URL = idn.GetAscii(_values[0].ToString().Trim()); 
                    info.IsChecked = true;

                    info.TestResults = int.Parse(_values[1].Replace("%","").TrimEnd().TrimStart());
                        
                    info.PageRank= int.Parse(_values[2]);
                    info.RelPageRank = int.Parse(_values[3].Replace("%","").TrimEnd().TrimStart());
                        
                    info.Alexa= long.Parse(_values[4]);
                    info.RelAlexa = long.Parse(_values[5].Replace("%","").TrimEnd().TrimStart());
                        
                    info.Links= long.Parse(_values[6]);
                    info.RelLinks = long.Parse(_values[7].Replace("%","").TrimEnd().TrimStart());
                        
                    info.GIW= long.Parse(_values[8]);
                    info.RelGIW = long.Parse(_values[9].Replace("%","").TrimEnd().TrimStart());
                        
                    info.GIN= long.Parse(_values[10]);
                    开发者_运维问答info.RelGIN = long.Parse(_values[11].Replace("%","").TrimEnd().TrimStart());
                        
                    info.Kw1Indexed= long.Parse(_values[12]);
                    info.RelKw1Indexed = long.Parse(_values[13].Replace("%","").TrimEnd().TrimStart());
                        
                    info.Kw2Indexed= long.Parse(_values[14]);
                    info.RelKw2Indexed = long.Parse(_values[15].Replace("%","").TrimEnd().TrimStart());
                        
                    info.Kw3Indexed= long.Parse(_values[16]);
                    info.RelKw3Indexed = long.Parse(_values[17].Replace("%","").TrimEnd().TrimStart());
                        
                    info.Kw4Indexed= long.Parse(_values[18]);
                    info.RelKw4Indexed = long.Parse(_values[19].Replace("%","").TrimEnd().TrimStart());

                    info.DKwIndexed= long.Parse(_values[20]);
                    info.RelDKwIndexed = long.Parse(_values[21].Replace("%","").TrimEnd().TrimStart());

                    info.Info= _values[22];

                    info.srNo = srNo;
                    url.Add(info);
                }

            }
            dataGrid1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                dataGrid1.Columns[2].Header = "URL ( " + url.Count + " )";

                try
                {
                    if (dataGrid1.ItemsSource == null)
                        dataGrid1.ItemsSource = url;
                    else
                        dataGrid1.Items.Refresh();
                }
                catch (Exception)
                {
                }
                labelRankCheckStatus.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                {
                    labelRankCheckStatus.Content = "Done";
                }));
            }));

        }
        sr.Close();
        labelRankCheckStatus.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
        {
            labelRankCheckStatus.Content = "Complete ";
        }));
    }
    catch (Exception c)
    {
        MessageBox.Show(c.Message);
    }`


Instead of building in-memory copies of your large objects, consider a more functional approach where you stream data in, process it and output it to your database of choice. If you need to do operations on the old data, you can use an SQL database like Sqlite.

Creating managed objects for every single entity in your system is beyond wasteful, you won't need most of them.

Of course, if you have a lot of RAM, it might simply be that the GC isn't yet bothering to collect all your garbage because the memory isn't actively needed by anything. It's more likely that you're holding references to it though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜