I’m a bit confused about the right way to write a code. I read that i don’t have to keep reference in variables cause the garbage collection doesnt clean it but i dont understood few things.
-
What about this code :
private List<String> foo; public List<String> bar () { foo = new List<String>(); foo.add(something); return foo; }
Each time that i call bar method the old foo list Lose the reference and it will be candidate for the garbage collection? should i declare (and initialize????) most of the variable at start of the class and use after or declare before and initialize inside the method?
2.
What about the disposed Object? If i write this code
private void browser_navigating(Object sender, NavigatingEventargs e)
{
Uri someurl = new URI(something);
List<String> somelist = new List<String>();
}
If i have 20 webBrowser Object that use navigating event , they create Uri and List when the navigating event is fired right? But what happen to object inside the event when i dispose the webBrowser? They will lose the reference? When i dispose something this works for all his child , but Does it work also for those object that doesnt implement iDisposable(like Uri and List) so will they cleaned up from Garbage or they will stay in the memory ?
2.1. This events are unregistered automatically after that the object which registered it is disposed?
3) I read that static Objects are a problem for memory leaks.
What about if i need use a class many times and should i create it every times that i need to declare it static (for example in my case i have a static connection class which inside has methods to login and comunicate with a webserver , i declare static (in my mainWinform Class) cause i store inside it the cookies recived from login)
I will thank you who could answer my questions.
In general, an object should go out of scope the moment you’re done with it. So if it’s only being used inside a method, it should be local to that method. The longer an object lives, the more likely it is to be promoted to a higher generation on the heap. Data in higher generations is more expensive to collect, and is collected much less frequently. Therefore, getting objects out of scope ASAP will generally reduce your memory consumption and also let your program run faster.
When you dispose of an object, it’s effectively dead to the world. Anybody else who has a reference to it will still have that reference, but the object will (or should, anyway) raise an
ObjectDisposedExceptionif they try to do anything with it.An object’s unmanaged resources (file handles, connections, etc.) are released immediately when it’s disposed. Its unmanaged resources are cleaned up normally by the garbage collector. So they won’t be cleaned up until the last reference to that object goes out of scope.
This is because a class’s static fields will never go out of scope. So any data they reference can’t be cleaned up unless you explicitly set its reference to
nullor replace it with something else.It’s much better to handle each session with a web server separately. Not only does that help the garbage collector to clean up after you, but it also enables you to do things like have more than one concurrent session.