Link to home
Start Free TrialLog in
Avatar of bashash
bashash

asked on

Removing .CLASS files after dynamically compiling using JAVAX

Hi Experts! Here is the problem I have encountered:

My application is written in java and uses JDK 6 to compile a series of strings dynamically using JAVAX.TOOLS.
This process is done every seconds, therefore the class names are as follows:
met11293132592002587812278410175.class

Where the long number is the millisecond epoch. (The reason the file name & class name changes are because the dynamic code is downloaded from a server and is different every time, otherwise the code will just look for the first compiled code).

Now this makes the application to create thousands of CLASSes in the classes directory and the size of that directory is increased over 400 MBs per 12 hours.

I have tried using regular java FILE.delete method, but since the class is locked from the custom classloader I have, it fails every time.
Also, Its not possible for me to unload the class and then remove, or at least I dont know how.

Your help is highly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of ernitingoel
ernitingoel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bashash
bashash

ASKER

Thanks for your reply ernitingoel.
I have managed to explicitly close each class and remove the file.
Here is what I did:
From within the same custom classLoader, (in Finally clause) I re-instantiate the same classLoader, and load the class, it some how releases the lock to the class (but not the classLoader), afterwards I can remove the file using FILE.delete() method. I can leave the old classLoader to the garbage collector. Not sure if thats good practice since the class loader is called every second. But I guess the garbage collector knows its job.
So, I guess im going to have to monitor and see if it really works (efficiently) and then mark this as solved.

Thanks