• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Memory Usage - OutofMemory Error  RSS feed

 
Ranch Hand
Posts: 259
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: 8997
168
  • 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: 59453
187
  • 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: 259
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: 23607
48
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: 8997
168
  • 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: 12103
197
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: 848
27
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.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!