Sunday, September 10, 2006

Tip #1: Java Memory issues.

Hmm. I am going to write some tech bits frequently, which might help others. So, let’s start the first one.

Memory issue:

The most critical problem, most of us might be experiencing is memory leak. Your JVM may brainlessly spit out “OutofMemoryException”, then say “bye” and exit. It’s because your heap memory ( heap -the memory used for storing objects used in the program) might have become full.

So, in order to fix this issue, you have to increase heap space size using the JVM command line arguments –Xms (Initial Heap size) and –Xmx (Maximum Heap size)

Exercise: (If you wish!)

1. Just compile and run the below code. Watch curiously what happens. Then, use the tip presented above and fix the issue.I may get a chocolate for you if you get it running :-)

class memory {

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

int[] a = new int[18000000] ; // Watch here! :-)
System.out.println("Code is running");
}

}

Note:I will post the ans when I post next Tip. :-)

5 comments:

prabs said...

is there any limits on starting and max value for heap??

Manivannan P said...

Yeah, the JVM sets 2 MB as a initial heap mem value and 64 M as max, on Windows/i386.

Ramprasad said...

Cool mani,
You post a lot of tips on Java. Though I don't use Java that much and care about such minor details. Things posted here are amazing. Good going ;-)

I would like to imitate you in some things.. :-D

Manivannan P said...

Ram! Thanks man! Happy to see here? BTW, in which state are you in now? (as per our SW engg life cycle)

prachi, thanks! I will post many such details sooner here.

Ramprasad said...

Mani, I am in the design, coding and unit testing.

It comes back again and again in interation ;-)