garbage collector collects unused object during runtime, OK ? i know it but never fully understood it. Like what 'unused' objects ? what makes them unused ?
and does GC works when you EXECUTE your program by clicking .exe file ?
and does GC work when i run file after being compressed by inno or WIX, for demployment purpose ?

HELP !

Recommended Answers

All 8 Replies

The garbage collector runs when the system decides that it needs to do so, usualy when you have a lot of fragmented memory that it wants to clean up.

An object is unused if it goes out of scope and no variable in scope retains a reference to the object. For example:

public int MyMethod() {
    Object myObject = new Object();
}

In that short bit of code we create an Object and the variable holding it immediatly goes out of scope. That object is now elegible for garbage collection.

ok
and does GC works when you EXECUTE your program by clicking .exe file ?
and does GC work when i run file after being compressed by inno or WIX, for demployment purpose ?

GC runs (sometimes) during the execution of your program. I say sometimes as it's possible that it will never run if you don't have a lot of objects.

ok great ! so it mean that if i make a complete package like installation wizard of my app (via wix etc) then will GC work ? will GC work when i exectute my program as a normal software ?

If it's .NET, the garbage collector is running. Whether it actually does something is up for debate, but there basically won't be a time when you have to worry about manually managing memory for stuff that the garbage collector handles.

ok but lets suppose, i'm a programmer and i have declared and initialized some objects e.g.

    public int MyMethod() {
    Object myObject = new Object();
    }

so for the first time if GC has freed some memory from the above code then why GC would need to run again and again ? if it has been done for once

if it has been done for once

How does it know? What if you've created other objects? What if those objects create objects behind the scenes or trigger events that spawn threads that create objects? It's not quite as simple as you seem to think, and you may be misunderstanding how the garbage collector works.

There's plenty of learning material on the .NET garbage collector. Maybe if you point to parts that you don't understand rather than paraphrasing how you think reality should work, it would be easier to help clarify things for you.

ohh great ! i got it ! thanks man !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.