Results 1 to 2 of 2
- 11-22-2010, 07:47 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
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
- 11-22-2010, 08:44 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
java.lang.OutOfMemoryError: unable to create new native thread
By BigBear in forum Java ServletReplies: 0Last Post: 04-11-2010, 11:14 PM -
Ant fileset java.lang.OutOfMemoryError
By 1inmillion in forum Advanced JavaReplies: 0Last Post: 12-23-2008, 04:27 PM -
java.lang.OutOfMemoryError
By new_2_java in forum New To JavaReplies: 2Last Post: 11-01-2008, 05:12 AM -
java.lang.OutOfMemoryError
By vidhya.sk in forum New To JavaReplies: 2Last Post: 09-18-2008, 12:21 PM -
java.lang.OutOfMemoryError in a web service client
By elchape in forum Advanced JavaReplies: 4Last Post: 06-28-2008, 05:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks