March 18th, 2003, 09:47 PM
|
#10 (permalink)
|
| Banned
Join Date: Oct 2001
Posts: 447
| Quote:
Object's are eligible for garbage collection when there's
no more references to that object.
| That is correct and I believe the java definition of when resources are eligible for gc-ing. As stated gc is really left to JVM, but any resources that go out of scope (vs. =null are generally gc-ed efficiently. Quote: |
now.. I don't actually populate them in main... main makes a static method call to the method that populates them.
| Ooh, ouch! So you have one class file with all STATIC functionality? That makes scope tougher. Here's why:
With static (class) methods in java, there is no real reference to the class object itself, your basically running a C program (loose reference). When you run 'java classname', you run the main method, which is static. This effectively 'loads' all static qualified members of classname. I used 'loads' carefully, because it doesn't exactly load all static to memory (if this is an issue, I can explain), it 'loads'... Thus main is in SCOPE (and static members**) until main finished, then everything out of SCOPE. **= not exactly true, but good enough for now.
What does this mean? if you call a static method from main, even if method finished and returned to main, SCOPE of that method unknown for sure. For many reasons...
Here is how to quarantee that the Object you use for the 'file reading, array filling, object creating' mayhem goes out of SCOPE and gets gc-ed effectively from the main method.
You can actually use your existing class to do this, but for ease of use, create new java class, call it qball or whatever. This class will read file and create huge arrays. The tricky part, will be the object creation, but more later.
public class qball
{
public void someMethod()
{
int anArray[] = new int[345600];
int anArray2[] = new int[345600];
//read file
//fill arrays
}
}
==============
compile qball where your main method has access. Now in your main method do this:
...
qball myqball=new qball();
myqball.someMethod();
myqball = null;
//now those arrays, way outsa SCOPE!
...
But I need to instantiate some objects based upon each array val! Yes you do. But they need to be in SCOPE of your main method (I would think), not class qball. Yes they do, and here is a trick that should work.
Create a static method (createObj) on your class (the one that run main) that creates the objects. Then call that from the qball class...
public class qball
{
public void someMethod()
{
int anArray[] = new int[345600];
int anArray2[] = new int[345600];
//read file
//fill arrays
//create objects
yourclass.createObj();
}
}
This when you call:
...
myqball.someMethod();
...
myqball object will create objects through your STATIC class method, for your static main method.
=======================
I'm guessing, none of this makes any sense? |
| |