|
Hi chris
Thanks for the quick response
Why i should not use GC.Collect? Becuase after using GC.Collect function before calling popup display, it has reduced the objects in memory from 23459922 to 13902334 its amazing.. it didn't harm any other part of code..still everything is working fine. Even i have used finally part to null all the objects..and almost in all my code i have done Object=null. I guess i have used lot of singleton object that might be occupying the memory..lot! so what has to be taken care while using GC.Collect
|
|
|
|
|
engsrini wrote: Why i should not use GC.Collect?
Because this is not C++. The GC is written in a highly optimised way to decide for itself when to collect. The more important question is, are your objects marked for collection ? If you didn't call Dispose, then they need to become orphaned in memory, and noticed as such, in order to be eligible for collection.
engsrini wrote: it has reduced the objects in memory from 23459922 to 13902334 its amazing
Sure, it may force some items that had been marked for 2nd or 3rd generation cleanup to disappear, but this can *hurt* performance. Did your app run faster, or did some number you've fixated on just change ?
engsrini wrote: Even i have used finally part to null all the objects
If you didn't call Dispose, I'm not sure how much that would help. Either way, let the GC do it's job.
engsrini wrote: so what has to be taken care while using GC.Collect
Just don't do it, not unless you have a more compelling reason than 'that number just got smaller'
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Christian Graus wrote: Sure, it may force some items that had been marked for 2nd or 3rd generation cleanup to disappear, but this can *hurt* performance. Did your app run faster, or did some number you've fixated on just change ?
My application is running normal after using it (may be i didn't do thoruough testing and didn't do remote debugging stuffs), I am not using GC.Collect at all the places.. Whenever the user run the test steps with larger iterations, then i will try that command. One more reason for larger memory i am using lot of images for my applicaition since our application is Touch screen we used lot graphics. So as per your suggestion i will put the dispose method wherever necessary, is there any other way to avoid memory leak or any tool to notify me the memory leaks? also is there anyway to idenify the memory used is huge, i suppose to call GC.Collect.
|
|
|
|
|
engsrini wrote: My application is running normal after using it
You are missing the point. Was it running normally before ? Odds are, it's less efficient now, but you can't tell, as it works fine, both ways.
engsrini wrote: So as per your suggestion i will put the dispose method wherever necessary, is there any other way to avoid memory leak or any tool to notify me the memory leaks?
In C++, you got a leak when you didn't call delete. In C#, you get it if you don't call Dispose. Dispose negates any need to GC.Collect, and lets the GC do it's job. There is nothing else to learn, just call Dispose whenever it's there to call. Bitmaps and videos are particular places where this is crucial.
engsrini wrote: also is there anyway to idenify the memory used is huge, i suppose to call GC.Collect.
NEVER call GC.Collect, did I forget to mention that ?
As you were not aware of the Dispose method, I have no doubt that GC.Collect has acted as a band aid on a major problem in your code. Call it, and you will have no issues that GC.Collect could solve.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
So when you call dispose, you actually just mark that for GC, your not directly removing it?
He who laughs last is a bit on the slow side
|
|
|
|
|
No, you're telling it to run custom code which will immediately clean up any unmanaged resources.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Now I know how that works, thanks.
He who laughs last is a bit on the slow side
|
|
|
|
|
One of the reasons to not call GC.Collect() that CG hasn't mentioned directly is that when a garbage collection cycle runs, it runs on a different thread than your application. In order for the GC to determine which objects are available for collection, it must freeze your application's main thread. This means that the more time spent in garbage collection, the less time your application has to run.
Eventually, you will start to see performance problems as your application spends more time in a frozen state.
The best option is to ensure that you dispose of objects as early as possible by calling Dispose or by using the using syntax. As you mentioned, setting the object to null is a good way to inform the GC that the object is eligible for collection because it helps ensure that there are no references to that object remaining.
There are a lot of good references on the Dispose pattern and how the garbage collector works. Look at this article, and the references section:
http://www.codeproject.com/useritems/idisposable.asp[^]
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Scott Dorman wrote: As you mentioned, setting the object to null is a good way to inform the GC that the object is eligible for collection because it helps ensure that there are no references to that object remaining.
I heared that before but never found a documentation on that topic.
Do you have a link to the docu?
Thanks
All the best,
Martin
|
|
|
|
|
|
Thank you for your time!
All the best,
Martin
|
|
|
|
|
Hello,
Apart from the Disposing method (which off course is a "must") you have to make sure that all your delagates are disconnected from your objects.
For example your Usercontrol or Form is connected with a global class over an event, and you close the Form (which calls Dispose as well) or Dispose the Usercontrol manually, you have to disconnect the event in your Dispose method.
The only reason when I use GC.Collect() is when I'm doing memory profiling before a snapshot is done.
Using a memory profiler is what I would also recomend to you, if you whant to be sure that you have no memory leaks.
All the best,
Martin
|
|
|
|
|
All,
I've gotten myself stuck on this problem and I can't seem to find a solution.
I have an initial data bound radio button list(rbl) in a table that has an OnSelectedIndexChanged event that fires at the push of a button. Next I have the code that adds a child rbl and a button that would fire a subsequent OnSelectedIndexChanged for the child rbl.
My question is this - How do I set the OnClick event for the dynamically created button?
Yes I know that this could be solved with a tree view control, but I'm forced to write this code in 1.1 which doesn't have this control.
Here is the code -
<br />
public void rbLocation_OnChange(object sender, System.EventArgs e)<br />
{<br />
DBAccess OBJBO = new DBAccess();<br />
DataSet BaseNodeSet = new DataSet();<br />
<br />
Table tbl = new Table();<br />
tbl = tblLoc;<br />
TableRow tr = new TableRow();<br />
TableRow tr2 = new TableRow();<br />
<br />
int numRows = tbl.Rows.Count;<br />
int iteration = numRows + 1;<br />
tbl.Rows.AddAt(numRows,tr);<br />
tbl.Rows.AddAt(iteration,tr2);<br />
<br />
TableCell td = new TableCell();<br />
TableCell td2 = new TableCell();<br />
TableCell td3 = new TableCell();<br />
td.Width = 30;<br />
tr.Cells.Add(td);<br />
tr.Cells.Add(td2);<br />
<br />
RadioButtonList rbChild = new RadioButtonList();<br />
rbChild.Attributes["OnSelectedIndexChanged"] = "rbChild_OnChange()";<br />
rbChild.DataTextField = "Description";<br />
rbChild.DataValueField = "NodeID";<br />
rbChild.ID = "rbChild" + iteration;<br />
rbChild.DataSource = OBJBO.GetBaseNodes(Convert.ToInt32(rbLocation.SelectedValue),3,1,out BaseNodeSet);<br />
rbChild.DataBind();<br />
td2.Controls.Add(rbChild);<br />
<br />
rbLocation.SelectedValue = rbLocation.SelectedValue;<br />
rbLocation.Enabled = false;<br />
btnOK.Visible = false;<br />
<br />
tr2.Cells.Add(td3);<br />
Button bk = new Button();<br />
bk.ID = "btnBack";<br />
bk.Text = "Previous Group";<br />
bk.Attributes["OnClick"] = "btnBack_OnClick()";<br />
bk.Visible = true;<br />
td3.Controls.Add(bk);<br />
<br />
<br />
}
An American football fan -
Go Seahawks!
Lil Turtle
|
|
|
|
|
How would I go about setting up an event to happen when a time (input by the user in a specific format) is equal to the system time?
Any help would be greatly appreciated... thanks!
|
|
|
|
|
You can use DateTime.TryParse[^] to parse the inputted time into a DateTime object, then compare it with the system time (DateTime.Now ) using the standard == operator.
As for firing custom events, search the following terms: C# custom events.
|
|
|
|
|
First, parse the user input time and convert it to a DateTime object (see Judah's post). Second, set up a timer to generate timing events. You'll need to decide how often you need "tick" events from the timer, i.e. how often you want to check to see if the time specified by the user has expired. Third, when you compare the user's time with the current time, make sure you use the "less than or equal to" operator:
if(userTime <= DateTime.Now)
{
timer.Stop();
}
If you use the equality operator instead, it's possible that the tick event generated by the timer will skip over the point in time in which the user's time is equal to the current time.
Hope this helps.
[EDIT] Oops, I original said to use the "greater than or equal to operator." It needs to be the "less than or equal to" operator given the way I've set up the comparison.
|
|
|
|
|
Hi
I know how can i open Combobox dropdown list :
<br />
ComboBox1.DoDragDrop();<br />
But i want to close it programmatically. I want to use it in my custom UserControl like Visual Studio DateTimePicker control. Clicking on it's ComboBox does't cause to openning dropdown list and just show Date Panel.
Tanx.
|
|
|
|
|
DoDragDrop does not drop down the combo box, it starts a drag-and-drop operation. If you want the combo box list to drop down, use the ComboBox.DroppedDown[^] property. You can close it by setting that property to false , open it by setting the property to true .
|
|
|
|
|
Hi,
On some system the showDialogue shows up, on some it is minimized.
I am new to C#, can someone help in showing this dialogue normal.
<br />
public long ShowDialog()<br />
{<br />
long rc = 0;<br />
<br />
try<br />
{<br />
m_dlg = new ScheduleEditor();<br />
if ( m_dlg.Initialize(m_InitData) )<br />
{<br />
m_dlg.StartPosition = FormStartPosition.CenterScreen;<br />
<br />
System.Windows.Forms.DialogResult result = m_dlg.ShowDialog();<br />
switch(result)<br />
{<br />
case System.Windows.Forms.DialogResult.OK:<br />
rc = 0;<br />
break;<br />
case System.Windows.Forms.DialogResult.Cancel:<br />
rc = 1;<br />
break;<br />
}<br />
<br />
m_dlg.EndDialog();<br />
}<br />
m_dlg = null;<br />
}<br />
catch<br />
{<br />
m_dlg = null;<br />
}<br />
GC.Collect();<br />
<br />
return rc;<br />
}<br />
|
|
|
|
|
AnilUnni wrote: GC.Collect();
NEVER do this.
AnilUnni wrote: case System.Windows.Forms.DialogResult.OK:
put using System.Windows.Forms at the top, then just use DialogResult.OK, etc.
AnilUnni wrote: m_dlg.EndDialog();
What does this do ?
AnilUnni wrote: if ( m_dlg.Initialize(m_InitData) )
Why do you need to do this ?
AnilUnni wrote: _dlg.StartPosition = FormStartPosition.CenterScreen;
You can set this on the form itself in the designer.
AnilUnni wrote: m_dlg = new ScheduleEditor();
AnilUnni wrote: m_dlg = null;
In fact, you should have called m_dlg.Dispose(), but, more to the point, why do you have a member if you reset it every time ? Just declare it locally.
A form has a WindowState variable, if the form is minimised, this has been set to minimised somewhere.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
m_dlg.WindowState = FormWindowState.Normal;
I did this but still it shows the dialog state minimized.
Is there any other property i need to consider?
|
|
|
|
|
Ooops I am sorry., I got it.
private void ScheduleEditor_Load(object sender, System.EventArgs e)
{
this.TopMost = false;
// make it visible
this.Opacity = 1;
}
Someone was doing a false. Thanks for the suggestion.
|
|
|
|
|
Hello everyone,
I am using the following code to get the DVD information. I have realised that in the case that the system has both DVD Drive and CD drive, this code gets the info. of the disc on the both drives.
This means eventhough the DVD Drive is loaded by a DVD Disc, the code still trys to get the info on the CD Disc which is not what I wish to have.
I was wondering if there is anyway that I can only get the DVD drive info and not the CD drive.
Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
how can convert using DNS from computer name to IP address??
|
|
|
|
|
IPAddress[] ip = Dns.GetHostAddresses("MyHostName");
|
|
|
|