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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:20:08+00:00 2026-05-26T14:20:08+00:00

Consider the following two segments of code in Java, Integer x=new Integer(100); Integer y=x;

  • 0

Consider the following two segments of code in Java,

Integer x=new Integer(100);
Integer y=x;
Integer z=x;


System.out.println("Used memory (bytes): " +   
(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));

In which the memory usage was when tested on my system : Used memory (bytes): 287848


and

int a=100;
int b=a;
int c=a;

System.out.println("Used memory (bytes): " + 
(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));

In which the memory usage was when tested on my system : Used memory (bytes): 287872



and the following

Integer x=new Integer(100);       
System.out.println("Used memory (bytes): " +  
(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));

and

int a=100;        
System.out.println("Used memory (bytes): " + 
(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));

in both of the above cases, the memory usage was exactly the same when tested on my system : Used memory (bytes): 287872



The statement

System.out.println("Used memory (bytes): " + 
(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));

will display the total memory currently in use [Total available memory-Currently free available memory], (in bytes).


I have alternatively verified through the above mentioned methods that in the first case the memory usage (287848) was lower than the second one (287872) while in the rest of the two cases it was exactly the same (287872). Of course and obviously, it should be such because in the very first case, y and z contain a copy of the reference held in x and they all (x, y and z) point to the same/common object (location) means that the first case is better and more appropriate than the second one and in the rest of the two cases, there are equivalent statements with exactly the same memory usage (287872). If it is so, then the use of primitive data types in Java should be useless and avoidable though they were basically designed for better memory usage and more CPU utilization. still why do primitive data types in Java exist?


A question somewhat similar to this one was already posted here but it did not have such a scenario.

That question is here.

  • 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-26T14:20:09+00:00Added an answer on May 26, 2026 at 2:20 pm

    I wouldn’t pay attention to Runtime.freeMemory — it’s very ambiguous (does it include unused stack space? PermGen space? gaps between heap objects that are too small to be used?), and giving any precise measurement without halting all threads is impossible.

    Integers are necessarily less space efficient than ints, because just the reference to the Integer takes 32 bits (64 for a 64-bit JVM without compressed pointers).

    If you really want to test it empirically, have many threads recurse deeply and then wait. As in

    class TestThread extends Thread {
        private void recurse(int depth) {
            int a, b, c, d, e, f, g;
            if (depth < 100)
                recurse(depth + 1);
            for (;;) try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException e) {}
        }
    
        @Override public void run() {
            recurse(0);
        }
    
        public static void main(String[] _) {
            for (int i = 0; i < 500; ++i)
                new TestThread().start();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following two blocks of Java psedo-code in a system that uses optimistic
Consider following piece of code: JSONObject json = new JSONObject(); json.put(one, 1); json.put(two, 2);
Consider the following two ways of writing a loop in Java to see if
Consider the following two Java files that contain a simplified version of my issue
Consider the following .Net ASMX web service with two web methods. using System; using
consider the following two pieces of code: public static Time Parse(string value) { string
Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
Consider the following two methods, written in pseudo code, that fetches a complex data
Consider the following two statements out of a very simple assembly language program: DATA1
Consider the following two fragments of code: scala> def f1(x:Any) = x match {

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.