Memory problem

First I will explain how I thought memory works in Java and then my problem. Please correct me if I am wrong.

From the docs on the “java” command:

To me, this means that I start out with 2MB of RAM available to my program. If my program requires more that 2MB, then the JVM will add more memory for use in my program, until the 64MB max is reached.(This is all assuming default values).

This line from the JVM spec seems to confirm this:

In a program that I am working, I am trying to open a file for viewing. I am doing some manipulation with the data in a StringBuilder before I view the file. The amount of memory required for the file is 11MB. When I try to run with this file I get an OutOfMemoryError. The only way I can get this file to work is if I set the initial heap size to a value larger than 11MB, ie if I pass -Xms20m gives me enough memory. This seems to contradict the statements above from the spec that the heap is supposed to expand as required.

I tried to search to see if there is a bug, but couldn’t find anything.

Am I totally off base here? Is there something I have to do programatically to access more memory? I would rather not have to pass that memory argument to the JVM.

Thanks.

Tried the pathological test case?


byte[] b = new byte[<some big figure>];

Cas :slight_smile:

I don’t specify any amount of memory, and I’ve never had any problems.

CaptainJester… you understood how memory works. OutOfMemory is thrown after your app exceeds 64MB (on default setup). When you run your application, assuming you use windows, open task manager and see how much javaw.exe has allocated memory. Is your application running ok on default memory setup? My guess is that you have memory leak somewhere. As princec mentioned, this is often the case if you use arrays and forget to nullify them (if you just use “array = new array[]” then the data it held before isn’t freed before allocating new data).

edit: clarified last sentence

Really? That sucks hard… thank you for mentioning it I wouldn’t have think about that.

I thought that you didn’t need to nullify them if the variable goes out of scope. For instance, if you define the array within a loop, at the end of the loop the array will be gone. Nullifying the array is only required when the array is stored in an object that still has a reference to it. Then all the objects in the array will have references to them through the object.

I think he means this:

This piece of code requires 64MB of ram:


byte[] data;
data = new byte[32 * 1024 * 1024];
..
data = new byte[32 * 1024 * 1024];

This piece of code requires 32MB of ram:


byte[] data;
data = new byte[32 * 1024 * 1024];
..
data = null;
data = new byte[32 * 1024 * 1024];

Ok. Thanks guys. After trying the test Cas suggested and had no problem, I checked the memory usage as Kova suggested. For some reason, my usages does jump past 64MB. The input file is only about 1MB and I do some manipulation that temporarily raises memory up to 7MB for the one file, but the total JVM usage hits 100MB. This is unexpected for what I am doing. I will have check to see if I have a runaway memory condition somewhere. I will post back if I can’t find it.

Thanks again.