• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.gc() has stirred a hornet of questions  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

all the while i was thinking that by doing MyClass1 mc1 = new MyClass1() i am creating an object, but nopes i was wrong, by using that statement i was just creating a reference variable to point to the object created by the default MyClass1 constructor.

so here goes the list of questions

1) will the default constructor create only one object for a class
2) How many references can an object have
3) please check the below program



here default constructor of the class MethodDemo1 creates an object which is being referenced by md reference variable. in the above case when will the object be destroyed i.e. garbage collected
4) Does for every execution of the program an object is created in the heap memory of the program

5) is it not that object becoming unreachable mean that there are no referencing variable to it...

6) How can a reference variable be created on a heap and if it is created what would be its status as it is the object only that is garbage collected.


Thanks
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:. . .
1) will the default constructor create only one object for a class
No. One object is created every time the new operator is used and then the constructor runs. So you can have many instances of a class with a default constructor.

2) How many references can an object have
As many as you have space to fit them in
. . .
5) is it not that object becoming unreachable mean that there are no referencing variable to it...
Usually yes, that is the definition of unreachability.
. . .
6) How can a reference variable be created on a heap and if it is created what would be its status as it is the object only that is garbage collected.


Thanks
Variables are not created on the heap; they are local variables on the stack or they are fields inside the code representing an object. That latter is usually on the heap.

Those questions look like something out of a certification book. If so, please tell us which book (and page number).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:
all the while i was thinking that by doing MyClass1 mc1 = new MyClass1() i am creating an object, but nopes i was wrong, by using that statement i was just creating a reference variable to point to the object created by the default MyClass1 constructor.

Not entirely wrong, but to be precise:

Variables in Java are not objects, they are references to objects. The 'new' operator creates a new object and calls a constructor to initialize the object. In your example above, the no-arguments constructor of class MyClass1 is called to initialize the new MyClass1 object. The 'new' operator then returns the reference to the new object, and the variable 'mc1' is set to this reference.

krishnadhar Mellacheruvu wrote:1) will the default constructor create only one object for a class

The constructor is not what creates the object. A constructor is called by the JVM to initialize a new object. And yes, a constructor never initializes more than one object at a time, and the 'new' operator always creates exactly one new object.

krishnadhar Mellacheruvu wrote:2) How many references can an object have

There's no practical limit to how many variables can refer to the same object.

krishnadhar Mellacheruvu wrote:here default constructor of the class MethodDemo1 creates an object which is being referenced by md reference variable. in the above case when will the object be destroyed i.e. garbage collected

The JVM keeps track of how many variables refer to any object. As soon as there are no variables in any live threads referring to an object, the object can be garbage collected. However, it might not be garbage collected immediately. When exactly garbage collection happens depends on implementation details of the JVM.

In your example, the variable 'md' goes out of scope at the end of the main() method, and then there are no variables left referring to the object, so at that point the object becomes eligible for garbage collection.

krishnadhar Mellacheruvu wrote:4) Does for every execution of the program an object is created in the heap memory of the program

Objects are always* created on the heap. Objects are only created when the program runs. So yes, every time you run the program, the object is created on the heap.

krishnadhar Mellacheruvu wrote:5) is it not that object becoming unreachable mean that there are no referencing variable to it...

As I explained above.

krishnadhar Mellacheruvu wrote:6) How can a reference variable be created on a heap and if it is created what would be its status as it is the object only that is garbage collected.

Variables are not created on the heap; objects are.

The important thing to understand is: A variable is not an object. It's just something that points to an object.


*: except when the JVM optimizes things with escape analysis, but this is not something you need to think about normally.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:. . .. . . in the above case when will the object be destroyed i.e. garbage collected
Probably never. The “GC” tool only runs when there is a risk of running short of heap space, and that is highly unlikely in the case of a small program like yours. you should not worry about when the GC tool runs, nor about how it does it, but simply remember that it does it automatically. The GC tool can do things automatically far better than any programmer can program and is much less error‑prone. Forget all about it. As long as you don't have any uncollected and unwanted references somewhere. An example of an unwanted reference would be on a stack, where you push a reference and forget to delete it after it has been popped.

4) Does for every execution of the program an object is created in the heap memory of the program
. . .
Simple answer: No. You create "Robert", "Hi ", " your age is:", "Hi Robert your age is:30", args(array) and one MethodDemo object. They are on the heap. And there are other objects behind the scenes, e.g. a StringBuilder, which you do not see. Again on the heap.

Correct answer: No.. You create "Robert", "Hi ", " your age is:", "Hi Robert your age is:30", args(array) and one MethodDemo object. Since Java6 there has been an optimisation whereby short-lived small local variables may point to objects created on the stack; these do not need garbage collection.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those questions look like something out of a certification book. If so, please tell us which book (and page number).

Hi

Campbell

i got this doubt when i was going through ocajp1 se 8 cert book by Jeane boyarsky and scott. it is the first chapter towards the end in a block which states objects vs references..

it said "a reference amy or may not be created on the heap.."
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


String[] names = new String[10000000];
Arrays.fill(names, "Campbell"); // 10000000 references inside the same array


that was a crackling/awesome.. example by the way to understand....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!