Click here to Skip to main content
15,031,223 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
using System;

class Calci

    public int Add(int a, int b)

        return (a + b);


    public int Sub(int a, int b)

        return (a - b);


    public int Multi(int a, int b)

        return (a * b);


    public int Divide(int a, int b)

        return (a / b);



class GCExample3

    public static void Main(string[] args)

        Calci oCalci = new Calci();

        Console.WriteLine("Calci object is now on " + GC.GetGeneration(oCalci) + " Generation");
        Console.WriteLine("call to GC.Collect(0):");
        Console.WriteLine("Garbage Collection Occured in 0th Generation:" + GC.CollectionCount(0));
        Console.WriteLine(oCalci.Add(9, 0));


output :
Calci object is now on 0 Generation
 call to GC.Collect(0):
Garbage Collection Occured in 0th Generation:1

My question is that:
If my calci object is garbage collected in 0th generation then how after that it calls to methods of calci class?

fdiu 28-Oct-12 1:31am
I read that but not gate answer .So please explain?
Kuthuparakkal 28-Oct-12 1:36am
GC.Collect call will usually not do much to reduce overall memory usage. And it will impose a slowdown whenever it is called.
The GC.Collect is best used for diagnostics purposes, such as for determining a baseline of memory usage in your program. You can also use it to see if all the objects you no longer need are not reachable and can be collected by the garbage collector.

In deployment, the GC.Collect method is not useful because it often has little benefit and might even reduce performance, while increasing complexity and causing you headaches.
Also read:
Kuthuparakkal 28-Oct-12 10:42am
Reason for downvote please
Because your oCalci object is not null, the garbage collector cann't collect the oCalci object. And I think the collected object is an object in the background, that's not still collected, and I think the collected object isn't your oCalci object because if I don't declare the oCalci object, and if I do a garbace collection, the GC collected 1 object also.
fdiu 28-Oct-12 5:59am
That means GC collects object from other process not from our running application process.
So our oCalci remains there in heap.
Im I correctly means your answer??
Thomas Daniels 28-Oct-12 9:18am
The GC doesn't collect objects from other processes, but I think the GC collects object in the background of your process. Objects that you have not declared, but that are declared automatically in the background.
fdiu 28-Oct-12 10:08am
can u please explain me any example of class that is allocated on heap that i not declared when process start
Thomas Daniels 28-Oct-12 10:11am
I can't give an example. I know that there're background objects, but I can't give an example. But I'm sure that Google can help you.
fdiu 28-Oct-12 11:03am
Thanks bro ..
read this article:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900