| Author |
Do static variables, methods impose a penalty
|
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
Hi All As the static methods and variables are loaded even when there is no class instance loaded and probably remain in the JVM till it shuts down. So do these variables and methods impose a performance penalty with respect to there non static counterparts. I am talking of scenario when there are probably hunderds of objects and each living a very short life.
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12324
|
|
I doubt very much that you could measure it even if there is a penalty. Think about it - what are your alternatives? All of the constants that might be used to initialize instance variables have to be part of the class object anyway. Bill
|
Java Resources at www.wbrogden.com
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
The byte code of methods is only loaded once - when the class is loaded - anyway, and local variables live on the stack, that is, their memory is allocated when the method is called. There is no difference in this respect between static and non-static methods.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
Thank you William and Ilja. I also thought it the same way. But your posts gave way to some other questions [WB] All of the constants that might be used to initialize instance variables have to be part of the class object anyway. But won't that be shortlived. Let say the frequency usage of the static method class is very less. So if I do invoke this class let say after x CPU cycles won't instance vars. be more beneficial. [IP] local variables live on the stack, that is, their memory is allocated when the method is called. It gives me all the more reason to use instance methods. Once the stack is over everything is over for that method, but the same wouldn't be true for static methods.
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12324
|
|
But won't that be shortlived.
No, once the class is loaded, there they sit in memory. That includes the bytecodes (or machine code if the JIT compiler has run) - not matter how many instances of the class are created, they ALL use the same bytecodes so there is no difference with static methods. Choosing between static methods and variables versus instance etc is more related to how the class is used than to performance. Bill
|
 |
steve souza
Ranch Hand
Joined: Jun 26, 2002
Posts: 852
|
|
Here is a similar thread on the topic. http://www.coderanch.com/t/202303/Performance/java/Static-vs-Instance
|
http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Anupam Sinha: [IP] local variables live on the stack, that is, their memory is allocated when the method is called. It gives me all the more reason to use instance methods. Once the stack is over everything is over for that method, but the same wouldn't be true for static methods.
It would be true for static methods, too. The stack memory is only reserved for a method *call* - once the method returns, the memory is freed again. As far as I know, the very only difference between static and instance methods is whether the "this" reference is available.
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1170
|
|
I hope you are seeing the difference between an instance of the class definition and an instance of the class. Both of these are runtime objects. If a variable is defined as belonging to an instance of the class, then it dies when the instance of the class dies. If a variable is defined as belonging to an instance of the class definition (a static variable) then it dies when the class definition instance dies. Which happens when the class definition is unloaded. The only difference here is scope. As to thread local memory, in this case 'stack' memory, all variables that are to be worked on will be placed on the stack. It does not matter if its a class variable or an instance variable. A variable is a variable. In this respect, there can be no performance difference. [ March 12, 2007: Message edited by: Mr. C Lamont Gilbert ]
|
 |
sreekanth nair
Greenhorn
Joined: Mar 12, 2007
Posts: 20
|
|
As long as you have enough memory in your computer.No performance issue will come up. Static variable or static methods are written to the memory as soon as complier finds it. The name itself 'Static' will give the idea about it. So if you purposefully need a class member to be static then only you have to make it as static.
|
 |
 |
|
|
subject: Do static variables, methods impose a penalty
|
|
|