Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where and when the use of primitive data types in Java is effectively appropriate?

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.

like image 786
Bhavesh Avatar asked Oct 10 '22 14:10

Bhavesh


1 Answers

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();
    }
}
like image 75
Daniel Lubarov Avatar answered Oct 31 '22 17:10

Daniel Lubarov