Two Laptop Bag*
The moose likes Java in General and the fly likes Why Object creation is costly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Object creation is costly?" Watch "Why Object creation is costly?" New topic
Author

Why Object creation is costly?

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
In Java when an instance is created for a class, JVM loads the class, initialize the instance variables by calling the constructor and its super constructor. Is it because of this we say object creation is costly or is there any specific reason because why we say object creation is costly?
Buddhika Mawella
Ranch Hand

Joined: Jan 06, 2011
Posts: 39

Object creation is considered as costly because it consumes your memory.As a result of this, program will suffer from lack of resources(memory) and it become slow.


BM
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Ummm... no.

Object creation is actually not nearly so costly as it once was, either in Java or in other languages. The reason for this old chestnut is that chunks of memory for objects have to be handed out by a piece of system software called an allocator. The allocator has to keep track of what memory is available to be used for new objects, and it has to be able to accept newly-freed memory to add back into the allocatable pool. Because of these responsibilities, an allocator is fairly complex, and historically, they took a relatively long time to hand out a chunk of memory. This time is the "cost", plain and simple.

Today's allocators are much more sophisticated than they once were, and creating an object is not a very expensive operation anymore. In fact, in modern JVMs, many objects are actually secretly allocated on the machine stack, and that's basically free-- it takes no time at all.


[Jess in Action][AskingGoodQuestions]
Buddhika Mawella
Ranch Hand

Joined: Jan 06, 2011
Posts: 39

Thanks for idea Ernest. My problem was not solved.But still when I am coding with java I am always cautious to create a new object if I really want a new object.And code reviewers always advice me not to create unnecessary objects as a best practice.According to your opinion are you saying that we do not need to be careful when creating new objects?. Wont it be a performance hit if we create many objects as we want?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Buddhika Mawella wrote:Wont it be a performance hit if we create many objects as we want?


In general, avoiding unnecessary object creation is generally a good idea, but the truth is that in recent JVMs creating small temporary objects -- especially wrapper objects like Integer or Double -- almost doesn't matter anymore. A lot of work has gone into making that kind of allocation essentially free.
Buddhika Mawella
Ranch Hand

Joined: Jan 06, 2011
Posts: 39

Ok. thanks.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Also, terms like "costly" and "cheap" are relative. You need to ask relative to what? And does it matter? Object creation may be 10 times slower than, say, adding ints, and 1000 times faster than reading a file. (I just made those numbers up; do not pay attention to the exact values, as they don't matter.) Does that mean we should never read a file? No, because reading a file is still pretty quick compared to many other things we humans do, like taking a breath of air or eating a donut. Likewise, creating objects is remarkably fast, and often it really has no discernable effect on your program's performance. Something else in the program is often much slower, yet still the program may be fast enough for the user that it just doesn't matter.

Put another way, avoiding unnecessary object creation may be a good idea if the object creation really is completely unnecessary. If nothing else, it's good practice for those comparatively rare times that it actually has an observable effect on performance. But don't work too hard to avoid creating objects that are actually useful to you in some way. That's what object-oriented programming is all about: we often find benefits in organizing our data and programs this way.
 
wood burning stoves
 
subject: Why Object creation is costly?