Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 949891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:28:31+00:00 2026-05-15T23:28:31+00:00

I was using newInstance() in a sort-of performance-critical area of my code. The method

  • 0

I was using newInstance() in a sort-of performance-critical area of my code.
The method signature is:

<T extends SomethingElse> T create(Class<T> clasz)

I pass Something.class as argument, I get an instance of SomethingElse, created with newInstance().

Today I got back to clear this performance TODO from the list, so I ran a couple of tests of new operator versus newInstance(). I was very surprised with the performance penalty of newInstance().

I wrote a little about it, here: http://biasedbit.com/blog/new-vs-newinstance/

(Sorry about the self promotion… I’d place the text here, but this question would grow out of proportions.)

What I’d love to know is why does the -server flag provide such a performance boost when the number of objects being created grows largely and not for "low" values, say, 100 or 1000.

I did learn my lesson with the whole reflections thing, this is just curiosity about the optimisations the JVM performs in runtime, especially with the -server flag. Also, if I’m doing something wrong in the test, I’d appreciate your feedback!


Edit: I’ve added a warmup phase and the results are now more stable. Thanks for the input!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:28:32+00:00Added an answer on May 15, 2026 at 11:28 pm

    I did learn my lesson with the whole reflections thing, this is just curiosity about the optimisations the JVM performs in runtime, especially with the -server flag. Also, if I’m doing something wrong in the test, I’d appreciate your feedback!

    Answering the second part first, your code seems to be making the classic mistake for Java micro-benchmarks and not “warming up” the JVM before making your measurements. Your application needs to run the method that does the test a few times, ignoring the first few iterations … at least until the numbers stabilize. The reason for this is that a JVM has to do a lot of work to get an application started; e.g. loading classes and (when they’ve run a few times) JIT compiling the methods where significant application time is being spent.

    I think the reason that “-server” is making a difference is that (among other things) it changes the rules that determine when to JIT compile. The assumption is that for a “server” it is better to JIT sooner this gives slower startup but better throughput. (By contrast a “client” is tuned to defer JIT compiling so that the user gets a working GUI sooner.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i load a class using Class.forName(klassname,false,loader) After this i create an instance using klass.newInstance();
I'm trying to instantiate a class using the Constructor.newInstance() method but am running into
I am using this code to parsing xml DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db
I am validaing a xml using following code snippet. SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); try
I am using the following code to dynamically load a class in java: URL
According to java ee 5 api ExpressionFactory provides a method #newInstance . I'm using
Using a java.lang.reflect.Proxy , I can create a class that takes an InvocationHandler as
I was under the impression that using the Resolve method returned a new instance
I have some code using a System.Transactions.TransactionScope , that creating a new instance of
I want to create an object of a class, and override some of it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.