• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to release memory of String object.  RSS feed

 
Haritima Sharma
Greenhorn
Posts: 3
MySQL Database Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once we crate object that memory not released from JVM because character array in string class is private. Please suggest some way to release memory or how to make corrective action.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't. You let the garbage collector and string pool take care of things. Our responsibility is simply to not hang on to references to things that we no longer need.
 
Parvathi Raju
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the varaible is no longer used then the memory allocated by that variable will be reclaimed by garbage collector.We don't need to care about this.That's y java is robust
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one way is assigning string reference to nul.E.g

str=null


But I am not sure if it is advisable or should be done because there is already Garbage collector doing its job
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should not worry about it. Your computer has gigabytes of memory. Even my phone will have tons of memory available to the JVM if you are developing for mobile platforms.

Spending time worrying about freeing 20, 50, or even 100 bytes is probably not your most productive use of time. You'd be better served spending time writing clean, easy to understand and easy to maintain code.

ONLY if you can show - by using a legitimate profiling tool - that you have memory options should you even consider thinking about such things, and even then, only if you can show - again using the profile tool - that is is all the Strings that are hogging it should you worry about it.

Odds are MUCH more likely that any memory problem will be somewhere else in your code, assuming you even HAVE such problems.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:But I am not sure if it is advisable or should be done because there is already Garbage collector doing its job

Well, usually you're far better off to just let the object go out of scope; but the technique you show can certainly be used in extreme cases.

Before you do, you might want to check your code to see if you aren't hanging onto things for too long though.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!