• Post Reply Bookmark Topic Watch Topic
  • New Topic

static method is better to use instead of instance method in terms of memory?  RSS feed

 
karthikeyan sai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static method are invoked via a class name and instance method are invoked using class instance. For instance method invocation, typically for n number of user , n number of instance is created which is going to occupy more memory but for class method this will not be the case. Even though java automatically manages memory management, instance method still going to occupy memory.
my question is it is always better to use static method instead of instance method in terms of memory?
can any one throw some light on this?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. A method always occupies the same (very small) amount of memory whether it's a static method or an instance method.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krthik s.au wrote:
my question is it is always better to use static method instead of instance method in terms of memory?


you cant go for static method always![infact some situation only you can(if the method is common for all instance)] . please dont confuse yourself with memory management[if instance needs to create,you create it]
 
karthikeyan sai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is given in the JAVA performance Tuning book

"Avoid creating objects in frequently used routines. Because these routines are called
frequently, you will likely be creating objects frequently, and consequently adding heavily
to the overall burden of object cycling. By rewriting such routines to avoid creating objects,
possibly by passing in reusable objects as parameters, you can decrease object cycling."

From the above phrase, it is always to better go with static method rather than creating instance and calling instance method.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krthik s.au wrote:This is given in the JAVA performance Tuning book


Optimization advice often has a short lifespan. When was this book written?
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krthik s.au wrote:
From the above phrase, it is always to better go with static method rather than creating instance and calling instance method.


If you can make a method either static or non-static, then you should make it static. Not for performance reasons but for design reasons. If a method doesn't use information held in individual objects then it should be static because then it uses class-wide information only and then it belongs to the class.

It's generally a big mistake to let optimization considerations influence program design. It's highly likely that you end up with a bad design AND a slow program.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krthik s.au wrote:"Avoid creating objects in frequently used routines. Because these routines are called
frequently, you will likely be creating objects frequently, and consequently adding heavily
to the overall burden of object cycling. By rewriting such routines to avoid creating objects,
possibly by passing in reusable objects as parameters, you can decrease object cycling."

From the above phrase, it is always to better go with static method rather than creating instance and calling instance method.

No, that doesn't follow at all. The quote is talking about creating objects inside methods. It makes no difference at all whether those methods are declared static or not as far as that quote is concerned.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"krthic s,au",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!