• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to manually delete an object from String constant pool ?  RSS feed

 
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an object that is placed in the String constant pool has no reference left, how can we manually delete it from there?
Since String constant pool exists in the Class/Method Area, it is inaccessible to Garbage Collector which works on the Heap Area in JVM.
Apart from the fact that it will automatically get deleted when the JVM shuts down on the code finishes its execution. Is there any other way??

Thanks,
Shivom
 
Rancher
Posts: 42972
73
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What prompted this question? You're not really worrying about single object instances, are you?
 
Shivom Shukla
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What prompted this question?


Curiosity...I was wondering how this happens.. I asked a couple of people but they had no idea.

 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivom Shukla wrote:Curiosity...I was wondering how this happens.. I asked a couple of people but they had no idea.

Nothing wrong with curiosity, but have you thought that maybe the reason they don't know is that it doesn't happen?

I suppose its just possible that the JVM could run a Thread behind the scenes to remove all unreachable objects from the String pool, but
(a) Is it really worth tying up resources for something that might save you a few K of memory - remember, the String pool is mainly used for String literals.
(b) Is it really any of your business?
And that last question isn't meant to be rude; it's a genuine "scope" issue.

Java is not like C or C++ and there are many, many things that the JLS/JVM specs don't tell you about memory or how it's used.
The philosophy is simple:
  • create objects.
  • use 'em.
  • let the GC deal with 'em when you're done.
  • Memory is NOT your concern - unless you're programming for a cell phone, in which case the question may well be moot, since there might not even be a String pool (I honestly don't know).

    HIH

    Winston
     
    Sheriff
    Posts: 22844
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's extremely common and perfectly normal for strings in the constant pool to have no references. Consider this code:



    Here you have an example of a string constant which never has any references. Are you sure you want it to disappear from the pool? What would you like to happen when that line of code is executed after the "Hello World" constant has been garbage collected?

     
    Shivom Shukla
    Ranch Hand
    Posts: 77
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Winston and Paul ,

    I have written how objects will be deleted when I asked this question and my answer is obvious for all developers.
    But i wanted to know if it was possible to delete it from String constant pool manually or any process that JVM ran to delete them other than finishing the code or shutting down.

    Thanks,
    Shivom
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shivom Shukla wrote:I have written how objects will be deleted when I asked this question and my answer is obvious for all developers.
    But i wanted to know if it was possible to delete it from String constant pool manually or any process that JVM ran to delete them other than finishing the code or shutting down.

    My answer: Probably not. But it still begs the question: Why would you even want to?

    Winston
     
    Shivom Shukla
    Ranch Hand
    Posts: 77
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Winston,
    So i think it can be concluded that objects cannot be deleted from string constant pool ?

    There is no reason why i wanted to do so. I agree with you that it is not required just for a few k,
    but just wanted to know if it was possible or not.

    Thanks,
    Shivom
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shivom Shukla wrote:So i think it can be concluded that objects cannot be deleted from string constant pool ?
    I actually said "probably not". The simple fact is: I don't know.

    There is no reason why i wanted to do so. I agree with you that it is not required just for a few k,
    but just wanted to know if it was possible or not.
    My suggestion: don't think of the String pool as a bunch of objects that need to be deleted; think of it as a cache - because that's exactly what it is. And it's there specifically to save space.

    Now, if you wrote a cache, would you want clients to be able to clear it, or remove objects from it, any time they want?

    Winston
     
    Shivom Shukla
    Ranch Hand
    Posts: 77
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston, you are right. I finally got the answer (after researching all this time) that the only time objects get deleted from the String constant pool is when the class gets unloaded.
    So if you want to manually delete the objects from SCP, then unload the class.

    Regards,
    Shivom
     
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have programmed in Java for over 12 years now and never, ever, not even once have I needed to unload a class to force the removal of a String object from the string pool. Don't worry about this stuff; some very smart people have already done the hard work around this for all of us. Worry instead about YOUR code and design that solves an actual business problem for YOUR CUSTOMER.
     
    Shivom Shukla
    Ranch Hand
    Posts: 77
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Junilu,

    I have programmed in Java for over 12 years now and never, ever, not even once have I needed to unload a class to force the removal of a String object from the string pool. Don't worry about this stuff; some very smart people have already done the hard work around this for all of us. Worry instead about YOUR code and design that solves an actual business problem for YOUR CUSTOMER.


    It is not about use, as i had previously said, its just out of curiosity.

    Thanks,
    Shivom
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!