Results 1 to 8 of 8
  1. #1
    msv1022 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default How can I protect a Variable to be Garbage Collected

    Hi guys,

    Been searching all over the net on how to protect a variable from garbage collected. I need some variables (tables) to be present and not be garbage collected since the application is running 24/7. I have tested that sometimes, the variable or tables that i have created was FREED (GCed).

    Thanks in advance.

    Regards,
    msv1022

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,290
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by msv1022 View Post
    Been searching all over the net on how to protect a variable from garbage collected. I need some variables (tables) to be present and not be garbage collected since the application is running 24/7. I have tested that sometimes, the variable or tables that i have created was FREED (GCed).
    As long as there is a reference to the object in scope, the object won't be garbage collected. There is a transitive relation here: if 'a' refers to 'b' and (some part of) 'b' refers to an object and 'a' is in scope, the object won't be garbage collected.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    msv1022 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    HI JosAH,

    Thanks for your quick reply. But if the application is running for sometime let's say 7 days and there's no activity there will be a time that a vector of table or an object will be GCed. That table is needed and should not be collected at anytime. Is there such as as thing as:

    class Test {
    string b;
    int c;
    };

    PROTECT_FROM_GC Test test = new Test();
    //new object test should not be GCed even not used for 7 or 30 days

    Thanks in advance

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,290
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by msv1022 View Post
    HI JosAH,

    Thanks for your quick reply. But if the application is running for sometime let's say 7 days and there's no activity there will be a time that a vector of table or an object will be GCed. That table is needed and should not be collected at anytime. Is there such as as thing as:

    class Test {
    string b;
    int c;
    };

    PROTECT_FROM_GC Test test = new Test();
    //new object test should not be GCed even not used for 7 or 30 days

    Thanks in advance
    As I wrote: as long as you refer to your object Test somewhere it won't be garbage collected. A blunt method is to keep a static reference to your Test object if it is just a singleton object:

    Java Code:
    private static Test test= new Test(); // will never be GC'd
    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    msv1022 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks again. Just to put a little detailed example:

    class IncNodeList inl {
    String node;
    int n;
    }

    protected Vector <IncNodeList> v;
    public static NodeList nodes= new NodeList();
    IncNodeList inl = new IncNodeList();
    inl.node = "TestNode";
    inl.n = 10;
    v.addElement(inl);
    IncNodeList inl2 = new IncNodeList();
    inl2.node = "Node";
    inl2.n = 20;
    v.addElement(inl2);
    ---
    ---
    ---
    in this example, is vector will not be GCed, and its members inl and inl2? Do i need to make the Vector static not protected?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,290
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by msv1022 View Post
    Thanks again. Just to put a little detailed example:

    class IncNodeList inl {
    String node;
    int n;
    }

    protected Vector <IncNodeList> v;
    public static NodeList nodes= new NodeList();
    IncNodeList inl = new IncNodeList();
    inl.node = "TestNode";
    inl.n = 10;
    v.addElement(inl);
    IncNodeList inl2 = new IncNodeList();
    inl2.node = "Node";
    inl2.n = 20;
    v.addElement(inl2);
    ---
    ---
    ---
    in this example, is vector will not be GCed, and its members inl and inl2? Do i need to make the Vector static not protected?
    For the third time: keep a reference to that object in scope and the object will not be garbage collected; your example is too convoluted; note that the reference doesn't need to be static but then again, the reference is an element of another object and you have to keep a reference to that other object in scope to prevent garbage collection. Static references are class members and classes are never unloaded/collected. Nor does the reference need to be protected or private or whatever; it's up to you.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    msv1022 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    ok thanks so much Jos for your advise.

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    6

    Default

    EDIT: Ignore, please. Accursed habit of opening up tabs and not looking at them for a long while.

Similar Threads

  1. Tips to protect your software from piracy?
    By ozzyman in forum Forum Lobby
    Replies: 27
    Last Post: 11-22-2012, 09:32 PM
  2. JVM Garbage Collection
    By daromnet in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2011, 11:18 AM
  3. Need protect you MacBook?
    By bababoo in forum Reviews / Advertising
    Replies: 0
    Last Post: 01-12-2011, 05:36 PM
  4. how to protect java codes from being seen?
    By anthrax in forum Eclipse
    Replies: 5
    Last Post: 10-14-2009, 03:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •