0

The following is a c# code, in which I have to find out the number of objects(DOG) and reference after the whole code. Initially I calculated it to be 4 objects and 8 references. But after seeing this question Garbage collector test ,(which says that garbage collector runs only when the system has low memory and other similar causes) I doubt if the no of objects should be 6?. The below question says nothing about the memory obtained by the program.

So my question is if I ever get this type of question what should be my answer(4, 6 or should I say it would depend on the memory)?

 Dog rover = new Dog();
 rover.Breed = “Greyhound”;
 Dog rinTinTin = new Dog();
 Dog fido = new Dog();
 Dog quentin = fido;

 Dog spot = new Dog();
 spot.Breed = “Dachshund”;
 spot = rover;

 Dog lucky = new Dog();
 lucky.Breed = “Beagle”;
 Dog charlie = fido;
 fido = rover;
 rinTinTin = lucky;

 Dog laverne = new Dog();
 laverne.Breed = “pug”;
 charlie = laverne;
 lucky = rinTinTin;

// No of objects and reference at this point?(DOG objects not considering the string objects)

Hoping this question is not stupid.

Community
  • 1
  • 1
dividedbyzero
  • 187
  • 3
  • 16
  • 1
    I think you have to ignore garbage collection for this question, because it is indeterminate when it runs. Also note that strings are objects, and in your example code `Dog.Breed` is a reference to a string. – Matthew Watson Feb 11 '15 at 09:25
  • @MatthewWatson....I'm interested in dog objects only. But thanks for pointing out. – dividedbyzero Feb 11 '15 at 09:29

4 Answers4

2

For almost all purposes, the operation of the garbage collector is out of scope and you should not worry about it.

Counting the number of "Dog "objects created here is as simple as counting the number of times "new Dog()" appears; each of those creates a new object.

As Matthew notes, strings are objects too; I'm note sure if you're supposed to count those, as well, but that would make it a total of 10.

Community
  • 1
  • 1
Rik
  • 28,507
  • 14
  • 48
  • 67
  • 3
    I think you forgot a word: "should NOT worry" – Sebastian Negraszus Feb 11 '15 at 09:28
  • And also `rover.Breed = “Greyhound”;` creates an object (a string). But then it starts to get REALLY complicated because of string interning - because if the same constant string was created somewhere else, then a new string object may NOT be created! – Matthew Watson Feb 11 '15 at 09:28
  • @Rik....But after using "new...()",the reference variable is pointing to some other Dog object leaving behind the previous Dog object no reference to it. – dividedbyzero Feb 11 '15 at 09:31
  • 1
    @dividedbyzero yes, but the object still exists, at least until the garbage collector runs. And since you don't know when that happens, you should still count it. – Rik Feb 11 '15 at 09:34
  • @Rik....But in Head First Book it says that once an object has no reference to it, it is garbage collected.So this is not the case right? – dividedbyzero Feb 11 '15 at 09:54
  • @dividedbyzero No that's not correct. When an object has no references to it, it is *eligeble to be* garbage collected, but it won't actually be collected until the garbage collector runs. – Rik Feb 11 '15 at 10:00
  • @Rik...and when does the garbage collector run? – dividedbyzero Feb 11 '15 at 10:07
  • You don't know and you should not make assumptions about it. – Rik Feb 11 '15 at 10:25
1

It is 6. To clarify the answer change the Dog class like the following:

public class Dog
{
    static int counter = 0;

    public string Breed { get; set; }

    public Dog()
    {
        Interlocked.Increment(ref counter);
    }

    ~Dog()
    {
        Interlocked.Decrement(ref counter);
    }
}

the static property counter will count the instances of the class Dog in memory. There is more explanation here

Community
  • 1
  • 1
user3021830
  • 2,784
  • 2
  • 22
  • 43
1

Every call to new will create a new object (and therefor also a new reference to it). The GC is not deterministic, so you cannot be sure how many objects are still alive when you got out of scope of your variables. So none of your answers will be right, it completely depends on what the GC assumes to be good.

As long as you´re in the scope of your objects you can of course count them, as Rik already told. Having said this you have 6 objects (excluding 4 strings).

Community
  • 1
  • 1
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • It means that you´re in the same method/class/block/... where the variables have been defined. If you are in a method for instance and define these objects there than this method is the scope and your objects will as long be alive as you are in this method. Thus you cannot access them from outside this method. – MakePeaceGreatAgain Feb 11 '15 at 10:03
0

Each new key word means that you allocate on the heap a new object. So i count 6 objects of type Dog. And 4 of these dogs have a String object. So, 10.