File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Deleting a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Deleting a class" Watch "Deleting a class" New topic

Deleting a class

Mohamed Jeleel

Joined: Mar 22, 2010
Posts: 18
Hi All,

I want to know in java can we delete a class loaded in to memory.

for example I have a login module. After user logs in I want to delete all the login module related classes its members etc... from the memory.
Because those classes will not need after that.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ClassLoader do that for you.
Mohamed Jeleel

Joined: Mar 22, 2010
Posts: 18
I am not clear.
Pushkar Choudhary

Joined: May 21, 2006
Posts: 425

code killer wrote:

Please edit your first and last name to use a real name. Obviously fictitious names are not allowed at Javaranch. Read this for more details.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19725

If you use a new class loader to load the classes, the classes loaded using this class loader will be unloaded when the class loader is garbage collected. This does require you to remove all other references to these classes, including references to instances.

URLClassLoader is used most often.

How To Ask Questions How To Answer Questions
David Newton

Joined: Sep 29, 2008
Posts: 12617

Technical issues aside, why are you trying to do this? It's unlikely the memory savings you see will be significant, or even register, in the great scheme of Java memory usage.
I agree. Here's the link:
subject: Deleting a class