views:

20

answers:

1

This is tomcat loader structure:

      Bootstrap
          |
       System
          |
       Common
       /     \
  Webapp1   Webapp2 ... 

My question is how does the loaders actually work?

Do they load all classes when tomcat is started even when there is no request?

Or they load necessary classes when a request comes in?

And how about the life cycle of the loaded classes?

+1  A: 

Do they load all classes when tomcat is started even when there is no request?

The answer is maybe. The strategy used is down to your JVM implementation. More often than not classes are only loaded when they are required.

Or they load necessary classes when a request comes in?

Again it depends on your JVM's class loader strategy. Generally speaking though classes are only loaded when they are needed. For example, if request 1 doesn't needed class Foo but request 2 does then the class will only be loaded during request 2.

And how about the life cycle of the loaded classes?

Instances of the classes follow the normal garbage collection rules so they're deleted when there are no longer any references to them. As for the classes themselves, I'm not entirely sure. I'd imagine they follow a similar pattern, i.e. if there are no instances of the class left and the JVM needs more memory they it unloads the class.

Adrian