• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collection

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from K Sierra's book:






Below are the options:

A.true will never be output
B.true will be output atleast once
C.if true and false are both output,their respective values for x can be the same.
D.if true and false are both output,their respective values for x can never be the same.
E.This code will not compile
F.A given value for x can never be output more than twice


As per the book correct answer is only C.

Please could someone explain how is C true and how come both A and B be both false?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well true can be in the output. But it is not sure that it will be in the output or not. So A and Bare both false. A is false as true can occur one or more times and B is false as true might not occur in the output...
 
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you please specify in which chapter this example is given ?
So that i can refer

 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

I understood that A and B can both be false but I did not get how is C correct (under what circumstances).Please explain.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kushtawar lets see how C can be true. First of all I think you are clear with the program in general. You have a loop which will create objects, explicitly call finalize method on them and then the object will become eligible for GC. Now the first time the loop is executed, then x is 0 and gc is false. So when we explicitly call finalize on it, then the output would be "false 0". Then the loop will keep executing 100000 times. Now suppose an object created in the loop is garbage collected by the JVM. The finalize method is executed and it has entered the second if condition in the finalize method. The loop has also finished so x will again be set to 0 and gc will be set to true. And now the println statement of the object being garbage collected is executed so the output would be "true 0". So as you can see we have true and false as output with the same value of x i.e. 0...
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:The loop has also finished so x will again be set to 0 and gc will be set to true.


But inside the first if block, x will be incremented. So, in the second if block, x will not be 0.
 
Ranch Hand
Posts: 78
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so the output would be "true 0"



I don't get that output, everytimes it's different answer. Is it possible to get such question on the exam? (I mean with random answers).
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajshekhar Paul wrote:But inside the first if block, x will be incremented. So, in the second if block, x will not be 0.



No that will not happen. The if condition will be false as gc will be false till now.

Kamil Wojcik wrote:I don't get that output, everytimes it's different answer. Is it possible to get such question on the exam? (I mean with random answers).



Well it is an extreme case that you can get that ouput. usually you might not get that output. But for the exam you need to consider every possible output so this question is very good indicator of the questions on the real exam...
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:No that will not happen. The if condition will be false as gc will be false till now.



Suppose, the for loop has finished executing. Then following things will be executed in order.
1. x will be reinitialized with 0
2. gc will be reinitialized with true.
3. explicit GC call.

After the 3rd step, if JVM wants to free memory, it will run finalize() methods for the objects created. So, when for the first time JVM invokes finalize() for a Finalizer object, inside the method, first if block will be executed as gc is having the value true(as per step 2), which in turn will increment x(which is 0 as per step 1) to 1. So, the next if block will print true 1.

Correct me if I am totally in wrong direction.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see it so:


A.true will never be output


False: if the garbage collector does it's job after line 14 (and more than 10000 remained there), true will be output.



B.true will be output at least once


False: if the garbage collector does it's job before line 14 and collects ALL created objects (so that nothing remains to collect later), true will not be output at all.


C.if true and false are both output,their respective values for x can be the same.


True:
Garbage collector starts immediately after line 10 when x is for example 10000 => will output some "false 10000" (even more than once as x is static and all instances will see it with the value 10000)
Then the code runs further, and the NEXT garbage collection happens after line 14.
For each collected object, x is increased by 1. At the 9999-th collected object (will be reached as first time only the first 10000 objects were collected) x is 9999 => x++ is 10000. So "true 10000" will be output.


D.if true and false are both output,their respective values for x can never be the same.


False: see above


E.This code will not compile


False: ... clear


F.A given value for x can never be output more than twice


False:
For example garbage collector starts after line 10 when x is, lets say, 10000 and collects all objects previously created => will print 10000 more than twice (again: x is static and all collected instances will see it with the value 10000)
(more than that: 10000 can/will also be printed later, if a garbage collection starts after line 14 and .... )


 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Cristian
Why are you with the impression that the garbage collector will do it's job during the for loop execution?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajshekhar, Cristian gave an example of how the output can be same for true and false. What you are saying is right. But the garbage collection may also happen without an explicit call to System.gc(). In fact usually you don't need to call System.gc on to free memory. I was talking about the case when an object is garbage collected in the time the loop ends to the time when gc is set to true. This can happen before x is set to 0 or after that. But the example Cristian gave is simpler so you can look at that...
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ankit
Yes, I know that when JVM goes short of memory, it'll try garbage collecting the objects. But I didn't know that you were talking about the GC process during the loop execution which might be another chance!
 
Cristian Senchiu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I lost track of this thread for awhile ...

Rajshekhar Paul wrote:@Cristian
Why are you with the impression that the garbage collector will do it's job during the for loop execution?


@Paul
Hi Paul. It's not an impression, it's a possibility. That answer from the book was the right one, because, pure theoretically, it can happen (in real life being less probable, although not impossible).

Ankit Garg wrote:... In fact usually you don't need to call System.gc on to free memory. ..


@Ankit
Hi Ankit. I'd say that not only the System.gc() calls are usually not needed, but forcing major garbage collections (as System.gc() can trigger) should in general be avoided. Of course System.gc() call gives no guarantee that the JVM will decide to really garbage-collect.
 
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic