Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 746791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:07:53+00:00 2026-05-14T14:07:53+00:00

I have read somewhere that using the class instances as below is not a

  • 0

I have read somewhere that using the class instances as below is not a good idea as it might cause memory leaks. Can someone tell me if that is a valid statement? Or are there any problems using them this way?

Map<Class<?>,String> classToInstance = new HashMap();

classToInstance.put(String.class,"Test obj");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:07:53+00:00Added an answer on May 14, 2026 at 2:07 pm

    Yes, you do need to be cautious! For example, if your code is running in a web container and you are in the habit of doing hot deployment of webapps, a retained reference to a single class object can cause a significant permgen memory leak.

    This article explains the problem in detail. But in a nutshell, the problem is that each class contains a reference to its classloader, and each classloader contains references to every class that it has loaded. So if one class is reachable, all of them are.

    The other thing to note is that if one of the classes that you are using as a key is reloaded then:

    1. The old and new versions of the class will not be equal.
    2. Looking up the new class will initially give a “miss”.
    3. After you have added the new class to the map, you will now have two different map entries for the different versions of the class.
    4. This applies even if there is no code difference between the two versions of the class. They will be different simply because they were loaded by different classloaders.

    From Java 8 – Permgen was removed. Do you think it is ok to use Class instance as HashMap key in any situations?

    Be aware that you will still have a memory leak. Any dynamicly loaded class used in your HashMap (key or value) and (at least) other dynamically loaded classes will be kept reachable. This means the GC won’t be able to unload / delete them.

    What was previously a permgen leak is now a ordinary heap and metaspace storage leak. (Metaspace is where the class descriptors and code objects for the classes are kept.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read somewhere on this site or CodeProject that good rule is if
I have read somewhere that Silverlight is subset of WPF , then why triggers
To create a daemon I have read somewhere that in order to close all
I think I read somewhere that some modules only have object oriented interfaces (
I have a class that might throw any run-time exceptions during initialization. I want
I've read somewhere, that using anchor tag to submit a form isn't very safe,
I have read somewhere one of the main differences between Java and C++/C# is
I read somewhere the view ids used by JSF framework have a happy side
I am designing a test application (Using NUnit) that will have to navigate (through
I have a class called MembershipHelper, which I am using in my ASP.NET project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.