At which line , Object is Garbage collected.

1. public class GC {
2.  private Object o;
3.   private void doSomethingElse(Object obj) { o = obj; }
4.   public void doSomething() {
5.  Object o = new Object();
6.  doSomethingElse(o);
7.   o = new Object();
8.   doSomethingElse(null);
9  o = null; 10. }
} When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection?
A. Line 5
B. Line 6
C. Line 7
D. Line 8
E. Line 9
F. Line 10
I think it should be line 9.but answer is 8....
How is it line 8?
Edited by: user12203354 on Nov 4, 2012 9:38 PM

You've already ask a similar question in your other thread where I ask you to answer some questions.
Question on Garbage collection
You may want to work on one question like this at a time until you understand the difference between an object and a variable that references an object.

Similar Messages

  • RMI server object getting garbage collected

    Hi all,
    I have seen a number of posts regarding the ConnectException and found that this can occur in a number of situations.
    I am having a problem here.
    I am having an RMI server that is always up and running. And the server object gets requests from the client at regular intervals. But, when the server object is not receiving any requests for a long time (ex: 1 day), then I think the remote object itself is getting garbage collected. And so, tough I am able to get the remote reference using the lookup method, I am getting "Connection refused to host: 192.168.0.216; nested exception is:
         java.net.ConnectException: Connection refused" when I call a method using this reference.
    I believe that this is because the server object getting garbage collected as there are no requests for the server since long. Please correct me if my assumption is wrong.
    I want to know, after how much time the server object gets garbage collected if no requests are received. But, my requirement is that the server object should always be available and WHENEVER a client request comes then that should be processed. What should I do to accomplish this task.
    If any one have any suggestions, please reply as soon as possible.
    Thanks in advance,
    srik4u

    You might do some research into using an activatable remote object. You run rmid (the rmi activation deamon) and register a subclass of java.rmi.activation.Activatable (instead of the usual UnicastRemoteObject) with it. With an activatable object, the remote reference to the activatable object doesn't need to have a live object behind it. If an activatable object is not running (ie: it hasn't been constructed yet, or it has been garbage collected ...as in your case) a remote reference to the object can still be exported to a client. The first time the client calls a method on the remote object, the activation service on the server sees the object is not active, and activates the object for the client. If the object is running ...it is rmi as usual. But if it gets gc'd again, the next invocation on the remote object will trigger the activation service again.
    (The above explanation paraphrases author David Flanagan from Java Enterprise in a Nutshell, O'Reilly)
    I have only built one of these, which loosely followed an example from the above mentioned book. It's a whole other ballgame over and above a regular rmi object. But like anything else, if you dig in and get your head wrapped around it, it eventually makes sense. Ok, why lie ...it confused the hell out of me and left me a little queasy. But you know the drill, by the time you build a few of them it will probably seem as easy as mapping the human genome, right? At any rate, it seems like what you might be after ...so have a look at it. Good luck, and wear your lifejacket.

  • Object [5610] : garbage collection is off. Anyone tell me how to turn it on ?

    Can anyone tell me how to turn garbage collection on ? I'm getting objct: [5610] garbage collection is off.

    Press Cmd-F5 together (that's Cmd + the F5 key, two keys only,  not F-5.)

  • Manage behaviour of an object at garbage collection??

    Hello.
    I would like to specify that a counter is eventually decremented when an iterator is no longer in use. Is there some way to define the behaviour of an object when JRE realises that no references to it exist and proceeds to kill it?
    I don't know very much about what Java does with data that is no longer used, so perhaps the notion is a preposterous one. Please advise.
    Grape

    I am also writing the collection thoroughly because I'll keep it in a general library
    of data structures and patterns that I am building up
    over time for reuse in the future.
    Sounds cool but wouldn't it be easier to use one of the many container APIs that already exist and have been tested and are known to be reliable? That way you could concentrate on modeling the automata and not worry about measly collections. It doesn't sound very useful even as a learning experience since you must already be familiar with most kinds of containers. If you later find that you need a feature that's not in the standard collections, you can easily extend it or use an additional package like Collections from Jakarta Commons. http://jakarta.apache.org/commons/collections/
    Should it be the iterator or the collection that calls the exception?You are the designer, it's your call.
    Is there some kind of
    ModificationListener that Iterator could implement,
    so that when the collection is modified it sends out
    something like a ModificationEvent that all Iterators
    would get...? And the Iterators could then verify
    that an exception should be thrown by checking
    numModsAtStart != event.getSource().numMods()
    But that would imply that the collection is aware of its iterators and also that iterators have to be both registered and de-registered with the collection. This approach has the same problems as the counting one you suggested before.
    This use of the Observer pattern might be useful for other purposes.

  • Which algirithm is being used by java runtime for garbage collection??

    On what basis does java reclaim objects using Garbage Collection ??
    and explain the method i.e the step by step basis on how it is being done??

    There are various whitepapers and the like on this.
    As far as which objects may be collected, it's just any object that isn't referred to - directly or indirectly - by one of the root set of threads in the JVM.
    Collecting algorithms vary from jvm to jvm. You'd do better to search the web for a whitepaper on the subject.
    ~Cheers

  • Preventing garbage-collection of a RMIRegistered server object

    I am developping a client/server RMI application, and keep facing occasional ObjectNotFioundException: no such object in table, when the client tries to invoke method on the server stub retrived from the RMIRegistry.
    The Javadoc and online documentation say this means the server object has been GC'ed since it has been registered in the RMIRegistry.
    Following the advice found at: http://www.nabble.com/java.rmi.NoSuchObjectException:-no-such-object-in-table-t260095.html,
    I register the server in a RMIRegistry I obtain through LocateRegistry.getRegistry(), and not createRegistry().
    But I still face these exceptions.
    Assuming the issue is really due to the server object being garbage-collected, I tried to keep a static reference to the server but it didn't help (the Main class was probably garbage-collected itself).
    As a last hope, I've set up a "keep-alive" thread in the server VM, that keeps a direct reference to the server object (not the stub), and regularly invokes something on it. With this mechanism I never face the exception.
    This solution looks obviously clumsy.
    Is there a neater way to prevent garbage collection of the server object?
    Note that I use dynamic stubs (Java5-style).

    Without seeing some code, I'm not sure what you are doing. Also the link you supplied is broken.
    You always need to keep a live reference to your implementation class. How you do this is your business. Using a separate thread is over-kill.
    I set a reference to the implementation class in the start up class and use a never ending wait() to make sure the start up class thread (with the main()) lives forever.

  • Resources garbage collection, can be done this way ?

    The problem
    I would like to apply the same concept of garbage collection to system resources like Files, Database resulsets, Sockets etc...
    The reasons
    I am tired of looking around desperately into a program to identify memory leak that are instead resources leak. And this is getting worse while program size grows.
    How it could be done
    -- Let's have a resource manager class from where I get the wanted resource (I will write this class, fairly easy).
    -- Let's say that this manager class is a thread that runs in a scheduled way and this is decided by me (this is also something anybody can write).
    -- Let's say that when it runs it checks for all resources (objects) that it has given to the rest of the program if the reference count greater than one. If it is > 1 then the resource is still in use, else it can be deallocated (that just means calling close and then leaving the object to garbage collect).
    What I need
    A way to know in a reliable way how many objects are having a reference to a given object. Is there such a way ?
    Evangelism on..
    Please, if anybody at Sun is reading this, this IS a TOP issue. If you look around you will see more and more issues of leaking, they are resources leakage because it is NOT easy to ALWAYS close a resource.
    Someone may say "use the finally" of yes, try to think to use the finally to deallocate memory and you will immediatly understand the mess that will come.
    Garbage collection is a good idea let's have a way to apply it to resources.
    Thanks

    I have no problem, as it is, in making applications
    reliable.As usual the problems come in deciding how to do
    things.Yes.
    >
    Which is why mine don't. Since resources are usedin
    so few places it is easy to manage their usage.We are not talking on who has the biggest gun...Eh?
    >
    The sad thing is that the problem most of
    the time is NOT yours but of somebody else thatforgot
    to clean up after himself...Sounds more like a management problem than a code
    problem. If you are allowing programmers in your
    organization to write code which leaks resources oris
    so poorly designed that other users can notproperly
    dispose of resources then it is a managementproblem.
    Ok, let's drop it here.
    I hope you will never have to deal with code other
    that yours or written by your coworkers.
    I always deal with other people's code. And they with mine. Although I occassionally work on one person projects - perhaps 2 weeks every three years. the rest of my career has always involved multi-person projects and often (if not always) involve third party libraries.
    So I believe I have dealt with others code.
    in the applications that I work on.)There is java.lang.ref in a way it seems to dowhat
    I
    want....
    I don't believe so. Finalize() more closely fitswhat
    you might want to do. java.lang.ref is used in
    building caches of disposable entities. And it is
    most definitely only useable with memory.I have a funny sensation that whatever I write you say
    no...Not necessarily.
    Thanks for your help, if anybody wants to add
    something useful to
    what I am tryng to do they are welcome.I am trying to be useful.
    I am trying to point out that you are looking for a new feature in the language that just isn't very useful to most people. I was trying to point out why it wasn't useful to most people.
    Perhaps this might help.
    Look at it this way, go look at the bug database and look at the top vote getters for things that people want fixed. Then you might trying looking through the RFEs for something like you want. If it isn't there then add it. If enough people actually want it then it will be voted for. With enough votes it would have a better chance of being added.
    Of course that presumes it could be done in the first place. Based on my memory of past topics in the forums on reference counting I suspect that the answer is no. But you might want to read those.

  • Array garbage collection

    I need to keep track of what objects got garbage collected in a program.
    So I made sure that the classes of interest have a 'finalize()' method that simply print out the toString() and reference of the garbage collected object (using System.identityHashCode(this)).
    This worked fine for all objects except arrays.
    I went as far as substituting my own version of java.lang.Object that has an appropriate finalize(), but still no finalize() method seem to be getting called when an array object is 'supposed' to be garbage collected (the process consumes most of the system memory...)
    Any thoughts?
    Thanks,
    Wes

    Just an idea. You can try using WeakReference. Rather than using finalize, you can use the clear method of Reference.
    Here is some pseudo code
    .. class GCWatcher{
    List wrl = ...
    public void watch(Object obj){
    wrl.add(new WatchableWeakReference(obj));
    .. class WatchableWeakReference{
    WathableWeakReference(Object obj){
    super(obj);
    public void clear(){
    wrl.remove(this);
    Object obj = get();
    print out your message
    super.clear();
    If you try it let me know if it worked.

  • Surviving garbage collection

    I have a number of forms that use functionality encapsulated in script variables. These script variables have state in them, that must survive between calls. However, SpiderMonkey's GC disagrees, causing issues when objects that are supposed to be initialized disappears. I know there are some workarounds for this, but I worry that none of them are documented by Adobe. Many of these forms are used by third parties and must continue to work from year to year and version to version of Reader. Does anyone have suggestions?

    personal opinion really. I do call garbage collection destruction, the memory occupied by the objects in question is reclaimed. It is a discussion point however as it is from the perspective of the JVM; from the perspective of the application the object is "destroyed" as soon as you cut all references to it.
    What to explain about finalize()? It is invoked right before an object is garbage collected, possibly allowing you to do some last minute clean up jobs, like letting go of resource handles and such. The general rule about it is that in 99.99% of the cases you need not touch it. If you have a design where you need it, it is best to assume the design is wrong and you need to change it. For example in the case of closing resources, it is better to have a close() method that needs to be invoked manually.
    If you do use it, you better be really sure that you know what you are doing as you may cause performance problems or even hard to trace unexpected behavior.

  • Garbage Collection is not destruction?

    Garbage Collection is not destruction?
    This is one of the point from Thinking In Java 4th edition Chapter 5.Can someone please explain me this point.Does that mean that Garbage Collection does not destroy objects.If yes,then what is it role?
    And also please explain something about finalize().I tried to read it but couldn't understand.
    Thanks for your help.

    personal opinion really. I do call garbage collection destruction, the memory occupied by the objects in question is reclaimed. It is a discussion point however as it is from the perspective of the JVM; from the perspective of the application the object is "destroyed" as soon as you cut all references to it.
    What to explain about finalize()? It is invoked right before an object is garbage collected, possibly allowing you to do some last minute clean up jobs, like letting go of resource handles and such. The general rule about it is that in 99.99% of the cases you need not touch it. If you have a design where you need it, it is best to assume the design is wrong and you need to change it. For example in the case of closing resources, it is better to have a close() method that needs to be invoked manually.
    If you do use it, you better be really sure that you know what you are doing as you may cause performance problems or even hard to trace unexpected behavior.

  • Full Garbage Collection

    Hi Friends,
    I'm using weblogic workflow for my project. Last night i got one error, once i gone throw the bea ...i come to know that the error will comes because of "once the application calls webservice and the webservice intern calls the EJB stateless or stateful will fail". But my application is not using any sort of webservices. So i try to find the problem finally i found tht the problem is because of Garbage collection of Heap Size. It is taking 3.8508577 sec's. I feel in this time the JVM thread gets heighest priority and it is killing the application thread which is to be executed as usal.
    Can u guide me how to catch this exception so tht my application won;t get affected. The actual error says like this which is related to Garbage Collection...[Full GC 313152K -> 105060K (1004928K), 3.8508577 secs]. I'm using JDK 1.4.
    Thaks & Regards
    [email protected]

    Replies in this thread.

  • Objects & garbage collection

    Hi. There is a question in scjp preparation and I wish to know more about the explained answer:
    Given:
    3. class Dozens {
    4. int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
    5. }
    6. public class Eggs {
    7. public static void main(String[] args) {
    8. Dozens [] da = new Dozens[3];
    9. da[0] = new Dozens();
    10. Dozens d = new Dozens();
    11. da[1] = d;
    12. d = null;
    13. da[1] = null;
    14. // do stuff
    15. }
    16. }
    Which two are true about the objects created within main(), and eligible for garbage collection
    when line 14 is reached?
    A. Three objects were created
    B. Four objects were created
    C. Five objects were created
    D. Zero objects are eligible for GC
    E. One object is eligible for GC
    F. Two objects are eligible for GC
    G. Three objects are eligible for GC
    Answer:
    ® ✓ C and F are correct. da refers to an object of type "Dozens array," and each Dozens object
    that is created comes with its own "int array" object. When line 14 is reached, only the
    second Dozens object (and its "int array" object) are not reachable.
    Can anyone tell me why its mentioned that there are 5 objects are reported created here.
    Also doesn't defining the array of objects create the objects? E.g., in the line number 8, I see three element array is created, doenst this mean 3 objects are instantiated?
    Thanks

    >
    Also doesn't defining the array of objects create the objects? E.g., in the line number 8, I see three element array is created, doenst this mean 3 objects are instantiated?
    >
    What is your reasoning for thinking that creating a PLACE to hold three instances has ANYTHING to do with creating those instances?
    If you move three bar stools to the kitchen counter in your home does that automatically create a person sitting at each bar stool?
    If you create an egg carton that can hold a dozen eggs does that automatically create 12 eggs? If it did why do we need chickens?

  • Find out references to objects (garbage collection)

    Is there an easy way to find out, which references prevent the garbage collector from collecting my old objects, which I expect should be collected? Something like System.out.println(...)

    if you just want an object to be garbage collected, just
    make sure you de-reference is completely.CMueller obviously doesn't know which references prevent garbage collection so naturally he cannot set those references to null, now can he?
    o = null; // sets the value to null and drops all the referencesThat is simply not true. Removing a reference to an object doesn't affect any other references to that object.
      Object o1,o2;
      //create an object and make o1 reference it
      o1=new Object();
      //make o2 reference the same object as o1 references
      o2=o1;
      //now there are two references to the object
      //make o1 reference null
      o1=null;
      //at this point there is still one reference (o2) to
      //the object and therefore the object is NOT garbage
      //which means that it can NOT be collected by the GC- Marcus Sundman

  • Huge number of garbage collected objects

    We're running a system here with the java heap set to 256mb and have noticed
    that now and then, garbage collection takes a horribly long time to complete
    (in the order of minutes, rather than fractions of a minute!). Something
    like 3 million objects are being freed when the server is heavily loaded.
    Has anyone else experienced this behaviour? Has anyone tested weblogic with
    JProfiler/OptimizeIt and found any troublesome spots where many objects are
    created? One potential place where this can be happening is in the servlet
    logging. Since there is a timestamp that is a formatted date, my guess is
    that a new Date object is being created, which is very expensive and hence
    might cause many more objects that need to be garbage collected. Can any
    weblogic engineers confirm/deny this?

    Use vmstat to determine if you're swapping. sar would work too.
    Swapping is definitely dictated by the OS, but an inordinate amount of
    swapping activity just means you get to tune the hardware rather along
    with the application.
    Jason
    Original Message <<<<<<<<<<<<<<<<<<On 2/21/00, 12:45:26 PM, "Hani Suleiman"
    <[email protected]> wrote regarding Re: Huge number of
    garbage collected objects:
    Here are the results from running top on that machine:
    Memory: 512M real, 14M free, 553M swap in use, 2908M swap free
    PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
    3035 root 50 59 0 504M 334M sleep 308:42 5.13% java
    How to make sure I'm not swapping? I thought that kind of thing was dictated
    by the OS...
    Rob Woollen <[email protected]> wrote in message
    news:[email protected]..
    If GC takes on the order of minutes to run then I suspect that you
    are
    paging. How much physical memory do you have on the machine? Make sure
    that
    you are not swapping.
    -- Rob
    Hani Suleiman wrote:
    We're running a system here with the java heap set to 256mb and have
    noticed
    that now and then, garbage collection takes a horribly long time tocomplete
    (in the order of minutes, rather than fractions of a minute!).
    Something
    like 3 million objects are being freed when the server is heavilyloaded.
    Has anyone else experienced this behaviour? Has anyone tested weblogicwith
    JProfiler/OptimizeIt and found any troublesome spots where many
    objects
    are
    created? One potential place where this can be happening is in theservlet
    logging. Since there is a timestamp that is a formatted date, my guessis
    that a new Date object is being created, which is very expensive andhence
    might cause many more objects that need to be garbage collected. Can
    any
    weblogic engineers confirm/deny this?

  • Nested object reference after garbage collection

    I have a question about how GC works.
    Assume I have a Class A which has a reference to class B inside it. I create a object of class B and assign to member variable of class A.
    Now when obja is no longer needed, I set it null. And when GC runs, the object will be removed from memory. But should I also need to explicitly set objb to null. If i don't set it, will it also get garbage collected when obja is being removed from memory?
    public class ClassA {
    private ClassB objB = new ClassB();
    private static void main(String args[]) {
    ClassA obja = new ClassA();
    obja = null;
    }

    801625 wrote:
    But should I also need to explicitly set objb to null.No.
    If i don't set it, will it also get garbage collected when obja is being removed from memory?If there are no other references outside of classA to the object referenced by objB (in your case an instance of ClassB) then it will be garbage collected.
    Note - the only time one needs to set a reference to null is if one wants the object it references to be eligible for GC before the reference goes out of scope. There is a slight complication to this - depending on the JVM implementation, if a reference is defined in a block internal to a method then even though it goes out of scope when execution goes out of the block anything it references may not be eligible for GC until the method exits.

Maybe you are looking for