Re: Class hierarchy prolem
dgront wrote:
I have a class, say MyClass, that consist of a few fields in
particular of a list of other objects:
public MyClass {
int someID;
LinkedList<ManyOfThem> bigData;
}
public ManyOfThem {
// many fields inside
}
I read a file and I create MyClass object from it. My structure of
objects reflects the file format. For each file I calculate some
features:
public MyFeature {
double theFeatureValue;
MyClass source;
}
As you see, I keep the computed value and a reference to MyClass
because I need to remember MyClass.someID for some reasons.
Unfortunately Java also keeps in memory all ManyOfThem objects and
there are really many of them!
Unfortunately ManyOfThem objects are necessary to calculate MyFeature.
The problem: I read many files and for each I compute just a few
MyFeature features. Each MyFeature has a reference to MyClass. Because
MyClass keeps also a list of ManyOfThem references, 2GB RAM is not
enough to parse my all data. MyFeature data itself is very small, the
problem is how to get rid of ManyOfThem objects.
In C++ I would use destructor.... What to do in Java to forget about
ManyOfThem objects?
Make sure that there are no more references to an object and eventually the
garbage collection (GC) thread may clean it up - will clean it up if it needs
the heap.
Can I do the following: I split MyClass into two:
public SimplerClass {
int someID;
}
public MyClass extends SimplerClass {
LinkedList<ManyOfThem> bigData;
}
This will keep the reference to bigData alive whenever there's a reference to
the SimplerClass instance that happens to actually be a MyClass instance.
And now MyFeature keeps only a reference to the simpler class.
public MyFeature {
double theFeatureValue;
SimplerClass source;
}
I guess when I set "source" field to a reference to (SimplerClass)
MyClass. It want solve my problem, will it?
No - the references chain.
But what if I set:
someFeature.source = ((SimplerClass) myClassObject).clone()
I expect that clone() method from SimplerClass will create a new
object without ManyOfThem stuff. And myClass memory will be free at
some point. Is it correct?
Instance methods are polymorphic - if the object happens actually to be a
MyClass, then the MyClass clone() will operate and you will copy the reference
to the List.
Do you know any simpler/other solution to my problem?
Yes, release the MyClass instance and create a new one when you want to
"forget" the List inside the old one. Simply pointing the MyClass reference
to a different instance will do the trick, assuming it was the only reference
to the old object.
A typical example is a loop:
for ( Foo foo : foos )
{
MyClass inst = new MyClass();
inst.doSomething( foo );
}
Each time through the loop, the 'inst' variable points to a new 'MyClass' and
the previous instance is eligible for GC, assuming doSomething() doesn't
create another reference to the 'MyClass' object.
When the previous instance becomes eligible for GC, all the instances that it
referenced will be eligible for GC if there are no other references to them.
--
Lew