can i have an optimal solution for this java code ? Facing Java heap space problem even at 3GB heaps

My desired output is

run:

for this  1
for this  2
for this  3
for this  4
for this  5

for this  1,2
1->2
for this  2,3
2->3
for this  3,4
3->4
for this  4,5
4->5

for this  1,2,3
1->2,3
1,2->3
for this  2,3,4
2->3,4
2,3->4
for this  3,4,5
3->4,5
3,4->5

for this  1,2,3,4
1->2,3,4
1,2->3,4
1,2,3->4
for this  2,3,4,5
2->3,4,5
2,3->4,5
2,3,4->5

for this  1,2,3,4,5
1->2,3,4,5
1,2->3,4,5
1,2,3->4,5
1,2,3,4->5

i had a program in java like this

:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author vaibhav
 */
public class tmep {

    private static String str[] = {"1", "2", "3", "4", "5"};
    private static StringBuffer sbuf = new StringBuffer(50);

    public static void main(String args[]) {
        int count = str.length;
        // sbuf=sbuf.append(".");
        // str="123456789";
        for (int lim = 0; lim < count; lim++) //specifies the limit
        {
            for (int start = 1; start <= (count - lim); start++) // gives the starting point
            {
                for (int i = start; i <= lim + start; i++) //up to limit the words should be printed
                {

                    sbuf = sbuf.append(str[i - 1]).append(",");
                    //System.out.print(str[i - 1] + ",");
                }//for loop 3

                //System.out.print(sbuf);
                sbuf.deleteCharAt(sbuf.length() - 1);// this will remove the , at last position
               System.out.println("for this  "+ sbuf);
                //the module for creating all episode rules
                generateEpiRules(sbuf);

                // System.out.println("");
                sbuf.delete(0, sbuf.length());
                //System.out.print("\t");
            }//for loop 2
            System.out.println("");

        }//for loop 1

    }

    private static void generateEpiRules(StringBuffer sb) {
        String string;
        StringBuffer left=new StringBuffer ();
        StringBuffer right=new StringBuffer ();

        string = sb.toString();
        String st[] = string.split(",");
        for (int i = 1; i < st.length; i++)
        {
            //System.out.print("LHS  ");
            for (int j = 0; j < i; j++)
            {
                left.append(st[j]).append(",");
                //System.out.print(st[j] + ",");
            }
           // System.out.print(" RHS   ");

            for (int k = i; k < st.length; k++)
            {
               
            right.append(st[k]).append(",");
            // System.out.print(st[k] + ",");
            }
            left.deleteCharAt(left.length() - 1);// this will remove the , at last position
            right.deleteCharAt(right.length() - 1);// this will remove the , at last position
            System.out.println(left+"->"+right);
            left.delete(0, left.length());
            right.delete(0, right.length());
        }
    }
}

but if i give a sting length of more size;
the system tends to use RAM too much and after some time an exception comes that java Heapspace out of memory

I increased the heap space to 3 GB also but of no use :wall:

Please tell me the optimal program for this
i'm stucked up :wall:

thanks in advance

How long a string are we talking about?

the input string can be in terms of some thousands e.g 4000