Java program consumes too much memory

Hi i'm relatively new to java programming. The following program i've written seems to be taking a lot of memory(around 240 MB, is it normal? - I don't think so!) Please suggest some ways to optimise this program so to reduce memory storage.

Program -

 You have 2 sorted array suppose arr1[4] and arr2[3]
          now we have to combinely sort them using very little extra space(i.e. in-place sort)
          ex..arr1[4]={5,8,11,14,16}
          arr[2]={1,7,10}
          then result should be--in arr1[4]={1,5,7,8,10}
          in arr2[3]={11,14,16}

Code -

import java.util.*;

class sort_in_place
{
    public static void main(String args[])
    {
        // input 2 sorted arrays
        int []a = {5,8,11,14,16};
        int []b = {1,7,10};

        System.out.println("initial array: a = "+Arrays.toString(a)+"   b = "+Arrays.toString(b)+"\n");

        // use extra space to store the array 'a' elements.
        int key = 0, i=0; 

        for( i=0; i<a.length ; i++)
        {
             key = a[i];         // store value in temporary variable key.

            // if key is greater than 1st element of array 'b' then exchange their values and sort array 'b'
            if(key > b[0])
            {
                int j = 1;
                a[i] = b[0];
                while(j<b.length && key > b[j])
                {
                    b[j-1] = b[j];
                    j++;
                }
                b[j-1] = key;
            }
        }
        System.out.println("final array: a = "+Arrays.toString(a)+"   b = "+Arrays.toString(b)+"\n");
    }
}

Answers


You need to remember that a Java program cannot run on its own, but need the Java Virtual Machine platform to handle memory, byte code execution and other tasks. The Oracle JVM uses quite a bit of memory to speed up execution so even trivially small programs come with a large start-up penalty.

This is why small utilities are rarely written in Java.

You need to measure inside the JVM to determine if you use too much memory. Use a profiler for that. JVisualVM in the JDK is a good, free starting profiler.


Even to run a simple Helloworld program java will be needing 300+ classes to be loaded. What I mean here is Java intializing needs memory. Java has Xmx and Xms options for defining the memory usage. For more details go through Java documentation


24 MB is normal for a jvm process.


Your program uses far less memory than the 240 MB ideone reports. I just ran it on my notebook, inserted System.in.read(); at the end of main() to delay termination, and looked at the corresponding process with the windows task manager. Reported peak memory use was 9 MB ...

Most of that will be the infrastructure of the JVM (such as the just in time compiler), and completely unrelated to your program.


Need Your Help

GNU screen custom key bindings format

key-bindings gnu-screen screenrc

I am trying to add key bindings to the .screenrc file for making a lot of things easier. So far I have understood the following pattern

Freeing the allocated memory of a zero-length array in C

c arrays memory-management struct free

I learned a new trick today, consisting of ending a struct with a zero-length array to allow that array to be dynamically sized as I need it. This is extremely handy and helps save a good amount of