GeeCON Prague 2014*
The moose likes Performance and the fly likes Not making Strings as null reference in static method has impcat on performace? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Performance
Bookmark "Not making Strings as null reference in static method has impcat on performace?" Watch "Not making Strings as null reference in static method has impcat on performace?" New topic
Author

Not making Strings as null reference in static method has impcat on performace?

Sarath Chandra
Greenhorn

Joined: Jan 31, 2007
Posts: 23
Hi,

If in a static method 2 strings are created for temp caluclations.Before exiting the method these temparory string variables hasnt been made as null reference. This method has been called number of times.Then will it have any impact on performace.

i tried to calculate the time for this method and the method with strings made as null and with a private method,But not much differnce for these 3 methods even with 10000000 number loops .Will it have any impact on memory usage.Pleae tell me how exactly i can get the details about memory usage.I tried using gc option but couldnt get much help.

Below is the sample method.

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Sarath,

variables you declare within any method are local variables. They are local to this method and only exist until your method exits. Theses variables are stored on the stack of the JVM each time you invoke a method and automatically get lost when you're method exits so you don't have to set the references to null or something like this (which with current JVMs isn't a good idea anyway) in order to "help" the garbage collector.

With string literals (like "hello") there's a little difference to most other objects but that's another story...

Marco
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Most people are shocked if they ever look at the bytecode that implements array declarations like the ones you've shown here. The arrays are created from scratch each time the method is called, setting each element, one at a time, exactly as if you had written out all the initializations yourself.

I'm telling you this because if you're interested in improving the performance of replaceUTLSpecialChars, then make 'schars' and 'chars' into member variables -- i.e., declare then private static final String[] at the class level -- so that all that work is done just once. Since the arrays are only read and never written to, this would work perfectly, and give you a noticeable improvement.

Of course, there are other far more important ways to speed this up. Instead of using "replaceChars" in a loop (aak!), I'd use an explicit loop over the chars in str, and append the appropriate characters to a StringBuilder in response to each one. Furthermore, you could make schars an int[] and use Arrays.binarySearch to find out whether each character in str existed in schars. The resulting code could be orders of magnitude faster for moderate-sized inputs!


[Jess in Action][AskingGoodQuestions]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You might even find there's a performance improvement if you just replace all that code by a call to the URLEncoder.encode method.

Sarath Chandra
Greenhorn

Joined: Jan 31, 2007
Posts: 23
Originally posted by Paul Clapham:
You might even find there's a performance improvement if you just replace all that code by a call to the URLEncoder.encode method.



Hi all,
Thanks for you suggestions.
Above method that i have posted is taken from one our utilities.
When i tried profile our application using Jprofile(just learning ) ,lot of String instance of this method are alive.

Previously i thought just as Marco Ehrentreich said since these are local variables and only exists untill the method exits.So i just want to be clear on that.

Ernest Friedman-Hill :
I tried with private static string declarion but couldnt find much time differnce for method execution.

if we make the same method as private instead of static and with and without making strings as null reference does all these methods be same performance wise?

Thanks,
Sarath
[ April 02, 2008: Message edited by: Sarath Chandra ]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Sarath,

are you really sure that the local variables do exist even if your method exits? I'm pretty sure that your references schars and chars vanish immediately. Of course it will take some time until the corresponding objects (Strings) on the heap get collected by the garbage collector. This is like Ernest and Paul already said far from ideal regarding performance but at least the Strings should get collected.

Regarding the performance I can't imagine that there could be a difference. Probably when static or non-static members or multiple threads come into play there may be a difference if you use a static method and static members or non-static methods. But in your case I don't think it will make a noticeable difference. Why do you think there is a difference? I think it would be much better to reason about the things Ernest and Paul said if you try to optimize performance. You should first take care of the big waste of performance when you create schars and chars from scratch with each method call. If there's a performance bottleneck after you've improved this than you can still think about other optimizations.

Marco
[ April 02, 2008: Message edited by: Marco Ehrentreich ]
Sarath Chandra
Greenhorn

Joined: Jan 31, 2007
Posts: 23
Hi,
Ok,Thank you.I will take a note of your valuble suggestions.

Here is antoher method which jprofiler is showing as hotspot



Is this method is same as Object clone or is there any other differences.

Thanks,
Sarath
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Sarath,

this one is of course not exactly the same as the clone() method of class Object. Class Objects has a protected non-static clone() method with no parameters. By default the clone() method of Object performs a shallow copy of the object you call.

Besides it's difficult to tell what's going on with the method you posted. I'd say it depends very much on the kind of objects you try to clone here. But at first glance it doesn't seem one of the most elegant solutions to put your objects through this streams in order to clone them. But without further knowledge of what the objects really are it is hard to judge if it could be done more effective.

Marco
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Sarath:
Is this method is same as Object clone or is there any other differences.


No this is not the same as the clone method in Object class.
The clone method in Object class is a native and protected method.


apigee, a better way to API!
Sarath Chandra
Greenhorn

Joined: Jan 31, 2007
Posts: 23
Hi Marco,

Originally posted by Marco Ehrentreich:
depends very much on the kind of objects you try to clone here.
Marco


Here is the actual object



And the calling method is
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Well, I dont see any reason to implement the method clone that you have shown.
Ideally the class should implement Cloneable. If not, then it should provide a copy constructor i.e. a constructor that is passed an instance of the same class . This constructor copies all the state of the passed object to the new instance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

With apologies to Nitesh and Marco, your version of clone() is actually a common and reasonable idiom for deep-copying an object so that the copy is completely independent of the original. It will not only make copies of the List and Map members, but also of the contents of those Lists and Maps. Yes, it's relatively slow -- but if you need to make all those copies, then this is a nice, safe, simple way to do it. The "safe" part is that you can't forget any members -- they all get done automatically; so if the class changes, there's no danger of forgetting to update clone().

If, on the other hand, you need to copy the lists and maps but don't actually need to copy the objects they contain, and have a process in place for ensuring that clone() gets updated if members are added to the class, then you could easily write a more efficient clone() by, for example, calling clone() on the List and Map members and storing the copies in the new object's member variables.
Sarath Chandra
Greenhorn

Joined: Jan 31, 2007
Posts: 23
Thanks Ernest,

As i mentioned earlier i profiled the application using jProfile(i didnt profiled but i have some snapshots of application),as per HotSpots shown by Jprofiler lot of char[] and String live objects (almost 50% of char[]&string live objects)are from java.io.ObjectInputStream.readObject and java.io.ObjectOutputStream.WriteObject of Clone method mentioned above.

So making bstream and other objects in the Clone method as null reference will make these objects to be garbage collected freequently?

Thanks,
Sarath
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Sarath Chandra:


So making bstream and other objects in the Clone method as null reference will make these objects to be garbage collected freequently?


No, I didn't say a word of anything like that, did I?

There is only one situation in which deliberately setting local variables to null does any good at all: if the method performs multiple steps, runs for a long time, and the data used by the first step is not used during the second step. Even so, this only matters while the method is running. As soon a any method returns, it's exactly as if you had set all its local variables to null.

I've discussed at least three different ways to improve the performance of this program significantly, and none of them has anything to do with setting local variables to null. Regardless of what you've read or been told, setting local variables to null is very rarely the answer to everything. Instead of concentrating on this, and on what the memory profiler is telling you, rather you should think about the suggestions made above.

For example, if you think there are two many String instances, this could be because the deep clone() is copying all the Strings that are part of your object and its members. Replacing that clone() with a shallow(er) one that didn't copy the strings would not only be faster, but would significantly reduce the number of Strings being created in the first place!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not making Strings as null reference in static method has impcat on performace?