• 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

How to make gc-able?

 
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)I have read that it is possible to use dead thread's methods. Does this mean that the threads are to be stuck in memory forever untill stopping JVM?
Do I miss something?
How to make them garbage collectable, disposable before the end of application?
2)Suppose I have a member objects. It is static. It is big (huge structures). I have used it for its purposes and would like to get rid of it. How to make them garbage collectable, disposable before the end of application?
2a)How the objects instantiated w/o an idenifier (by new A()) are made garbage collectable
2b)If I instantiate an object without identifier in a static method, is it implicitly static?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Threads don't have methods. Only classes have methods. Perhaps you could clarify what you are talking about. The class staying in memory has nothing to do with the Threads life.
Classes loaded under the bootstrap classloader (the normal one) stay in memory for as long as the JVM is alive. Classes are NEVER garbage collected.
2) If you have a member object, you keep track of it by holding a reference to it in a variable. Other variables may also hold a reference to the same object. If you want to get rid of the object, find all the variables that hold a reference to it, and either null them out or set them to something else.
2a) Same as 2 - no references in variables - food for the gc
2b) No. If you instantiate an object in a method it is local to the method. Since there is no reference to it, it is available as soon as the method is over.
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tanks, Cindy,
Please return to details because you make a big confusion in my head.
I understood the main point that I am stuck with something inefficient in static context (for ex., a big static structure) till the end of the days of JVM, i.e. application
But I do not agree in any concrete detail.


  1. Only classes have methods.


    I do not agree. Why objs do not have methods?

  2. It is senseless and against JLS2 to have static definitions locally, in instance method, and if it is in static context, well, where is the distinction between lifetime of static context, the loaded class, JVM, application?

  3. "Threads don't have methods."


    I do not agree that a thread does not have methods. I think it has all methods of an Object: toString(), equals() and I checked it by coding. I am not satisfied here also.


  4. I do not agree that:


    "If you instantiate an object in a method it is local to the method."


    The final objects/primitives instantiated locally are not local to a method and overlive methods.


  5. "If you want to get rid of the object, find all the variables that hold a reference to it, and either null them out or set them to something else."


    How I am supposed to null an objects missing any explicit references? I did not ask about any explicitly referenced objects!!!
    Should it be understood that if explicit reference is not attributed then those objects are never Garbage-collected? It is very important point for performance!
    I instantiate them as:
    new classA(); //without any explicit identifier linked!

  6. [This message has been edited by G Vanin (edited October 25, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Objects only store state. The class Methods are only stored once no matter how many objects are created - in a special area called the Method area.
2) I have no idea what your question was.
3) OK - you got me there. The object which is a thread does use the threads class methods.
4) An object will only outlive the Method if a reference to it is passed to a variable outside the Method.
5) (are you yelling at me?) If there is no explicit reference then of course you can not null it. The gc will just get rid of it with no action from you. (I have the funny feeling that you are asking some other question here).
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy,
please stay with this topic, I am still unclear.


  1. inner classes defined in a method have access only to final local vars, of the enclosing methods. Correct?
    Explanation , that I have seen, is because final local vars are bound at compile time and outlive the method where they are declared. Correct or Not?
    If it is correct, then did I undestand correctly that they CANNOT BE UNLOADED?
    If not, then why inner classes defined inside method may access only final local vars of embracing method?


  2. I yell to your repetitions that I do not agree that:


    If there is no explicit reference then of course you can not null it. The gc will just get rid of it with no action from you.


    In the following examples of code (just what jumped to my head rapidly):

    OR

    am I risking to loose those nameless objects SUDDENLY due to gc? I do not think so! It is an ubiquitous pattern. But when will gc get rid of them? That rule "when staying w/o references" are not very good for such cases. It is very important issue to understand for big distributed applications running for months in service of thousands of remote clients (or just for me)


    1. PS
      I also have funny feeling that some people sometimes answer to what I am not asking.
      It is REALLY very hard to understand what, why, who and when people do not get in my communication.

      I think I continue getting experience in expressing myself

      [This message has been edited by G Vanin (edited October 26, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G,
I think you are mixing the words "unloaded" and "garbage collected" and they are NOT the same.
1) Local Inner objects might perhaps live beyond the life of the method, therefore they can only access variables that also live beyond the life of the method. Therefore they are restricted to only using Constants (final variables). Correct.


did I undestand correctly that they CANNOT BE UNLOADED?


Classes loaded by the bootstrap classloader are NEVER unloaded whether they are local inner classes or top-level classes. Perhaps you meant to ask if objects of the class can not be garbage collected? Yes, they CAN be garbage collected. As soon as there is nothing that holds a reference to them they are food for the gc.
2. list.add(new Integer(i)); The list holds a reference to the Integer created, so the object is not available for gc. If ANYTHING holds a reference to the object it is not available for gc.
Sometimes it is not explicitely CLEAR what is holding the reference, such as when you add a button. In that case you are adding it to the list that the AWT Container keeps internally, so there is a reference to it and it is not available for the gc.
What is important is that you understand how references can be held. The rule stands. If there is nothing that holds a reference to the object, then it is food for the gc.
PS:Communication is a very difficult thing at best. If we don't understand your question the first time, please be patient with us and try to re-word it again. Thanks .

[This message has been edited by Cindy Glass (edited October 26, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic