This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Java Memory Usage - OutofMemory Error  RSS feed

 
Ranch Hand
Posts: 340
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following code snippet:



Will it be a case of OutofMemoryError? Since we are creating infinite number of strings in the string pool, so, will we run outofmemory or not? I executed this program but it never throws this error...
 
Saloon Keeper
Posts: 9219
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you assume strings get added to the pool? The only string that is added to the pool is "hello". Strings that are created dynamically don't get added unless you intern them. Besides, the compiler may throw away the statement on line 5 because it doesn't do anything useful.

See what happens when you call s.intern() before the loop ends.
 
Marshal
Posts: 60083
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:. . .  it never throws this error...

Doesn't that answer your question?
 
Vaibhav Gargs
Ranch Hand
Posts: 340
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan and Campbell.

As per my understanding, the strings created using literals i.e. without using new operator will be allocated in String pool. Here we are not using new operator to create strings, so, won't they be created in string pool.
 
Sheriff
Posts: 23692
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:As per my understanding, the strings created using literals i.e. without using new operator will be allocated in String pool. Here we are not using new operator to create strings, so, won't they be created in string pool.



You don't understand what a String literal is. A String literal starts with a " character, then has a sequence of characters, then ends with a " character. You can (and probably should read the JLS) for a better description, though. You seem to think that the expression "hello"+i is a String literal because it doesn't use the new operator. But your characterization of String literals is wrong.
 
Stephan van Hulst
Saloon Keeper
Posts: 9219
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless the operands involved are compile time constants, the + operator creates new strings dynamically.
 
Sheriff
Posts: 12319
200
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:As per my understanding, the strings created using literals i.e. without using new operator will be allocated in String pool. Here we are not using new operator to create strings, so, won't they be created in string pool.


You seem to have contradicted yourself there.  The string literal "hello" will be in the String Pool. "hello" + i is a String expression; it will not be put in the String pool.
 
Saloon Keeper
Posts: 1037
43
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

JLS 9 15.18.1. String Concatenation Operator + wrote: If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time. The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string. The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).

Here is the link you can refer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!