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 998585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:13:11+00:00 2026-05-16T07:13:11+00:00

My experience shows that object creation in Java is very slow. Often, I optimize

  • 0

My experience shows that object creation in Java is very slow. Often, I optimize my code by just removing object creation and using the same objects repeatedly.

I am wondering if its similarly slow in other language with OOP base, because, to me, it is very counter intuitive that a language that’s hardcore object oriented, takes so much time for object creation.

Has anybody profiled this on several langauges?

  • 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-16T07:13:12+00:00Added an answer on May 16, 2026 at 7:13 am

    Try see what you get from this – some exercising to try to get the VM to not optimize the loop away:

    import java.util.ArrayList;
    import java.util.Random;
    
    class DummyInteger {
        private int i;
        public DummyInteger(int i) {
            this.i = i;
        }
        public int getI() {
            return i;
        }
    }
    
    public class ObjectCreationTest {
    
        final static int ITERATIONS = 2000000;
        public static void main(String[] args) {
            Random r = new Random(System.currentTimeMillis());
    
            int[] randArr = new int[ITERATIONS];
            for(int i = 0; i < ITERATIONS; i++) {
                randArr[i] = r.nextInt();
            }
            DummyInteger[] iArr = new DummyInteger[ITERATIONS];
            long start = System.currentTimeMillis();
            for(int i = 0; i < ITERATIONS; i++) {
                ArrayList<DummyInteger> list = new ArrayList<DummyInteger>(); 
                list.add(new DummyInteger(randArr[i]));
                iArr[i] = list.get(0);
            }
            long end = System.currentTimeMillis();
            System.out.println(String.format("Creating %d objects took %d ms",ITERATIONS*2,end-start));
            int sum = 0;
            for(DummyInteger mi: iArr) {
                sum += mi.getI();
            }
            System.out.println(sum);
            }
    }
    

    I got

    Creating 4000000 objects took 678 ms

    Which should be around 170 nanosecond per object.

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

Sidebar

Related Questions

I experience the following problem while using legacy VC6. I just cann't switch to
In the place I work, very often it happens that a developer and QA
I'm trying to create a database with history in mind (experience shows you'll have
I experience a weird problem; I have a bash script that runs as wwwrun,
I experience a very strange problem and I don't have found any help to
I experience strange problem. We have error handling in global.asax that would redirect user
Some clients experience an app crash directly when they launch it. Here's the code
I am trying to generate an <object> tag only embed code and cannot get
I'm not experienced with java applications but I found out that finding static pointers
I'm new to java but my experience with Matlab and C trained me to

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.