• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

GC

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?
1) The automatic garbage collection of the JVM prevents programs from ever running out of memory
2) A program can suggest that garbage collection be performed but not force it
3) Garbage collection is platform independent
4) An object becomes eligible for garbage collection when all references denoting it are set to null.
Ans: 2 and 4
4.. regarding eligiblity... what if this object is referred by another object and that is not gc'ed and still alive?
Q2:
Given a reference called
t
to to a class which extends Thread, which of the following will cause it to give up cycles to allow another thread to execute.
1) t.yield();
2) yield()
3) yield(100) //Or some other suitable amount in milliseconds
4) yield(t);
Ans:2
I can agree 1 and 2
(static can be accessed by instance reference too)
Ragu

[This message has been edited by Ragu Sivaraman (edited November 20, 2001).]
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ragu I think for the first part since as u say it is eligible for collection only when all its references are set to null so if another object is refrencing the one which is supposed to be garbage collected then the garbage collector wont collect it
for the second part
i agree with you i came across such a question in a mock but the answer in it also said the same
consider the following code
<code>
public class MyYield implements Runnable {
Thread t,r;
public MyYield() {
t=new Thread(this);
t.start();
r=new Thread(this);
r.start();
}
public void run()
{
try {
for(int i=0;i<1000;i++)
{
t.sleep(1000);
System.out.println("run:"+i);
t.yield();
}
}catch(InterruptedException e){}
}
public static void main(String args[])
{
MyYield my=new MyYield();
}
}
</code>
now this code though crooked runs fine but if I just say sleep(1000)
compiler complaints that it cannot resolve symbol method sleep(int)
further if I just say yield() above it repeats the same as above
cannot resolve symbol yield
what do u say of that?
------------------
coffee drinker
[This message has been edited by amit mawkin (edited November 20, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Amit Maukin,
Remember you are implementing Runnable and not extending Thread class. Because sleep() and yield() are methods of Thread class and not of Runnable.
Since they are static methods, the best way to call sleep() and yield() is
Thread.sleep(1000) - which makes the current thread to sleep
and Thread.yield() - which makes the current thread to yeild.
And one more thing. Since they are static methods you can call them using method name as sleep(1000) and yield() in static methods only.

------------------
Where there is a Will, there is a Way.
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu,
For Q1, I don't understand why 1 & 3 are not correct.
1) The automatic garbage collection of the JVM
prevents programs from ever running out of memory
3) Garbage collection is platform independent
In 1) isn't that the main reason for GC; since the user
is basically doing the malloc and delegating
the freeing up of memory. Has you seen a case
whereby the JVM ran out of memory ?
In 3) I need some clarification on this - is "platform independent" meant to mean "independence" from "different JVMs" or "different OS e.g. linux/windows/mac ?
I have question of my own taken from Mughal:


Which is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?



Can somebody help and write down their thought process as they go about solving this question ?
Pho
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for that folks I do agree with chandrashekhar but on second thoughts chandra if u look at my code could u confidently say that wheather its the sleep() method thats causing the second thread to run or the yield() method thats causing it to run
I think in either case if one of the methods is present it will result in the thread object referenced by variable r to run
correct me if i am wrong
------------------
coffee drinker
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pho Tek:
Ragu,
For Q1, I don't understand why 1 & 3 are not correct.
1) The automatic garbage collection of the JVM
prevents programs from ever running out of memory
3) Garbage collection is platform independent
In 1) isn't that the main reason for GC; since the user
is basically doing the [b]malloc
and delegating
the freeing up of memory. Has you seen a case
whereby the JVM ran out of memory ?
In 3) I need some clarification on this - is "platform independent" meant to mean "independence" from "different JVMs" or "different OS e.g. linux/windows/mac ?
I have question of my own taken from Mughal:

Can somebody help and write down their thought process as they go about solving this question ?
Pho[/B]


Pho,
concerning your question about garbage collection: a JVM can definitely run out of memory. Just imagine some algorithm that builds a binary tree and (recursively) replaces all the leafs with instances of the current tree (excuse me if this is somewhat sloppy formulated).
About your new question:

At the start of method f(), initially two string literals are declared ("hello" and "bye" at //0). The compiler puts these into a literal pool. I searched through books and documentation I have, but found no clear rules about when objects in the literal pool are gc-ed. However, the literal pool belongs to a class and therefore I assume that it is eligable to be gc-ed when the JVM ends (normally this is when the main() method is finished). Therefore I would go for answer 5, but I'm not 100% sure. Does anyone has more detailed knowledge about this topic?
Jonatan
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is line #1. The String "!" is available for GC after c is constructed.

See for reference http://www.javaranch.com/ubb/Forum33/HTML/001640.html .
p/s what does the author of the question write "assuming no compiler optimizations are done?" ? What "compiler optimizations" is being referred to here ?
Thanks
Pho

[This message has been edited by Pho Tek (edited November 21, 2001).]
[This message has been edited by Pho Tek (edited November 21, 2001).]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic