CLR profile resolves high memory consumption

Source: Internet
Author: User

CLR profile resolves an issue with high memory consumption

Brother Cannon: "Hey, man, what busy, computer card into this force-like." "

Brother Yong: "Using the CLR Profile tool to analyze the memory footprint of the flexiprint. "

Brother Cannon: "Ouch, good ah, play advanced." ”

Brother Yong: "also did not Ah, is to find the Click Query button query data, if the amount of data a large, memory rose several m, so the first feeling is not quite normal. I've known the CLR profile before, but I've never used it for a long time, and this is just a study. ”

Brother Cannon: "Nice job, to learn from you, to proactively identify problems and research solutions." By the right, what have you found? ”

Brother Yong: The tool finds that the Dgmain_cellformatting method consumes too much memory. ”

Brother Cannon: "What does this method do with so much memory?" ”

Brother Yong: "Actually do things very simple, according to the different status of each line of orders, display different icons, so that customers can be very straightforward to understand the status of the order." ”

Brother Cannon: "Oh, yes, that's really nothing, let me look at the code." ”

Namespace IPP_PCL. homeviewusercontrol{public partial class Printorderinformationusercontrol:usercontrol,  Ihomeprintorderinformationview {#region Field private readonly printcellliteserviceclient _serviceclient        = new Printcellliteserviceclient ();        #endregion #region Event Handler//<summary>///This event is mainly used for Picture button display///</summary> <param name= "Sender" ></param>//<param name= "E" ></param> private void DG Main_cellformatting (object sender, Datagridviewcellformattingeventargs e) {if (Dgmain.columns[e.columni Ndex].                Name.equals ("Dgmain_printicon")) {int row = E.rowindex; Gets the cell that displays the Picture button in string status = Dgmain.rows[row]. cells["Dgmain_statuskey"].                Value.tostring (); Switch (status) {case ' Print in Progress ': string erpsonumber = This.dgmaIn. Rows[row]. cells["Dgmain_erpsonumber"].                        Value.tostring (); Find records for Erp_so_number from db ienumerable<fileindexmodel> fileindexes = _serviceclient.getfileinde                        X (Erpsonumber);                        If a pro's record exists in the FileIndex table if (fileindexes = = NULL | |!fileindexes.any ()) {break;}                        Fileindexmodel FileIndex = Fileindexes.first ();                        if (filestatus.printed! = Fileindex.printedstatus && filestatus.partial_printed! = fileindex.printedstatus)                        {e.value = Properties.Resources.picYellow;                         } else {e.value = Properties.Resources.picGreen;                    } break;                        Case "Printed": E.value = Properties.Resources.picGreen;                  Break  Case "Partial Printed": E.value = Properties.Resources.picGreen;                    Break                        Default:e.value = Properties.Resources.picYellow;                Break }}} #endregion}}

Brother Yong: "..."

Brother Cannon: "Very simple, write very clearly, this method is no problem, it will be wrong." ”

Brother Yong: "should not, through the CLR profile analysis found that in this method created a very many objects." ”

Brother Cannon: "You mean Bitmap object?" ”

Brother Yong: "Yes." ”

Brother Cannon: "Oh, I see, every time you access a Properties.Resources.picGreen similar property, a new object is created. But in fact, there are a few types of icons, can be saved in memory, the use of direct reference on the line. ”

Brother Yong: "That's right, let me change the code to see the situation." ”

Brother Cannon: "Let me see how you changed it?" ”

Brother Yong: "..."

 1 namespace Ipp_pcl. Homeviewusercontrol 2 {3 public partial class Printorderinformationusercontrol:usercontrol, Ihomeprintorderinformat Ionview 4 {5 #region Field 6 7 private readonly printcellliteserviceclient _serviceclient = new Prin Tcellliteserviceclient (); 8 9 Bitmap picgreenbitmap = properties.resources.picgreen;10 Bitmap Picyellowbitmap = Properties.resou          rces.picyellow;12 #endregion14 #region Ctor16 Printorderinformationusercontrol () 18 {InitializeComponent ();}21 #endregion23 #region Event Handler25 2 6///&LT;SUMMARY&GT;27////This event is mainly used for the display of the Picture button.//</summary>29//<param name= "se NDEr "></param>30//<param name=" E "></param>31 private void Dgmain_cellformatting (ob Ject sender, Datagridviewcellformattingeventargs e) (dgmain.columns[e.ColumnIndex]. Name.equals ("Dgmain_printicon")) {3 int row = E.ROWINDEX;36//Get the Display Picture button cell 7 String status = Dgmain.rows[row]. cells["Dgmain_statuskey"]. Value.tostring (); max switch (status), "Print in Progress": Prodt var = dgmain.datasource as datatable;44 if (Prodt = = NULL | | ProDt.Rows.Count = = 0) {break;} Erpsonumber string = This.dgmain.rows[row]. cells["Dgmain_erpsonumber"]. Value.tostring (); 48 49//Find records of Erp_so_number from DB Ienumerable<fileindex                         model> fileindexes = _serviceclient.getfileindex (Erpsonumber); 51 52//If there is a record of pro in the FileIndex table 53 if (fileindexes = = NULL | |!fileindexes.any ()) {break;} Fileindexmodel FileIndex = Fileindexes.first (); 56 57                        if (filestatus.printed! = Fileindex.printedstatus && filestatus.partial_printed! = Fileind Ex.                         Printedstatus) (E.value = picyellowbitmap;60                          }61 else62 {e.value = picgreenbitmap;64                         }65 break;67-Case "printed": 69 70 E.value = picgreenbitmap;71 break;72 case "Partial printed": 74 7 5 E.value = picgreenbitmap;76 break;77 default:79 8 0 e.value = picyellowbitmap;81 break;82}83}8 4}85 #endregion87 88}89}

Brother Cannon: "Then use the CLR profile to analyze the modified memory situation." ”

Brother Yong: "You see, the memory goes directly from 9.0M to 425kB." ”

Brother Cannon: Haha, it seems that the problem has been solved. ”

Brother Yong: Today I found out that C # also has the concept of closures. ”

Brother Cannon: "Closure?" What kind of thing? ”

Brother Yong: "I was learning JS when I found the concept of closures, but did not expect C # also have this." ”

Brother Cannon: "So when you open your horizons, you will find more beautiful." ”

Brother Yong: "No, it's time to get off work." Oh, yes, I saw a movie < A beautiful Life last night, and if you're interested, you can look at it. ”

CLR profile resolves high memory consumption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.