Resource Tests: Java vs Mono/.NET

As someone who has recently begun to experiment with the Linux operating system I have also been introduced to .NET’s Linux’s cousin Mono. This has made me question what the best cross-platform program language to use is. I am familiar with both Java and various .NET languages (Visual Basic & C#) so I decided to run a few tests to see what the resource usage on my Linux laptop is like between these two competing platforms.

Hardware

CPU Information

  • Processor (CPU): Intel Core2Duo CPU P8600 @ 2.40GHz

Memory Information

  • Total Memory (RAM): 4GiB
  • Swap Partition Size: ~6GiB

OS Information

  • OS: Linux 2.6.30.9-102.fc11.x86_64
  • System: Fedora  11
  • Desktop Environment: KDE 4.3.3

Display Info

  • Model: ATI Mobility Radeon HD 4670
  • Driver: 2.1.9026 FireGL

Hard Drive Info

  • Memory: 320GiB 7,200 RPM SATA

Software

I am using Java version 1.6.0 and Mono version 2.4.2.3 for these tests.

Test Setup

For the following tests I have provided source code for both the Java and the C# implementations. I then ran the programs and checked their CPU and memory usage.

Test I

Simple write/read test to the console. CPU usage was recorded before the read.

Java code:

import java.io.IOException;
  
public class TestI {

    public static void main(String[] args) {

        System.out.println("Hello World");

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

C# code:

using System;

namespace TestI
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            try {
                Console.Read();
            } catch(Exception e){
                Console.Write(e.StackTrace);
            }
        }
    }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test II

Simple primitive variable usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestII {
    public static void main(String[] args) {
        byte b = 0;
        short s = 0;
        int i = 0;
        long l = 0;
        float f = 0;
        double d = 0;
        boolean bo = true;
        char c = 'a';
        
        //set them all to something
        b = 1;  
        s = 1;  
        i = 1;  
        l = 1;  
        f = 1;  
        d = 1;  
        bo = false;  
        c = 'b';
        
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

C# code:

using System;

namespace TestII 
{ 
    class MainClass
    {
        public static void Main(string[] args)
        {  
            byte b = 0;  
            short s = 0;  
            int i = 0;  
            long l = 0;  
            float f = 0;  
            double d = 0;  
            bool bo = true;  
            char c = 'a';

            //set them all to something  
            b = 1;  
            s = 1;  
            i = 1;  
            l = 1;  
            f = 1;  
            d = 1;  
            bo = false;  
            c = 'b';

            try {
                Console.Read();
            } catch(Exception e){
                Console.Write(e.StackTrace);
            }              
        } 
    }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.1MiB
CPU Usage: 0% 0%

Test III

Simple primitive variable usage, this time with arrays. Same as above but with arrays of 10,000 for each primitive. The arrays were set using loops similar in structure to the following:

for(int x=0; x<10000; x++) { b[x] = 1; } Results:

Java C#
Memory Usage: 5.9MiB 2.4MiB
CPU Usage: 0% 0%

Test IV

Simple object usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestIV {
    public static void main(String[] args) {
        SimpleClass sc = new SimpleClass();
        
        sc.setID(5);
        sc.setName("Hello World");
        
        System.out.println(sc.getString());

        try {
            System.in.read(); 
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
}

public class SimpleClass {
    
    private int _ID;
    private String _name;

    public SimpleClass()
    {
        //default constructor
    }

    public SimpleClass(int ID, String name) 
    {
        _ID = ID;
        _name = name;
    }

    public int getID()
    {  
        return _ID; 
    }

    public void setID(int ID)  
    {
        _ID = ID; 
    }

    public String getString()  
    { 
        return _name; 
    }

    public void setName(String name) 
    { 
        _name = name;
    }
}

C# code:

using System;  

namespace TestIV  
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            SimpleClass sc = new SimpleClass();

            sc.setID(5);                  
            sc.setName("Hello World");

            Console.WriteLine(sc.getString());

            try {                  
                Console.Read();              
            } catch(Exception e){                  
                Console.Write(e.StackTrace);                  
            }  
        }  
    }  
}

using System;

namespace TestIV  
{
    public class SimpleClass          
    {
        private int _ID;
          
        private String _name;

        public SimpleClass()              
        {              
            //default constructor              
        }

        public SimpleClass(int ID, String name)              
        {              
            _ID = ID;
              
            _name = name;              
        }

        public int getID()              
        {              
            return _ID;              
        }

        public void setID(int ID)              
        {              
            _ID = ID;              
        }

        public String getString()              
        {              
            return _name;              
        }

        public void setName(String name)              
        {              
            _name = name;              
        }
    }      
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test V

Simple object usage, this time with arrays. Same as above but with arrays of 10,000 for each object. The arrays were set using loops similar in structure to test III:

Java code:

import java.io.IOException;

public class TestV {
    public static void main(String[] args) {
        SimpleClass[] sc = new SimpleClass[10000];

        for(int x=0; x<10000; x++) 
        { 
            sc[x] = new SimpleClass(); 
            sc[x].setID(5); 
            sc[x].setName("Hello World");
            System.out.println(sc[x].getString()); 
        } 

        try { 
            System.in.read(); 
        } catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
    } 
}

C# code:

using System;

namespace TestV      
{      
    class MainClass          
    {          
        public static void Main(string[] args)          
        {          
            SimpleClass[] sc = new SimpleClass[10000];

            for(int x=0; x<10000; x++) 
            { 
                sc[x] = new SimpleClass(); 
                sc[x].setID(5); 
                sc[x].setName("Hello World");
                Console.WriteLine(sc[x].getString()); 
            } 

            try { 
                Console.Read(); 
            } catch(Exception e)
            { 
                Console.Write(e.StackTrace); 
            } 
        } 
    } 
}

Results:

Java C#
Memory Usage: 10.1MiB 2.6MiB
CPU Usage: 8% 2%

Test VI

Everyone’s favourite bubble sort with 10,000 integers.

Java code:

import java.io.IOException;

public class TestVI {

    public static void main(String[] args) {

        int[] array = new int[10000];

        //fill array
        for (int i=0;i<array.length;i++) 
        { 
            array[i] = 10000-i; 
        } 
        
        boolean swapped; 

        do { 
            swapped = false; 

            for(int i=0;i<array.length - 1;i++) 
            { 
                if(array[i] > array[i+1])  
                {  
                    int temp = array[i+1];  
                    array[i+1] = array[i];  
                    array[i] = temp;
                    swapped = true;  
                }  
            }
        } while(swapped);

        System.out.println(&#8220;Sorted&#8221;);

        try {  
            System.in.read();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
    }

}   

C# code:

using System;

namespace TestVI  
{      
    class MainClass          
    {
      
        public static void Main(string[] args)          
        {          
            int[] arr = new int[10000];
        
            //fill array
          
            for(int i=0;i<arr.Length;i++) 
            { 
                arr[i] = 10000-i; 
            } 

            bool swapped; 
            
            do { 
                swapped = false; 

                for(int i=0;i<arr.Length - 1;i++) 
                { 
                    if(arr[i] > arr[i+1])         
                    {          
                        int temp = arr[i+1];          
                        arr[i+1] = arr[i];          
                        arr[i] = temp;
                        swapped = true;          
                    }          
                }          
            } while(swapped);

            Console.WriteLine("Sorted"); 

            try {          
                Console.Read();          
            }catch(Exception e){          
                Console.Write(e.StackTrace);          
            }          
        }          
    }  
}

Results:

Java C#
Memory Usage: 6.3MiB 2.2MiB
CPU Usage: 6% 12%

All Results

Java C#
Test I Memory Usage: 5.6MiB 2.2MiB
Test I CPU Usage: 0% 0%
Test II Memory Usage: 5.6MiB 2.1MiB
Test II CPU Usage: 0% 0%
Test III Memory Usage: 5.9MiB 2.4MiB
Test III CPU Usage: 0% 0%
Test IV Memory Usage: 5.6MiB 2.2MiB
Test IV CPU Usage: 0% 0%
Test V Memory Usage: 10.1MiB 2.6MiB
Test V CPU Usage: 8% 2%
Test VI Memory Usage: 6.3MiB 2.2MiB
Test VI CPU Usage: 6% 12%

Conclusions

Well there you have it. It seems that at this point in time Mono is not only mature enough to compete against a seasoned veteran programming language like Java, but in some cases good enough to even supersede it. This is wonderful news for people looking for alternative cross-platform high level languages.