Home > Net >  Release for help: CSharp program memory resources
Release for help: CSharp program memory resources

Time:09-15

Languages: c #
Platform: Windows
Operating environment: the.net Framework
IDE: Visual Studio 2019 Community

A racing game. Use the ini file as software configuration files, the player often need to change some Settings in the process of playing a game, including change/set the frame rate, most of the Settings can be made within the game interface, setting about the frames, the game interface is not given, but this is one of the most important game parameters, performance and experience of the pursuit of game players, including professional players, anchor, civilian players need often change the parameters,

In general, players change frames can open Windows file resource manager, and then find the configuration file directory of the game, to find the configuration file config. Ini, use notepad to open, then the configuration items such as "Framelimit=30" into "Framelimit=38", finally click on save,

Myself also is one of the game players, because feel need to click the this process, steps to a bit of trouble, just want to write a small tool, written application can run perfectly, but open the task manager, change the Text in the ComboBox, program memory will rise, although the memory is not big, there are only a few M, every time memory and only a few kB, but I still want to climb the memory of obsessive-compulsive disorder because to understand,

The entire program of hundreds of lines of code, to save space, this paper only posted below related segments, if you need other pieces and posted in the comments section:

 private void ComboBox1_TextChanged (object sender, EventArgs e) 
{
String ConfigFilePath=GetConfigFilePath ();//to get players need to modify the configuration file path of the frames
If (the File. The Exists (ConfigFilePath))
{
Try
{
PictureBox3. Visible=true;//display configuration file icon, can use notepad change after a user clicks on frames

//will be the value of the number of frames in the configuration file into the TextBox for the user to change within the
TextBoxesGetValues (ConfigFilePath);

//read the configuration file to hide the parameters of the pet
String LoverPetShowFlag=ReadValueFromIniFile (Section, strLoverPetShowFlag, "null", ConfigFilePath);

If (LoverPetShowFlag=="0")
{
CheckBox1. CheckState=CheckState. Checked;//hide the pet
}
The else
{
CheckBox1. CheckState=CheckState. Unchecked;//show pet
}
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);
}
}
The else
{
PictureBox3. Visible=false;
EmptyValueTextBoxes ();
}
}

Private void EmptyValueTextBoxes ()
{
TextBox1. Text="";
TextBox2. Text="";
TextBox3. Text="";
}

Private void TextBoxesGetValues (string iniFilePath)
{
TextBox1. Text=ReadValueFromIniFile (Section, strFramelimit, "null", iniFilePath);
TextBox2. Text=ReadValueFromIniFile (Section, strOutdoorFramelimit, "null", iniFilePath);
TextBox3. Text=ReadValueFromIniFile (Section, strDanceFramelimit, "null", iniFilePath);
}

Private string GetConfigFilePath ()
{
StringBuilder sb=new StringBuilder();
Sb. Append (QQSpeedDirectoryPath);
Sb. Append (@ "");
Sb. Append (comboBox1. Text);
Sb. Append (@ "\ configuration \ config ini");
Return sb. ToString ();
}


ComboBox is stored in the account, when there are multiple accounts, can use the up and down arrows on the keyboard to switch according to different accounts, is here, when change the Text ComboBox, program memory will rise, about ComboBox. Text every change to 5 times, memory is 1 m,

Is the code that lack the resources to release related instructions, or the existing code of writing is not standard, also please bosses! I was a small white, programming in c # code specification, operation mechanism, memory management is not very understanding of what, in the above code GetConfigFilePath () function or deliberately make, because the beginning is to use "+" text connector retrieve configuration file path, then the Internet said that this might lead to a String in memory replication, then use StringBuilder methods, didn't think that there is a memory problem of rising

In the predecessors to you for help, to show just a little, thanks!

CodePudding user response:

How ReadValueFromIniFile implementation, it use unmanaged memory or implement IDispose objects, to release,

CodePudding user response:

reference 1/f, guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
how ReadValueFromIniFile implementation, which use unmanaged memory or implement IDispose object, to release,


Procedures used in the c # code, namely the WIN32 API function GetPrivateProfileString and WritePrivateProfileString:

 # region ini file to read write function 
[DllImport (" kernel32, "CharSet=CharSet. Unicode)]
Private static extern long WritePrivateProfileString (string section, the string key, string val, string filePath);
[DllImport (" kernel32, "CharSet=CharSet. Unicode)]
Private static extern int GetPrivateProfileString (string section, the string key, string def, StringBuilder retVal, int the size, the string filePath);
Public static string ReadValueFromIniFile (string Section, the string Key, string def, string filePath)
{
The StringBuilder temp=new StringBuilder (1024);
Int I=GetPrivateProfileString (Section, Key, def, temp, 1024, filePath);
Return temp. ToString ();
}
Public static void WriteValueToIniFile (string Section, the string Key, string Value, string filePath)
{
WritePrivateProfileString (Section, the Key and the Value, the filePath);
}
# endregion


As for how to release here, I tried this:
 public static string ReadValueFromIniFile (string Section, the string Key, string def, string filePath) 
{
The StringBuilder temp=new StringBuilder (1024);
Int I=GetPrivateProfileString (Section, Key, def, temp, 1024, filePath);
//temp. The Dispose ();
Return temp. ToString ();
}


But this function can not be returned to the string, a start this program, because do not know anything about the C/C + +, to learn what Windows message mechanism, and the oneself also does not have the ability to write secure code, see online say write C # code does not need to manage the memory themselves behind the choice of C #, I didn't think when do we found many key areas have to call the C/C + + API...

CodePudding user response:

Because the program interface is very small, cancel the show after the maximize button title bar appears not good-looking, have chosen to use without borders form, make the title bar, the code is as follows:
//FormBorderStyle=None 
To draw on//in the Client Area of text and minimize, title bar close button do
//window control to prevent flashing, using Graphic. DrawString, replace the Label control
Private void Form1_Paint (object sender, PaintEventArgs e)
{
Um participant raphics. SmoothingMode=System. Drawing. Drawing2D. SmoothingMode. AntiAlias;
SolidBrush drawBrush=new SolidBrush (Color Black);

Um participant raphics. DrawString (TitleString, new Font (" Microsoft YaHei ", 9), drawBrush, 2, 5);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related