Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

freeing up of memory used by strings

 
Sarada Bhasker
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is the memory used up by strings released in a servlet or JSP service method?
Will it make any difference if it is a string literal or string
created using new String() method;
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like for every other object, Strings can be GCed when they have no reference. Your local variables can be GCed when the response is finished.
The trickiest chance for running low on memory would be if you store lots of objects in a session - they won't be GCed until the session is destroyed.
String literals are kept around and won't be freed as long as the Class is loaded.
Bill

------------------
author of:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic