wood burning stoves*
The moose likes Java in General and the fly likes Memory Allocation for Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory Allocation for Strings" Watch "Memory Allocation for Strings" New topic
Author

Memory Allocation for Strings

Venkatesh Rangamani
Ranch Hand

Joined: Aug 24, 2005
Posts: 31
Hi All,
I have a doubt on the allocation of memory for String Literals, I have a big application where we log(using Log4j) lot of information as below, the implementation of info method is done by us to write the data to a file.
logger.info("Data To Be Logged");

My Doubt is will the above String "Data To Be Logged" be stored in the internal String table, which has the reference to the String literals that are created in our code. Generally whenever we create String literals java internally stores the reference to the literals in a table, so that it doesnt have to create a new object if same string is used again. But, i wonder wether in the above scenario where we pass String as argument to a method call , or even a String parameter passed to System.out.println will find a place in the internal table.

Thanks
Venkat
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
More of a beginner's question.

[gross oversimplification]Any String which the JVM or compiler can recognise as such goes in the Strings table; if there are two Strings which the JVM recognises as identical, only one String object is created, unless the code instructs otherwise with the new keyword.[/gross oversimplification]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think Campbell's "gross oversimplification" overdid the simplification, as Venkat seems to already understand the basics of what Campbell said. In particular Venkat recognizes that not all Strings are interned, just literals. (And constant expressions and any String for which the intern() method is called, but those are outside the scope of Venkat's question.)

So, Venkat: within the info() method, there is no reason for the compiler or JVM to think that the String argument has been interned. It could have been called with any String argument. So nothing inside the info() method will cause the String to be interned, or not. But remember, the info() method doesn't get called until the class which calls info() has already been loaded. So the class which contains the string "Data To Be Logged" has already been loaded, and "Data To Be Logged" has already been placed in the intern table, before info() is called. The info() method gets a reference to a String which is already in the intern table.

If info() were called with an argument which is not a literal (as would be more common - why bother logging a constant?) then the argument would not have been interned. (Unless the intern() method was called explicitly.) Either way, the info() method doesn't know or care whether a String has been interned, and it has no effect on the status of that String.


"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Sorry.

I find it hard to work out how much people already know.
Venkatesh Rangamani
Ranch Hand

Joined: Aug 24, 2005
Posts: 31
Jim ,
Thanks for your Response . I dont get wat you are trying to say in your last paragrapg. Also, I missed to clarify one more thing in this issue.
If i call a method as say:

public class MyClass{
:
:
public static void main(String[] args){
int aRuntimeVariable = 5;
someMethod("A Constant String "+aRuntimeVariable);
aRuntimeVariable = 6;
someMethod("A Constant String "+aRuntimeVariable);
:
:
}
public static void someMethod(String inpt){
:
:
}
}

In this case the intenal String table will have reference to two String Objects namely:
"A Constant String 5"
"A Constant String 6"
. Am i Correct???



Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I believe that the constant part will be interned in the string table, but the Strings resulting from concatenating the constant part and the integer will not be interned or in the string table. They will be eligible for GC as soon as the method calls return.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes - and that's what I was trying to say in my last paragraph. Since "A Constant String 5" and "A Constant String 6" are not constants, they will not be interned.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory Allocation for Strings
 
Similar Threads
doubt regarding strings
String literal Pool doubt
doubt
HFJ doubt
Doubt on String Comparisson