Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:19:28+00:00 2026-05-29T04:19:28+00:00

I’m a bit confused about the right way to write a code. I read

  • 0

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.

  1. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:19:29+00:00Added an answer on May 29, 2026 at 4:19 am

    1: 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?

    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.

    2: 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 ?

    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 ObjectDisposedException if 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.

    3) I read that static Objects are a problem for memory leaks.

    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 null or 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.