classloader caching (in Matlab)

D

David Soukal

Hello,

I have a problem that I'm trying (unsuccessfully) to solve. I'm using
Java in Matlab to compute some things for which native Matlab code is
cumbersome and slow.

The problem is that Matlab (or better its JVM) caches classes which has
been loaded in the current session. This behavior is absolutely
reasonable and welcome but not in situations when the class changes as
it happens during debuging and development of the class. It is very
annoying to have to restart Matlab everytime I recompile the class...

Is there any generic solution??

The Matlab uses JVM's internal classloader, but it does not allow you to
flush its cache...

Well, I was thinking that it would flush the cache once the last
reference to the class in question was released. BUT IS NOT THE CASE (at
least in Matlab)! So, there are no references to the class and yet it is
still in the cache... I tried to use Matlab's commands like "rehash",
"clear classes" but it's no use.

THANK YOU FOR YOUR HELP!!

David
 
C

Chris Uppal

David said:
Well, I was thinking that it would flush the cache once the last
reference to the class in question was released. BUT IS NOT THE CASE (at
least in Matlab)! So, there are no references to the class and yet it is
still in the cache... I tried to use Matlab's commands like "rehash",
"clear classes" but it's no use.

Classes are not eligible for GC until the classloader that loaded them is also
eligible. I think you need to use your own ClassLoader (a
java.net.URLClassloader will probably do the trick) to load your classes. When
you want to use new versions of classes, drop your old ClassLoader, create a
new one, and use that. With a bit of care about paths (you have to make sure
that your changeable .class files are not on the CLASSPATH or they'll be loaded
by the system classloader) you should be able to load new versions of classes
without restarting.

Of course, any instances of the old classes that are hanging around will
*still* be instances of the old classes.

-- chris
 
D

David Soukal

Chris said:
David Soukal wrote:




Classes are not eligible for GC until the classloader that loaded them is also
eligible. I think you need to use your own ClassLoader (a
java.net.URLClassloader will probably do the trick) to load your classes. When
you want to use new versions of classes, drop your old ClassLoader, create a
new one, and use that. With a bit of care about paths (you have to make sure
that your changeable .class files are not on the CLASSPATH or they'll be loaded
by the system classloader) you should be able to load new versions of classes
without restarting.

Of course, any instances of the old classes that are hanging around will
*still* be instances of the old classes.

-- chris

Hello Chris,

thank you. I'll definitelly try it. The only problem that I see now is
that the ClassLoader used in Matlab is the default one - the one built
in the JVM. So, I don't know whether you can really drop it since this
is the "parent" guaranteed to alway exist.

This Matlab thing is very annoying. They claim that command "clear
classes" will remove all instances of classes along with their binary
representation in the memory (what one would perhaps call "the cache")
and that command "rehash pathreset" would check timestamp of all classes
(and other primitives) that are accessible in Matlab, and load the new
ones. But this simply does not happen...

Thank you!!

David
 
C

Chris Uppal

David said:
thank you. I'll definitelly try it. The only problem that I see now is
that the ClassLoader used in Matlab is the default one - the one built
in the JVM. So, I don't know whether you can really drop it since this
is the "parent" guaranteed to alway exist.

What you have to do is create a new instance of your preferred classloader, and
then use its findClass() method to load classes dynamically. You then use those
class objects' newInstance() method to create new instances. You don't
"install" the class loader instance into the Java runtime (there is no such
operation), you have to keep it around yourself, and remember to use for
everything. If you are doing it right then your code will never refer to any
of your dynamic classes by its name, but only by String constants that are
passed to findClass().

Once you've got your instances, then you can either "talk" to them using the
reflection APIs (which get *very* tedious *very* quickly!). Or the sensible
thing to do (*if* you were programming in Java) is to define interfaces and/or
abstract super-classes that *don't* get loaded dynamically, and cast your
dynamic objects (the ones returned by Class.newInstance()) to the relevant
static interfaces. Then you can handle them without using reflection.

However, I took a *very* quick look at Matlab's Java embedding. I was very
interested to see how they'd approached it, since I deal with the same set of
problems embedding Java in a different language. It *looks* as if, once you've
created your new object, the Matlab system will automatically "know"
(dynamically) what methods it has, so it may be that you'll be able to use your
dynamic objects without messing around with either reflection or interfaces.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,124
Latest member
JuniorPell
Top