pythonee
pythonee

Reputation: 924

What if creat many object in short time in java?

I know the best practice is to avoid object creation. But have to some time.

These new instance just behave as temp object. They don't be referenced after create. How java GC react to this.

Can it cause OOM? If I can collect them immediately. How can I remove them from memory?

Upvotes: 0

Views: 115

Answers (2)

NPE
NPE

Reputation: 500367

In a modern JVM, you'll find that creating lots and lots of very short-lived objects is cheap. "Short-lived" is key here, as things do get more expensive for objects that live long enough to get promoted out the Eden space.

As a practical matter, I recommend using a good profiler to examine the actual performance of your application.

Upvotes: 3

Michael
Michael

Reputation: 821

If you worry so much about memory management you could also consider using C++ instead, in order to have complete control of your actions. On Garbage Collector behaviour.

Upvotes: 0

Related Questions