dinesh028
dinesh028

Reputation: 2187

Does java creates a new object each time when new operator is called

In java, When we call a new Constructor(), then a new object is created each time i.e; a new memory is allocated or suppose there are already many objects created for a class that do not have any reference.

So can java return such object that are marked for de-allocation or will the java create a new object each time a new constructor() is called.

What's my basic intention to ask this question is if such thing happens then performance can be improved as the cost to create a new memory and destroying a un-refrenced object will be reduced.

Upvotes: 3

Views: 2496

Answers (6)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 341003

Java always creates new object. Note that new operator is very fast in Java. There is no allocation, typical JVM will just increment one pointer on heap. Once heap is full, old and unnecessary objects are removed and live are compacted. But garbage collection is a different story.

Your idea is clever, but would actually worsen performance. JVM would not only have to keep track of dead objects (eligible for GC) which it is not doing. But also it would have to clean up the old object somehow, so that it appears to be fresh. That's non-trivial and would take a lot of time. new is both faster and simpler.

There is one catch, though:

Integer answer = 42;

42 is a literal that has to be converted to Integer object. However JVM won't simply call new Integer(42) but Integer.valueOf(42) instead. And in the latter case valueOf() sometimes returns cached values (it will e.g. for 42).

Upvotes: 4

sheidaei
sheidaei

Reputation: 10342

There is a design pattern called flyweight, its main advantage is to reuse objects. Java uses that in creating strings itself.

you can learn more about it here: http://en.wikipedia.org/wiki/Flyweight_pattern

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533920

The cost of creating objects and destroying unreferenced object is trivial. What takes the time is

  • detecting when an object is no longer referenced. To do this every strong reference must be checked.
  • copying objects which are kept from one generation to another and defragmenting a generation.
  • finalising objects which implement the finalize() method.

If you create short lived temporary objects whether your Eden size is 8 MB or 8 GB, the time it takes to do a minor collection is almost the same.

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213411

Wherever you see new(), you can be pretty sure that a new object is being created.. As simple as that..

Upvotes: 0

Jesper
Jesper

Reputation: 207026

Yes, when you use new in Java, a new object is always created.

However, that does not necessarily mean that the JVM has to ask the operating system to allocate memory. How the JVM exactly allocates memory is up to the particular JVM implementation, and most likely it contains a lot of optimizations to make it fast and efficient.

Object allocation is generally considered a cheap operation in Java - usually you do not need to worry about it.

One example of a sophisticated optimization that's implemented in the current versions of Oracle's Java is escape analysis.

Upvotes: 2

SLaks
SLaks

Reputation: 888293

Yes.

Java will never re-use an object.

Upvotes: 5

Related Questions