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 992075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:12:27+00:00 2026-05-16T06:12:27+00:00

Please see the code below. I expect it to print either 10 because I

  • 0

Please see the code below. I expect it to print either 10 because I have explicitly invoked the garbage collector. But I always get either a 0 or 20 as output. Why is that?

void Main()
{
    Panda[] forest_panda = new Panda[10];
    for(int i=0; i<forest_panda.GetLength(0);i++)
    {
        forest_panda[i]=new Panda("P1");
    }

    for(int i=0; i<forest_panda.GetLength(0);i++)
    {
        forest_panda[i]=new Panda("P1");
    }

    System.GC.Collect();

    Console.WriteLine("Total Pandas created is {0}",Panda.population);          
}

class Panda
{
    public static int population=0;
    public string name;

    public Panda(string name)
    {
        this.name = name;
        population = population + 1;
    }

    ~Panda()
    {
        population = population - 1;
    }   
}

Please note that the class for Main is automatically created by LINQPad (the editor that comes with the “C# 4.0 in a Nutshell” book). I am new to C#.

  • 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-16T06:12:28+00:00Added an answer on May 16, 2026 at 6:12 am

    You have not run an explict garbage collection. From the docs of GC.Collect():

    Use this method to attempt to reclaim
    all memory that is inaccessible.
    However, the Collect method does not
    guarantee that all inaccessible memory
    is reclaimed.

    All objects, regardless of how long
    they have been in memory, are
    considered for collection; however,
    objects that are referenced in managed
    code are not collected. Use this
    method to force the system to attempt
    to reclaim the maximum amount of
    available memory.

    The garabage collector is highly optimized and “decides” all by himself when he actually does the garbage collection and then call the finalizers. Additionally it is all done asynchronously. That is also why Finalizers are called non-deterministic cleanup. You never now when cleanup happens.

    You have two options now. You can either call GC.WaitForPendingFinalizers() wich will halt the current thread until the all finalizable objects have been finalized. Or call this new overload: System.GC.Collect(int generation, System.GCCollectionMode mode) with GCCollectionMode.Forced It was introduced in .NET 3.5.

    Just keep in mind that usually it is not necessary and more importantly: a bad idea to call the garbage collector manually. Also implementing the finalizer is only needed in rare occasions. Calling the garbage collector will slow down the runtime. Implementing finalizers will slow down the runtime additionally. The garabge collector puts all objects that implement the finalizer into the finalization queue when they are ready to be garabge collected. Processing this queue is expensive.
    To make things worse, when the finalizer is run, it is not guaranteed that the members you are trying to access there are still alive. It is very well possible they have already been grabage collected. That’s why you should use the finalizer only when you have unmanaged resources that need to be cleaned up.

    All this is definately not needed in your example. What you acutally want is IDisposable for deterministic cleanup.

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

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use sleep to control wait period, and use WWW::Mechanize for… May 16, 2026 at 6:10 pm
  • Editorial Team
    Editorial Team added an answer You could reset the error state of my_string: my_string.clear(); my_string.ignore(… May 16, 2026 at 6:10 pm
  • Editorial Team
    Editorial Team added an answer No you can't use something like 'initial', you should use… May 16, 2026 at 6:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have problem with session and query execution,please see code below. class A implements
I have problem with session and query execution, please see code below. class A
Please see code below. The destructors are never called. Anyone know why and how
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
Please see code below: s = new Socket(nextHopIP, 3434); fileIn = new FileInputStream(fileName); fileOut
Please see the code below: #include <windows.h> int main(int argc, char* argv[]) { HANDLE
So Im trying to make an object dynamically, please see below code from my
see code below.. <div style=overflow:auto;width:250px;height:75px;border:1px solid #336699;padding-left:5px> <label style={width:250px;}><input type=checkbox name=wow[] onclick='highlight_div(this);'> PHP</label><br> <label
I'm writing some JDBC code which calls a Oracle 11g PL/SQL procdedure which has
I have a tree structure and I want to find all nodes matching a

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.