Results 1 to 4 of 4
  1. #1
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default OutOfMemoryError: Java heap space

    I am actually working on a Parser project and I am getting this error, I went through google and tried to find a solution tried to extend the heap memory as advised but it doesn't work it rather says there is no class, I thin k there is a memory leak in my Program.
    This is my code:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;

    /**
    *
    * @author *****
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    // program method reads the program and validates it


    public static String [] ReadFile(String name) throws FileNotFoundException
    {String [] array= new String[100] ;
    for(int k=0; k<array.length;k++)
    {
    array[k]="";


    }

    FileReader fr = new FileReader(name + ".txt");
    Scanner scan = new Scanner(fr);
    int j=0;

    while(scan.hasNextLine())
    { String line=scan.nextLine();
    String[] wordList = line.split("\\s");
    for(int i=0;i<wordList.length;i++)
    { array[j]=wordList[i];
    j++;
    }

    }

    return array;
    }



    public static String program(String [] array) throws FileNotFoundException
    { String newline = System.getProperty("line.separator");
    String type="<program>:=<compound_stmt>";
    type=type+newline+Main.compoundStmt(array,type,0);

    return type;
    }
    // methods describing the compound statement
    public static String compoundStmt(String [] array,String type,int i)
    { String newline = System.getProperty("line.separator");

    if(array[i].equals("BEGIN"))
    {
    type=type+newline+"<compound stmt> ::= BEGIN <stmt list> END"+newline+Main.stmList(array,type,i+1);
    }


    return type;
    }
    // method that truncates last character
    public static final char LF = '\n';
    public static final char CR = '\r';
    public static String chop(String str) {
    if (str == null) {
    return null;
    }
    int strLen = str.length();
    if (strLen < 2) {
    return "";
    }
    if(str.charAt(str.length()-1)==';')
    {

    int lastIdx = strLen - 1;
    String ret = str.substring(0, lastIdx);
    char last = str.charAt(lastIdx);
    if (last == LF) {
    if (ret.charAt(lastIdx - 1) == CR) {
    return ret.substring(0, lastIdx - 1);
    }
    }
    return ret;
    }
    else
    {
    return str;

    }

    }
    public static String stmList(String[] wordList, String type,int i)
    {
    String newline = System.getProperty("line.separator");
    type= type+newline+"<stmt list> ::= <stmt> <more stmt_list>";


    if(wordList[i].equals("READ"))
    {
    type=type+newline+"<stmt> ::= READ <ident>";
    if(wordList[i+1].charAt(wordList[i+1].length()-1)==';')
    {if(Main.checkString(Main.chop(wordList[i+1])).equals("identifier"))
    {
    type=type+newline+"<ident> ::="+wordList[i+1];
    type=type+newline+"<more stmt_list> ::= ; <stmt list>";
    if(!Main.chop(wordList[i+2]).equals("END"))
    {type=type+newline+stmList(wordList,type,i+2);
    }
    else
    {
    type=type+newline+"<stmt list> ::= <null>";
    }

    }
    else
    {
    System.out.println("Parsing not complete /n Parsing halted at"+wordList[1]);
    }
    }
    else
    {
    System.out.println("Parsing not complete /n Parsing halted at"+wordList[1]);
    }
    }
    else if(wordList[i].equals("WRITE"))
    {
    type=type+newline+Main.expression(wordList,i+1,typ e);
    if(!Main.chop(wordList[i]).equals("END"))
    {type=type+newline+stmList(wordList,type,i);
    }
    else
    {
    type=type+newline+"<stmt list> ::= <null>";
    }
    }
    else if(wordList[i].equals("ASSIGN"))
    {
    if(Main.checkString(wordList[i+1]).equals("identifier"))
    { type=type+newline+"<stmt> ::= ASSIGN <ident> <expr>";

    type=type+newline+Main.expression(wordList,i+2,typ e);
    if(!Main.chop(wordList[i]).equals("END"))
    {
    type=type+newline+stmList(wordList,type,i);
    }
    else
    {
    type=type+newline+"<stmt list> ::= <null>";
    }
    }
    }
    else if(wordList[i].equals("IF"))
    {

    type=type+newline+"<stmt> ::= IF <boolean_expr> <stmt>";
    type=type+newline+Main.boolean_expre(wordList, i+1, type);
    type=type+newline+stmList(wordList,type,i);

    }
    else if(wordList[i].equals("WHILE"))
    {
    type=type+newline+"<stmt> ::= WHILE <boolean_expr> <stmt>";
    type=type+newline+ Main.boolean_expre(wordList, i+1, type);
    type=type+newline+ Main.stmList(wordList,type,i);

    }
    else if(wordList[i].equals("SET"))
    {

    if(Main.checkString(wordList[i+1]).equals("identifier")&& wordList[i+2].equals("TO"))
    { type=type+newline+"<stmt> ::= SET <ident> TO <expr>"+newline+"<ident> ::="+wordList[i+1];
    type=type+newline+Main.boolean_expre(wordList, i+2, type);
    if(!Main.chop(wordList[i]).equals("END"))
    {type=type+newline+stmList(wordList,type,i);
    }
    else
    {
    type=type+newline+"<stmt list> ::= <null>";
    }
    }
    }
    else {
    if(wordList[i].equals("BEGIN"))
    { type=type+newline+"<stmt> ::= <compound statement>"+newline+Main.compoundStmt(wordList,typ e,i);
    }
    }




    return type;
    }

    public static String expression(String []wordList,int i, String type)
    { String newline = System.getProperty("line.separator");
    type=type+newline+"<expr> ::= <term> <more expr>";

    type=type+newline+Main.term(wordList, i, type);

    if(!(wordList[i].equals("+")||wordList[i].equals("-")))
    {type=type+newline+"<more expr> ::= <null>";
    }
    else
    {
    type=type+newline+Main.operator(wordList, i, type);
    type=type+newline+Main.expression(wordList, i, type);


    }






    return type;
    }

    public static String term(String[] wordList,int i, String type)
    { String newline = System.getProperty("line.separator");
    type=type+newline+"<term> ::= <factor> <more term>";
    type=type+newline+Main.factor(wordList, i, type);

    if(!(wordList[i].equals("*")||wordList[i].equals("/")))
    {type=type+newline+"<more term> ::= <null>";
    }
    else
    { type=type+newline+Main.p_operator(wordList, i, type);
    type=type+newline+Main.term(wordList, i, type);
    }


    return type;
    }
    public static String operator(String[] wordList,int i, String type)
    { String newline = System.getProperty("line.separator");
    if(Main.checkString(wordList[i]).equals("add")||Main.checkString(wordList[i]).equals("substract"))
    {
    type=type+newline+"<operator> ::="+Main.checkString(wordList[i]);
    i=i+1;
    }


    return type;
    }
    public static String factor(String[] wordList,int i, String type)
    { String newline = System.getProperty("line.separator");
    if(Main.checkString(wordList[i]).equals("identifier"))
    {
    type=type+newline+"<factor> ::= <identifier>"+newline+"<identifier> ::="+wordList[i];
    i=i+1;
    }
    else if(Main.checkString(wordList[i]).equals("integer"))
    {
    type=type+newline+"<factor> ::= <integer>"+newline+"<integer> ::="+wordList[i];
    i=i+1;
    }
    else
    {
    type=type+newline+"<factor> ::= (expr)"+Main.expression(wordList, i, type);
    }


    return type;
    }
    public static String b_operator(String[] wordList,int i, String type)
    { String newline = System.getProperty("line.separator");
    if(Main.checkString(wordList[i]).equals("lthan")||Main.checkString(wordList[i]).equals("gthan")||Main.checkString(wordList[i]).equals("equal"))
    {
    type=type+newline+"<b_operator> ::="+Main.checkString(wordList[i]);
    i=i+1;
    }




    return type;
    }
    public static String p_operator(String[] wordList,int i, String type)
    { String newline = System.getProperty("line.separator");
    if(Main.checkString(wordList[i]).equals("multiply")||Main.checkString(wordList[i]).equals("divide"))
    {
    type=type+newline+"<p_operator> ::="+Main.checkString(wordList[i]);
    i=i+1;
    }


    return type;
    }
    public static String boolean_expre(String[] wordList,int i, String type)
    { String newline = System.getProperty("line.separator");
    type=type+newline+Main.expression(wordList, i, type);
    type=type+newline+Main.b_operator(wordList, i, type);
    type=type+newline+Main.expression(wordList, i, type);
    return type;
    }
    public static String checkString(String word)
    {
    String type="";
    String value="";
    char[] charray=word.toCharArray();

    for(int i=0;i<charray.length;i++)
    {
    if(type.equals("identifier"))
    { if(!value.equals(" "))
    { if (i<charray.length-1 && (Character.isLowerCase(charray[i]) || Character.isDigit(charray[i])))
    {
    value=value+Character.toString(charray[i]);


    }
    else if (i==charray.length-1 &&(Character.isLowerCase(charray[i]) || Character.isDigit(charray[i])))
    {
    value=value+Character.toString(charray[i]);




    }
    else if(!(Character.isLowerCase(charray[i]) || Character.isDigit(charray[i])))
    {
    type=" ";
    }
    }}
    else if(!type.equals("identifier"))
    {

    if(i<charray.length-1 && Character.isLowerCase(charray[i]))
    {
    value=Character.toString(charray[i]);
    type="identifier";

    }
    else if (i==charray.length-1 && Character.isLowerCase(charray[i]))
    { type="identifier";
    value=Character.toString(charray[i]);


    }



    }
    // checks integer
    if (type.equals("integer"))
    {
    if(Character.isDigit(charray[i]))
    { if(i<charray.length-1)
    { value= value+ Character.toString(charray[i]);
    }
    else if(i+1==charray.length)
    {

    }
    }
    else if(!Character.isDigit(charray[i]))
    {
    type=" ";
    }
    }

    else if (!(type.equals("integer")) && !(type.equals("identifier")))
    {
    if(((charray[i] == '+')&& i<(charray.length-1) && Character.isDigit(charray[i+1]))||((charray[i] == '-') && i<(charray.length-1)&& Character.isDigit(charray[i+1])) || (Character.isDigit(charray[i])))
    {

    value=Character.toString(charray[i]);

    type="integer";

    }
    }
    else if (!(type.equals("integer")) && type.equals("identifier"))
    {
    if((charray[i] == '+'&& i<(charray.length-1) && Character.isDigit(charray[i+1]) ) ||(charray[i] == '-' && i<(charray.length-1)&& Character.isDigit(charray[i+1])) )
    {
    value=Character.toString(charray[i]);
    type="integer";
    }
    }

    if(charray[i] == ';')
    {
    type="seperator";


    }


    if(charray[i] == '=')
    {
    type="equal";


    }

    if(charray[i]=='<')
    {
    type="lthan";

    }

    if(charray[i] == '>')
    {
    type="gthan";


    }



    if( (charray[i] =='-'&& i<(charray.length-1) && !Character.isDigit(charray[i+1]))||( charray[i] =='-' && i == charray.length-1))
    {
    type="substract";



    }

    if(( charray[i] =='+' && i<(charray.length-1) && !Character.isDigit(charray[i+1]))||( charray[i] =='+' && i == charray.length-1))
    {
    type="add";



    }


    // check errors
    if(!(charray[i]=='+') && !(charray[i]=='-') && !(Character.isLowerCase(charray[i])) && !(Character.isDigit(charray[i]))&& !(charray[i]=='<')&& !(charray[i]=='>')&& !(charray[i]==';')&& !(charray[i]=='='))
    { System.out.println(charray[i]+"2");
    if( !(i<(charray.length-4) && charray[i]=='B' && charray[i+1]=='E' && charray[i+2]=='G' && charray[i+3]=='I' && charray[i+4]=='N' ))
    {
    if( !(i<(charray.length-2) && charray[i]=='E' && charray[i+1]=='N' && charray[i+2]=='D'))
    {
    if( !(i<(charray.length-2) && charray[i]=='S' && charray[i+1]=='E' && charray[i+2]=='T'))
    {
    if( !(i<(charray.length-4) && charray[i]=='W' && charray[i+1]=='H' && charray[i+2]=='I' && charray[i+3]=='L' && charray[i+4]=='E' ))
    {
    if( !(i<(charray.length-5) && charray[i]=='A' && charray[i+1]=='S' && charray[i+2]=='S' && charray[i+3]=='I' && charray[i+4]=='G' && charray[i+5]=='N'))
    {
    if( !(i<(charray.length-4) && charray[i]=='W' && charray[i+1]=='R' && charray[i+2]=='I' && charray[i+3]=='T' &&charray[i+4]=='E'))
    {
    if( !(i<(charray.length-3) && charray[i]=='R' && charray[i+1]=='E' && charray[i+2]=='A' && charray[i+3]=='D') )
    {
    if(!(i<(charray.length-2) && charray[i]=='I' && charray[i+1]=='F') )
    {

    type="error";
    String resToken="";
    for(int k=i;k<charray.length;k++)
    {
    resToken= resToken+charray[k];
    }

    break;
    }
    }}}}}}}
    } }
    return type;
    }
    public static void writeFile(String name, String type) throws IOException
    {
    FileWriter fw1 = new FileWriter(name+"_parse.txt", true);
    BufferedWriter out1 = new BufferedWriter(fw1);
    out1.write(type);
    }
    public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO code application logic here
    File dataFile1 = new File("test1_parse.txt");
    dataFile1.delete();
    File dataFile2 = new File("test2_parse.txt");
    dataFile2.delete();
    File dataFile3 = new File("test3_parse.txt");
    dataFile3.delete();
    File dataFile4 = new File("test4_parse.txt");
    dataFile4.delete();
    File dataFile5 = new File("test5_parse.txt");
    dataFile5.delete();

    String [] array1 =new String[100];
    array1=Main.ReadFile("test1");
    String [] array2 =new String[100];
    array2=Main.ReadFile("test2");
    String [] array3 =new String[100];
    array3=Main.ReadFile("test3");
    String [] array4 =new String[100];
    array4=Main.ReadFile("test4");
    String [] array5 =new String[100];
    array5=Main.ReadFile("test5");

    String type1=Main.program(array1);

    Main.writeFile("test1",type1);

    String type2=Main.program(array2);

    Main.writeFile("test2",type2);

    String type3=Main.program(array3);

    Main.writeFile("test3",type3);

    String type4=Main.program(array4);

    Main.writeFile("test4",type4);

    String type5=Main.program(array5);

    Main.writeFile("test5",type5);

    }

    }


    Please help me compile it? I apologize for the poor commenting and poor indenting in advance.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    First put your code in code tags. It is not clear :(

    since you write a parser it means there can be a logical error. libs refs may say to me

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    ... you add some files, keep them all in heap and then try to manipulate them?
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    To find out a real pre problem I advise you to put off all your exception try/catch blocks (throws) and then show us your code running console :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Yep.
    Format the code, and post in CODE tags.
    Tell us the exception and stack trace for it.

Similar Threads

  1. java.lang.OutOfMemoryError: Java heap space
    By paul in forum Advanced Java
    Replies: 11
    Last Post: 06-12-2010, 05:30 PM
  2. Java heap space OutofMemoryError
    By everlast88az in forum Advanced Java
    Replies: 3
    Last Post: 05-18-2009, 08:12 AM
  3. [SOLVED] Java heap space OutOfMemoryError
    By loki in forum New To Java
    Replies: 14
    Last Post: 04-25-2009, 04:11 PM
  4. java.lang.OutOfMemoryError: Java heap space
    By vidjogamer in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 06:52 AM
  5. OutOfMemoryError: heap space (SJSAS PE 8.2 u4)
    By mikamj in forum Advanced Java
    Replies: 0
    Last Post: 11-21-2008, 07:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •