This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Java Memory Usage - OutofMemory Error  RSS feed

 
Ranch Hand
Posts: 387
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: 9846
199
  • 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: 63409
205
  • 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: 387
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.
 
Marshal
Posts: 24191
54
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: 9846
199
  • 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: 13160
219
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.
 
Bartender
Posts: 1184
74
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.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!