Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can static methods cause any kind of overhead?

 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, that`s about it.
Can static methods cause any kind of overhead?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, static methods generally cause less overhead. As polymorphism does not apply to static methods, there are no runtime checks that must be performed to determine which method to invoke.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean if this method is called a lot... i meand a lot by really a lot.
Should i build a non static method and always instantianting a object to use it (only to call the method) or should i mantain the static method?
I know that the vm only maintains one copy of each method in memory, but anyway... i don`t know exactly what should be the best option.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static method would still have less overhead than instanciating an instance of a class to call the method. My only concern on having the static method would be whether or not there was the possibility of multiple threads calling the method. If not, then leave it static.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Bell:
My only concern on having the static method would be whether or not there was the possibility of multiple threads calling the method. If not, then leave it static.


Actually, there might be a lot of threads accessing this method, however it`s not a method that must be thread safe (i don`t need to actually use shynchronized), because it doesn`t manipulate any instance variables (FIXED LATER: i meant class variables).

Why did you say to leave it as static only if there not too many threads accessing it?? Isn`t that the overhead i`m talking about?

Sorry, if i didn`t get the point.

[ January 27, 2005: Message edited by: Leandro Melo ]

[ January 27, 2005: Message edited by: Leandro Melo ]
[ January 27, 2005: Message edited by: Leandro Melo ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leandro Melo:


Actually, there might be a lot of threads accessing this method, however it`s not a method that must be thread safe (i don`t need to actually use shynchronized), because it doesn`t manipulate any instance variables.[ January 27, 2005: Message edited by: Leandro Melo ]

A static method can't manipulate instance variables, even if you wanted to. However, does this method manipulate any class (static) variables?

I would agree with the above assessment that the static method will have LESS overhead than an instance method. Creating an instance of a class just to call a single method seems like a lot of overhead to me. First, there's the time need to allocate memory and call the constructor. Second, the instance takes up some amount of memory.

Layne
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your static method is really thread safe (be sure you're right) then lots of threads calling it a lot will be very efficient. This is how the Math methods are done.

it`s not a method that must be thread safe (i don`t need to actually use synchronized), because it doesn`t manipulate any instance variables.


Keep in mind that many operations on static variable are not thread safe.
[ January 27, 2005: Message edited by: Mike Gershman ]
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't mean to only leave it static if there are only a few threads, but if there are many threads you have to be more careful. Even in the case of many threads a static method is most likely going to have less overhead. The number of threads shouldn't make a difference in that regard.

The point about the number of threads is that you have to be careful to either be sure it is thread safe, or syncronize it properly.

If there were thread safety issues I could see situations that would carry less overhead by allowing threads to have a local copy of the method, but there it is a tradeoff between the overhead of syncronizing and the overhead of mulitple instansiations. I wouldn't claim to know which would be better, that would require some analysis with a profiler.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:

A static method can't manipulate instance variables, even if you wanted to. However, does this method manipulate any class (static) variables?
Layne


That`s what i meant, sorry for the word instance.

Thanks for your opinions.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, using static vs. instance methods shouldn't be decided based on performance considerations. In 99.999% the difference simply is negligible.

What you *should* think about are design considerations such as flexibility and maintainability. And regarding those properties, instance methods often win (simply because they are polymorphic).

Regarding the java.lang.Math example, that's just necessary because numbers (int, long, double etc.) simply aren't objects, and therefore can't have methods. In languages where they are (i.e. Smalltalk), those methods are typically defined as instance methods of an abstract Number base class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic