Java - 超过120个元素的堆空间?

时间:2016-05-14 10:00:00

标签: java memory-management

我有一个奇怪的问题。

我正在编写一些允许操作排列的类,更具体地说:从键盘读取,在屏幕上打印和编写(添加)排列。在执行小程序的过程中,我获得了与使用java.lang.OutOfMemoryError: Java heap space相关的Vector<Integer>,好像我有太多的项目需要添加到Vector。问题是我在S 5 中工作,它有120个元素:小于JVM的最大堆空间。有人知道发生了什么事吗?

在所涉及的代码之下。

public class Sn {

    int n;

    public Sn(int n) {
        this.n = n;
    }
    public Permutation valueOf(String stringInterpretation) {
        String[] support = stringInterpretation.subSequence(1, stringInterpretation.length()-1).toString()  .split("\\)\\(");
        int[][] p = new int[support.length][];
        for(int i=0 ; i<p.length ; i++)
            p[i] = convertStringToCicle(support[i]);
        return new Permutation(p,this);
    }

    public static int[] convertStringToCicle(String string){
        String[] support = string.split(",");
        int[] output = new int[support.length];
        for(int i=0 ; i<output.length ; i++)
            output[i] = Integer.parseInt(support[i]);
        return output;
    }   
    public static void main(String[] args) {
        Sn S_5 = new Sn(5);
        // xample, a=(1352), b=(256), c=(1634), ab=(1356), ac=(1652)(34).
        Permutation     a = S_5.valueOf("(1,3,5,2)"),
                        b = S_5.valueOf("(2,5,6)"),
                        c = S_5.valueOf("(1,6,3,4)");

        System.out.printf(" a = %s \n",  a.convertToString());
        System.out.printf(" b = %s \n",  b.convertToString());
        System.out.printf(" c = %s \n",  c.convertToString());
        System.out.printf("ab = %s \n", a.add(b).convertToString());
        System.out.printf("ac = %s \n", a.add(c).convertToString());

    }
}



public class Permutation {

    public int[][] p;

    public Sn group;


    // ======================================================
        // TODO | Constructors

    public Permutation(Sn group){
        this.group = group;
    }

    public Permutation(int[][] p, Sn group){
        this.p = p;
        this.group = group;
    }

    public Permutation(int n){
        this.group = new Sn(n);
    }   
    public Permutation(int[][] p, int n){
        this.p = p;
        this.group = new Sn(n);
    }

    // ======================================================
        // TODO | Basic management

    /** If {@code this} send the {@code i}-th element into the {@code j}-th element, the method returns {@code j}.
     * 
     * @param i
     * @return
     */
    public int returnPermutatedInput(int i){
        int j;
        for(int cicle=0 ; cicle<p.length ; cicle++)
            for(j=0 ; j<p[cicle].length ; j++)
                if(p[cicle][j]==i)
                    return  p[cicle][(j+1)%p[cicle].length] ;
        return i;
    }

    /** Returns a {@code Vector<Integer>} in which the {@code i}-th element is {@code i}.
     * 
     * @return
     */
    private Vector<Integer> integerVector(){
        Vector<Integer> output = new Vector<Integer>(group.n);
        for(int i=0 ; i<group.n ; i++)  output.add(i);
        return output;
    }

    public Permutation add(Permutation a){
        // Time complexity: n+k         where k is the number of cicles of the output
        Vector<Integer> currentCicle = null,                elements = integerVector();
        Vector<int[]> output = new Vector<int[]>();

        int examinedInput;
        while( elements.size() != 0 )
            if(currentCicle==null)  // If at the previous step we ended a cicle, start a new one
                {
                currentCicle = new Vector<Integer>();   
                currentCicle.add(elements.get(0));
                }
            else                    // If we are already considering a cicle, then proceed
                {
                examinedInput = this.returnPermutatedInput(a.returnPermutatedInput(currentCicle.get(currentCicle.size()-1)));   

                // If the element considered is the first element of the cicle, the cicle is finished
                if( examinedInput == currentCicle.get(0) )      
                    {
                    if( currentCicle.size() != 1 )          output.add(fromIntegerVectorToIntArray(currentCicle));      // A cicle of length 1 is a fixed point
                    currentCicle = null;
                    }

                else                        // ...otherwise add the new element to the cicle
                    {
                    currentCicle.add(examinedInput);
                    elements.remove(examinedInput);     // The current value of examinedInput will not be evalued in future
                    }
                }
        return new Permutation((int[][])output.toArray(), group);
    }

    /** ======================================================
            // TODO | Interface: Printable                                                          */

    public String convertToString(){
        StringBuffer support = new StringBuffer();      
        for(int i=0 ; i<p.length ; i++)
            support.append(convertIntArrayToString(p[i]));      
        return support.toString();
    }
    public static String convertIntArrayToString(int[] cicle){
        StringBuffer support = new StringBuffer("(");
        for(int i=0 ; i<cicle.length-1 ; i++)
            support.append(cicle[i]).append(",");
        support.append(cicle[cicle.length-1]).append(")");
        return support.toString();
    }
}    

编辑:修改代码,现在应该编译。

0 个答案:

没有答案