Results 1 to 8 of 8
- 05-16-2011, 09:38 AM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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
- 05-16-2011, 09:44 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,370
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-16-2011, 10:09 AM #3
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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
- 05-16-2011, 10:40 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,370
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:private static Test test= new Test(); // will never be GC'd
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-16-2011, 11:07 AM #5
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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?
- 05-16-2011, 11:42 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,370
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-16-2011, 12:00 PM #7
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
ok thanks so much Jos for your advise.
- 05-16-2011, 12:10 PM #8
Similar Threads
-
Tips to protect your software from piracy?
By ozzyman in forum Forum LobbyReplies: 27Last Post: 11-22-2012, 08:32 PM -
JVM Garbage Collection
By daromnet in forum Advanced JavaReplies: 4Last Post: 03-09-2011, 10:18 AM -
Need protect you MacBook?
By bababoo in forum Reviews / AdvertisingReplies: 0Last Post: 01-12-2011, 04:36 PM -
how to protect java codes from being seen?
By anthrax in forum EclipseReplies: 5Last Post: 10-14-2009, 03:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks