6 Replies - 728 Views - Last Post: 14 September 2018 - 08:14 AM Rate Topic: -----

#1 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

garbage collection

Posted 13 September 2018 - 09:26 PM

hi,

is this garbage collected?

int a = 5;
int b = 6;
int c = 7;
a = b;
b = c;
c = a;
a = c;



a equals b
b equals c
c equals the new a
a equals the new c witch is the new a

did a = c got collected, removed so to speak because 'a' already was declared?

which value got garbage collected?

This post has been edited by daygamer: 13 September 2018 - 09:30 PM

Is This A Good Question/Topic? 0
  • +

Replies To: garbage collection

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2647
  • View blog
  • Posts: 4,228
  • Joined: 21-June 11

Re: garbage collection

Posted 14 September 2018 - 03:19 AM

All of your variables have type int, which is a value type. You can think of value type values as living inside the variable they're assigned to. They stop existing whenever that variable stops existing (which happens when the object, closure or stack frame that contains them stops existing - or never in case of static variables) or is assigned another value. They do not get their own heap-allocation and thus do not interact with the garbage collector (except indirectly if they're part of an object or closure that's handled by the garbage collector).

So in your code that means for example that the value 5 stops existing as soon as you set a's value to 6. This happens immediately and the garbage collector is not involved.
Was This Post Helpful? 2
  • +
  • -

#3 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: garbage collection

Posted 14 September 2018 - 05:13 AM

well, i call that for garbage collection asap the value of 5 stop existing.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6453
  • View blog
  • Posts: 22,090
  • Joined: 05-May 12

Re: garbage collection

Posted 14 September 2018 - 06:54 AM

There is no garbage collection involved. The value is overwritten. There is no separate "5" or "6" objects that exist independently. As explained above, the value of 5 is stored in a. Later when the value of 6 is assigned to a, the slot is just overwritten.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6453
  • View blog
  • Posts: 22,090
  • Joined: 05-May 12

Re: garbage collection

Posted 14 September 2018 - 07:19 AM

I hope this makes things clearer. Assuming that you have a class named Car that looks like:
class Car
{
    int year;

    public Car(int y)
    {
        year = y;
    }
}



And you had the following code:
int age = 25;
Car car = new Car(1995);


You can think of the memory as looking like this:
Attached Image

Later when you run this code:
// After
age = 32
car = new Car(2002);


Memory would look like this:
Attached Image

Notice how the box for age just got overwritten, while the arrow for car changed to point to the other Car object with a 2002 year.

The car with the 1995 year will eventually get garbage collected.

Something is dramatically wrong if your C# book or your teacher is not teaching you concepts like this.
Was This Post Helpful? 1
  • +
  • -

#6 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: garbage collection

Posted 14 September 2018 - 07:32 AM

This is not really that important because of the usage of this is 100% automatic, just even waste of time talking about it but however.

first thing first, i have so far understood it this way: Garbage collector should take over after end of a codeblock "{'}'" but not necessery, depends on how great computer you have, if you have overkill computer the garbage collector will even be confused.

So there is usage of finalizer which i didnt read anything about because too advanced for something that programmer do not even need to bother.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6453
  • View blog
  • Posts: 22,090
  • Joined: 05-May 12

Re: garbage collection

Posted 14 September 2018 - 08:14 AM

In my opinion, garbage collection is a topic worth knowing about, but not to the same level as our Java using brethren have to deal with. If you start writing games which need specific response times without stalling for garbage collection, or if you have an application that needs to handle high continuous loads, or if you simply have an application that seems to be hogging memory, then it's worth looking into how you designed your classes and ensure that you are helping the garbage collector as much as you can.

(For some reason, my co-workers who work on Java are perpetually trying to deal with/tune their memory settings and garbage collection settings because their app can't handle the load. In fact, they have 14 Java web front end's struggling to deal with a cumulative load of about 4000 requests per second. Their WFE's eventually pass on requests to my MVC app running on just 2 IIS WFE's. My app would handle all their requests without missing a beat, nor pushing the memory usage past a cumulative 8GB. Somehow their machines have more than 1TB of memory between them, and they are falling over and they've opened multiple tickets with Oracle to try to resolve the issues.)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1