Loading Java classes that arent needed

I'm currently wondering what the actual overhead in the JVM is for loading extra classes that are never used.

We have code that iterates over all classes in the class path to search for classes that implement a specific interface, then we load them.

This allows custom classes to be simply dropped into the directory, and they are loaded and registered.

A side effect is that we push each class in the class path, causing the classes to load. What will affect the JVM memory?

Does loading classes simply affect memory in general?

+4
source share
5 answers

As usual, I would suggest measuring this for your specific scenario.

Having said that, I'm not sure that I advise scanning all the way to the classes. If you do not control the class path (this is your client or similar), they can potentially add something to it, and your process will scan everything that falls into their class path (possibly not related to your application).

I would suggest that you only assign specific directories / repositories to which classes can be downloaded, and in this way you limit the scanning of the class path and reduce the chance of inadvertently collecting materials that you do not intend to do.

+4
source

If you use a separate ClassLoader to load these classes and are very careful not to create references to these classes or instances from them, then when ClassLoader becomes suitable for garbage collection, also classes.

This way you can avoid unnecessarily clogging the PermGen space by doing 2 passes with separate ClassLoaders: one to load all the classes and determine which ones you want to keep, and the other to actually use them.

+2
source

Would using ClassLoaders this way have unintended side effects? Like starting static initializers, etc.

You can use the ServiceLoader mechanism, but if this does not work, you can check classes without using the ClassLoaders - byte manipulations libraries such as BCEL and ASM can be used to check classes.

+1
source

Yes, this forces the virtual machine to load the class file and validate it (which could be a performance issue). Moreover, if you use Sun VM, then these classes will remain in memory forever. Sun VM puts classes in the so-called PermGen space, which never collects garbage unless you specify a special option.

So this is usually a bad idea, but there are two simple workarounds:

  • Check the class name (file name). Repeat the interface name in the name so that you can easily notice what you need to download and what not.

  • Use two directories. One contains normal classes, the other contains all the ones you always want to load.

0
source

The sentence "far there":

Could you do the same, but without actually using the virtual machine? the class file specification is documented, could you write your own application just to read the class files and find out if they implement their interface / regardless of whether they load them?

This will give you the opportunity to scan any directories, but without any problems with loading classes or static insiders or something like that.

0
source

Source: https://habr.com/ru/post/1285997/


All Articles