Custom Class Loaders

R

Roedy Green

What are custom class loaders for?
I know of these uses:

1. dealing with hot switching class code.

2. dealing with code generated on the fly.

3. dealing with getting code from some unusual source.

Do you know of others?
 
A

Alex Molochnikov

Roedy Green said:
What are custom class loaders for?
I know of these uses:

1. dealing with hot switching class code.

2. dealing with code generated on the fly.

3. dealing with getting code from some unusual source.

Do you know of others?

With a custom class loader you can intercept the class bytecode and send it
to another JVM, that does not have this class in its classpath.

We use this technique in our client-server product, when a class located on
the server side must be made available to the client, that normally is not
delivered with the standard client distribution (e.g. a new feature). The
bytestream is pumped to the client over RMI, cached on the client side, and
then loaded through another custom classloader.

This may sound somewhat similar to (2), but in our case it is not
generating, but rather distributing the classes on the fly.

Alex Molochnikov
Gestalt Corporation
www.gestalt.com
 
L

Lee Fesperman

Roedy said:
What are custom class loaders for?
I know of these uses:

1. dealing with hot switching class code.

2. dealing with code generated on the fly.

3. dealing with getting code from some unusual source.

Do you know of others?

We use one for 3, and we also use it to enforce a sandbox mechanism that limits access
to the standard api for classes being loaded.
 
J

Jean-Baptiste Nizet

Custom classloading is very important to be able to separate different
modules or applications inside a single JVM. For example, an
application server uses custom classloaders to separate different web
applications, each using potentially the same libraries, but with
different properties, or of different versions.
Custom classloading is also used to intercept classes loaded, and
instrument them. This is used by performance analysis tools, for
example, to add traces and time mesurements at the end of each method
called.

JB.
 
T

Thomas Hawtin

Roedy said:
What are custom class loaders for?
I know of these uses:

1. dealing with hot switching class code.

2. dealing with code generated on the fly.

3. dealing with getting code from some unusual source.

Loading code from a cache (like 3, only the code may come from a
conventional source).

Dealing with singleton infested code. You might also step around memory
leaks, although you are probably more likely to cause them with the
current state of the JRE.

Security. Reflection, for one, allows access based on the callers class
loader (as well as access modifiers and security permissions). Obviously
separate class loaders stop applets/web apps fiddling with each other's
statics.

Tom Hawtin
 
R

Roedy Green

Dealing with singleton infested code. You might also step around memory
leaks, although you are probably more likely to cause them with the
current state of the JRE.

Could you elaborate on

1. how ClassLoaders make singleton code more tractable? It is that you
can gc classes sooner?

2. why is packratting more of a problem with singletons? Are you just
referring to the fact you have statics lying around that will never be
used again?
 
L

Lee Fesperman

Thomas said:
... Obviously separate class loaders stop applets/web apps fiddling with
each other's statics.

You don't need a custom class loader (your own implementation) to accomplish this.
 
T

Thomas Hawtin

Roedy said:
Could you elaborate on

1. how ClassLoaders make singleton code more tractable? It is that you
can gc classes sooner?

Say I've inherited some code that uses a singleton to access a database.
Now I want to use more than one database at once. I could go through and
replace all the singleton accesses with a design. Or I could have two
class loader instances, each one configured for a particular database.
2. why is packratting more of a problem with singletons? Are you just
referring to the fact you have statics lying around that will never be
used again?

If you've got a memory leak that doesn't go away if you drop all the
references from client code, then there's probably some static variables
involved. A cache that does not evict old entries is a memory leak. If
you drop the class loader referencing the static variables, the leaked
memory can come back. OTOH, there are a number of bugs that can cause
the class loader to hang around too.

Tom Hawtin
 
T

Thomas Hawtin

Roedy said:
For someone unemployed, you seem to have a huge amount of practical
experience.

Unemployed does not mean vegetating. "Three years commercial experience"
often means one months experience repeated 36 times.

I have spent a number of years as a Java programmer at three companies,
covering a nice spectrum of stuff. I just haven't been paid much in the
last two or three years. After starting my first job, all of my
subsequent career decisions have been absolutely terrible.

Tom Hawtin
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top