Results 1 to 2 of 2
  1. #1
    kathir0301 is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default java.lang.OutOfMemoryError..Help me

    HI
    I got the following exception when i tried to run the program..

    public class Test {
    public static void main(String[] args)throws Exception {

    String rootPath = "C:/Documents and Settings/sample ArrayList<String> fileList = new ArrayList<String>();
    sample ob=new sample();
    fileList = ob.findAllfiles(rootPath);
    System.out.println(fileList);
    }
    }
    class sample
    {
    ArrayList<String> allFiles = new ArrayList<String>();

    public ArrayList<String> findAllfiles(String rootPath)throws Exception
    {

    File dir = new File(rootPath);
    if (!dir.isDirectory()) // check if it is a file
    {
    allFiles.add(rootPath);

    }
    if (dir.isDirectory() ) { // check if it is a directory
    String files[] = dir.list();
    for (int j = 0; j < files.length; j++)
    {
    allFiles.addAll(findAllfiles(rootPath + File.separator + files[j]));
    }
    }

    return allFiles;
    }
    }



    and the exception which i got is


    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.lang.AbstractStringBuilder.expandCapacity(Unk nown Source)
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at java.util.AbstractCollection.toString(Unknown Source)
    at java.lang.String.valueOf(Unknown Source)
    at java.io.PrintStream.println(Unknown Source)
    at Task.zip.main


    pls help me to solve the problem...thanks in advance

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Spot the difference:

    Java Code:
    //allFiles.addAll(findAllfiles(rootPath + File.separator + files[j]));
    findAllfiles(rootPath + File.separator + files[j]);

    "allFiles.addAll(findAllFiles(..." will be continually adding the elements of allFiles, as returned by findAllFiles(), to itself. That way lies memory exhaustion.
    Last edited by pbrockway2; 11-22-2010 at 08:52 AM.

Similar Threads

  1. Replies: 0
    Last Post: 04-11-2010, 11:14 PM
  2. Ant fileset java.lang.OutOfMemoryError
    By 1inmillion in forum Advanced Java
    Replies: 0
    Last Post: 12-23-2008, 04:27 PM
  3. java.lang.OutOfMemoryError
    By new_2_java in forum New To Java
    Replies: 2
    Last Post: 11-01-2008, 05:12 AM
  4. java.lang.OutOfMemoryError
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 12:21 PM
  5. java.lang.OutOfMemoryError in a web service client
    By elchape in forum Advanced Java
    Replies: 4
    Last Post: 06-28-2008, 05:21 PM

Posting Permissions

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