|
Great. So assume I am not writing the Finalizer method for my class. I just wrote only Dispose(). So will GC call the Dispose() method ? What will happen to the object that uses unmanaged references if it is not cleared on Dispose()/Finalize ?
|
|
|
|
|
No, the GC will never call Dispose, hence the need for the finalizer in the first place.
Unmanaged objects that are not released will typically be released by the operating system when the process terminates. A few resource types (for example memory shared between processes) will not be released before next reboot.
|
|
|
|
|
lmoelleb wrote: No, the GC will never call Dispose, hence the need for the finalizer in the first place.
Thanks. I need one more help. Will Finalize method available there for all classes by default ? If Finalize method is not there, What GC will call ?
Is it a good practice to write a Finalize method when class contains Dispose() ? But MSDN say's it's costly in performance.
|
|
|
|
|
1) No, classes do not have a default finalize and if you do not add one nothing will be called.
2) A Finalizer is not needed if you have no unmanaged code to clean up (as you can see in my example, nothing happens in the finalizer if there are no unmanaged references.
3) Yes, a finalizer can impact performance - as I wrote in a comment in my example a finalizer will force the object to be promoted a generation in the garbage collector before it is released (which does not matter if it is a single object you dispose when closing the application, but for something you use a lot it is a problem).
However you will see that the call to Dispose result in a call to GC.SuppressFinalize(this) which basically disable the finalizer for the object once it has been Disposed (as we have cleaned up the references no need to call it again). This elimintes the performance hit when Disposed is used correctly (as you should always be carefull to do with objects implementing IDisposable).
|
|
|
|
|
Thanks for helping. It was really worth information. Thanks again for sharing.
|
|
|
|
|
lmoelleb wrote: Yes, a finalizer can impact performance - as I wrote in a comment in my example a finalizer will force the object to be promoted a generation in the garbage collector before it is released (which does not matter if it is a single object you dispose when closing the application, but for something you use a lot it is a problem).
Adding a finalizer to a class does affect perfromance, but not because it causes the object to change generations. Any classes that implement a finalizer are automatically placed on a separate finalization queue when they are created, even if you do nothing with the object and immediately call Dispose . This queue is checked each time a GC cycle runs.
lmoelleb wrote: However you will see that the call to Dispose result in a call to GC.SuppressFinalize(this) which basically disable the finalizer for the object once it has been Disposed (as we have cleaned up the references no need to call it again). This elimintes the performance hit when Disposed is used correctly (as you should always be carefull to do with objects implementing IDisposable).
Yes, the Dispose method should always include a call to GC.SupressFinalize , but it still doesn't eliminate the performance hit if your class has a finalizer. The purpose here is to prevent finalization from happening a second time unneccesarily.
|
|
|
|
|
N a v a n e e t h wrote: Is it a good practice to write a Finalize method when class contains Dispose() ? But MSDN say's it's costly in performance.
You should generally avoid writing finalizers if at all possible. They are difficult to write properly and do add a performance cost to using your class.
You should take a look at this article[^] for more detailed information regarding Dispose, finalizers, and the Dispose pattern.
|
|
|
|
|
First, please use the <pre> tags for large code blocks like this so it keeps the formatting.
lmoelleb wrote: To avoid confusing statements I'll just include the standard dispose pattern here.
A few points where you have really deviated from the pattern:
Your finalizer should not call Dispose , really under any circumstances. You introduce a lot of extra complexity to your resource cleanup logic by doing this and introduce a much higher chance for failure. Most of the assumptions you can make about the state of the runtime are not valid when running during finalization and most of the classes and methods are not safe to be used during finalization.
The call to GC.SupressFinalize should be in the public Dispose() method, right after the call to Dispose(true) . There are probably two reasons I can give for this. One is that is simply the way the pattern is defined, which isn't an entirely helpfull answer. The other reason is that the Dispose(bool) method should be the one that encapsulates all of the cleanup logic and that this method knows wheter it is being called to do explicit cleanup or being called due to a garbage collection cycle. Because of that, the method really would need to call GC.SupressFinalize under certain conditions only. By moving the call to the public Dispose() method, those risks/concerns are removed. The ordering is still important as it ensures that GC.SupressFinalize only gets called if the Dispose operation completes successfully.
The Dispose(bool) should be protected, not private, to allow any derived classes the ability to override it.
Again, the call to GC.SupressFinalize does tell the GC that it doesn't need to finalize a second time, but this really has nothing to do with generation promotion.
|
|
|
|
|
With regards to calling Dispose(false) from the finalizer, I stole the idea from these guys:
MSDN[^]
Just out of curiosity - who should ever call Dispose(false) if not the finalizer?
Moving GC SuppressFinalize is correct - I was typing from memory and trying to make sure the code did the right thing more than making sure it looked correct. Which was probably a mistake, but still better than making sure it looks correct but doesn't do the right thing. The way my code is written SuppressFinalize could be called multiple time for derived classes (yes, Dispose(bool) should be virtual and protected). It should not break anything but obviously isn't optimal.
With regards to suppress finalize I do agree there is an extra performance hit even when calling SuppressFinalize, and I should have used the term "minimize" performance hit. With regards to promotion, here is one of the sources confirming this (they might be wrong, but I have no reason to doubt them): MSDN Magazine[^]. I have not found any references to the finalizer queue management being a significent performance problem compared to the longer living objects on MSDN, but maybe I just missed it.
|
|
|
|
|
lmoelleb wrote: With regards to calling Dispose(false) from the finalizer, I stole the idea from these guys: MSDN[^]
I see where this could be confusing. The example they are providing is for implementing a base class that also implements a finalizer. In this scenario, you should make the call to Dispose(false from the finalizer of the base class, but the derived classes should not have their own finalizer or public Dispose() method. The derived classes should only override the Dispose(bool disposing) method.
lmoelleb wrote: Just out of curiosity - who should ever call Dispose(false) if not the finalizer?
Off hand, I can't think of anything.
lmoelleb wrote: With regards to promotion, here is one of the sources confirming this (they might be wrong, but I have no reason to doubt them): MSDN Magazine[^].
I am very familiar with this article, and Jeffery Richter is one of the primary people responsible for the GC implementation in .NET. I did find the statement you are referring to where he clearly states:
Finalizable objects get promoted to older generations, which increases memory pressure and prevents the object's memory from being collected when the garbage collector determines the object is garbage. In addition, all objects referred to directly or indirectly by this object get promoted as well.
In that respect, I don't think the finalization queue management is much more significant than longer living objects, but the whole idea behind the GC is that, ideally, you want to have as few long lived objects as possible.
|
|
|
|
|
N a v a n e e t h wrote: But AFAIK, GC won't consider the unmanaged resources. That's what they provided Dispose() to call explicitly. What do you think ?
That is correct. The GC does not know how to release unmanaged resources, which is the reason for using the Dispose pattern. If your class properly implements the pattern, then the resources would get released when the GC runs. The public Dispose method is available to help provide a more deterministic way to release the unmanaged memory.
If you want more details, check out this article[^].
|
|
|
|
|
HI,
i am trying to work with windows form....
Pardon me for asking such a basic question....
1)i have a text box and a combo box and a button in windows form......
when i click on the button i want the item selected in my combobox to displayed in the text box......what is the code for this?? i tried the following code "txtBranch.Text = cmbBranch.SelectedIndex();"
2)similarly if i add a datagrid to this i want the data grid to display this item selected in the combobox......what is the code for this??
3)if anybody can provide any link that will help me to get familiarise in windows controls that will be good....
thanking you
j
|
|
|
|
|
kabutar wrote: i tried the following code "txtBranch.Text = cmbBranch.SelectedIndex();"
Well, that won't work. The reason is that the selected index is a number. Also, it's a property, so the brackets are wrong. You would have got error messages explaining this. Try txtBranch.Text = cmbBranch.Text
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )
|
|
|
|
|
Hi,
ThankYou it worked.......Thanks a lot......
i tried google to find links that will help me to learn windows forms myself...
but i cannot find anything......
if its not much trouble can you get me a link .....
thanks again
j
|
|
|
|
|
How do i Add new column to the table called as “Nature of Comment" in a existing page,in that there,it has a drop down box.The drops down fields are: Bug, Suggestion, Grammar, and Formatting.thks a lot.
-- modified at 1:55 Wednesday 24th October, 2007
alok2171
|
|
|
|
|
Sounds trivial to impliment. Which bit are you stuck on ?
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )
|
|
|
|
|
alok2171 wrote: Addition of a new column to the table called as “Nature of Comment",in that there,it has a drop down box.The drops down fields are: Bug, Suggestion, Grammar, and Formatting.thks a lot.
What is your question ?
|
|
|
|
|
What kind of table are you talking about? An html table? An asp:Table control? A DataTable object? A database table?
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
Hello experts...
I am developing a desktop application. In that I use some web services that will fetch the data from web. As we all know the data comes from web service in the XML format and So first I parse it and then save into the msaccess databse.
When I click on button1. The web service is called and it will connect to the server and download the data in XML format and then after parsing I save it into the DB.
As obious it will take some time. It may be depends upon the net speed aur amount of data etc. Now mean while the end user thinks the application is halted. So I want to create an progress bar that will shows the current status like
Opening connections
Connection to web site
parsing the XML
saving the data
closing connections
etc.
How can I do that...? I also try threading but unable to get proper result because I have to check what is going on the code (taking data from web,parsing or saving data into the db)
Please help me
Regards
Pankaj Joshi
|
|
|
|
|
You can do your webservice calls in a backgroundworker, and then it can call the main thread with progress info. Of course, you can't provide a progress bar during an single step, such as a web service call.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )
|
|
|
|
|
If the Application is havy background operation, use BackgroundWorker
You can also use
private void SetProgressBar(int value)
{
this.progressBar1.Value = value;
Application.DoEvents();
}
When one event is end, just change the value of progress and lable message status.
means,
lblmessg.text="connecting........"
SetPogressBar(10)
SQL_Connection()
lblmessg.text="Connecting to web sites"
SetPogressBar(30)
Connect_WebSites()
......
Similary............... You can do....
But Backgorund Worker will be better
|
|
|
|
|
hi friends
i wanna store a image in ms access database and the code is
Aurigma.GraphicsMill.Bitmap bitmap =
new Aurigma.GraphicsMill.Bitmap(@"c:\mountain.jpg");
//Resize bitmap
bitmap.Transforms.Resize(bitmap.Width / 2, 0);
System.Data.OleDb.OleDbConnection connection =
new System.Data.OleDb.OleDbConnection(connectionString);
System.Data.OleDb.OleDbCommand command =
new System.Data.OleDb.OleDbCommand("INSERT INTO [Image](Image_Data) " +
"VALUES (?)", connection);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap.Save(stream, new Aurigma.GraphicsMill.Codecs.JpegEncoderOptions(70, false));
byte[] imageData = stream.GetBuffer();
System.Data.OleDb.OleDbParameter parameter = new System.Data.OleDb.OleDbParameter(
"@Image_Data", System.Data.OleDb.OleDbType.LongVarBinary, imageData.Length);
parameter.Value = imageData;
command.Parameters.Add(parameter);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
I am dynamically setting the background image of the label by using a property grid so is there anyway to get the address of the picture please let me know that
regards
sindhu tiwari
its me sid
|
|
|
|
|
Hello Experts,
I developed a desktop application using C#.net 2005.
I also have a digital certificate and I want to attach it with my assembly I.e. abc.exe.
In .net IDE at signing tab
1. I check the Sign the ClickOnce manifests
and in that I also select the appropriate certificate.
2. I also enter the right time stamp URL
and
3. I also sign the assembly with storng name using a .pfx File
When I build the application it builds successfully.
But when I right click the abc.exe file and see the properties of it,
It does not cointains any certificate..
Where I creating the problem please help....
Regards
Pankaj Joshi
|
|
|
|
|
Hello Experts,
I'm developing an desktop application in VS.net 2005. I make installer of it using Setup and deployment project in .Net.
In that I add Application launch shortcut, HTML help file launch Shortcut and uninstall shortcut.
Now I want to add one more shortcut that will open my home page...
How can I do that...?
Regards
Pankaj Joshi
|
|
|
|
|
pankaj.indore wrote: Now I want to add one more shortcut that will open my home page...
Create a internet shortcut file which navigates to your home page and add this to start menu
|
|
|
|