Click to See Complete Forum and Search --> : java.lang.OutOfMemoryError


sneakyimp
03-02-2009, 09:45 PM
I have an application I've been working on that does some recursive directory searching and then performs a diff on various files. In the process of trying to calculate the diff of a large file, I run out of memory.

I'm wondering some things:

1) Do I need to destroy vars or set them to null in my code to try and preserve memory? Generally speaking, I try to declare variables as locally as possible and I try to avoid excessive use of global vars.

2) I noticed that when my directory listing routine is complete that it appears to have a lock on certain files -- i can't delete them until I have closed my Java app, for instance. Is there some way to get my code to let go of these files? For instance, if I create an instance of a class that has one private BufferedReader var, do I need to set the BR var to null when I'm done with the object? Note that this class instance is a var internally declared inside a static function.

3) Given that this exception occurred when a large file was being processed, is there a way to allocate more memory in general for my java app?

Any help would be much appreciated.

chazzy
03-03-2009, 05:20 AM
generally speaking, recursive methods are high on memory consumption since every instance of the method stays on the stack until the parent thread is destroyed.

sneakyimp
03-03-2009, 11:37 AM
I don't mean to sound ungrateful for your post, but you haven't answered any of the questions about how to remedy the situation.

I have learned elsewhere that you can specify how much memory to allocate for the interpreter via CLI by using the flag -Xmx <size>

For my IDE, this is how you do it:
Yeah, I use JCreator. Once you have all your .java files in a project, then Go to the
Project menu and choose project settings. There, you choose the JDK Tools tab. In
the Select Tool Type pull-down, choose Run Application. If you are going to change
the default then highlight it and click on Edit. Click on the Parameters tab. In the
Parameters textbox enter the Xmx and Xms parameters. If you click on the Right Arrow
and choose JAVA, you can click on them and JCreator will add them in for you.

Also, while you are there choose 'Show additional run_time info'. And while you are
changing you're Run Application, choose 'Show commandline'. Then you can always see
how much memory you are allocating and so forth.

chazzy
03-03-2009, 12:11 PM
1) Do I need to destroy vars or set them to null in my code to try and preserve memory? Generally speaking, I try to declare variables as locally as possible and I try to avoid excessive use of global vars. Java has no concept of "destroy" nor does it have a concept of a "var." Setting a pointer to null like this:


Object o = new Object();
o = null;
Doesn't destroy the "new Object()" that was created until the GC comes through and decides that it's ready to clean up the objects.

2) I noticed that when my directory listing routine is complete that it appears to have a lock on certain files -- i can't delete them until I have closed my Java app, for instance. Is there some way to get my code to let go of these files? For instance, if I create an instance of a class that has one private BufferedReader var, do I need to set the BR var to null when I'm done with the object? Note that this class instance is a var internally declared inside a static function. No, but if you are opening readers you should be calling the "close()" method when done.

3) Given that this exception occurred when a large file was being processed, is there a way to allocate more memory in general for my java app? -Xmx sets the max amount of memory java can consume. You may also want to check on PermGen space. The exact use of -Xmx is "-Xmx=###(m|k)" no spaces.

Any help would be much appreciated.

sneakyimp
03-03-2009, 12:33 PM
Thanks!