Tuesday, September 4, 2012

Eclipse/Java: running out of memory


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
or 

Unable to execute dex: Java heap space Java heap space 


If you need to increase the memory, find the eclipse.ini and change maximum Xmx memory (-Xmx512m):

find /Applications/ide/eclipse/Eclipse.app




open /Applications/ide/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini 





if you use a TextWrangler it will open it by default.


-startup
../../../plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar
--launcher.library
../../../plugins/org.eclipse.equinox.launcher.cocoa.macosx_1.1.1.R36x_v20100810
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
512m
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-XX:MaxPermSize=512m
-Xms40m
-Xmx1024m
-Xdock:icon=../Resources/Eclipse.icns
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts



NOTE:


Before you go and start increasing available memory please take a look at your code and refractor ANY creation of objects from inside loops, or at least heavy objects like database Statement used in the inserts:


Connection connection = MySqlHelpers.makeDbConnection(db, user, password);
Statement statement = connection.createStatement();


Just removing the Statement from the loop stabilized java process "Real Memory" footprint growth at 61 MB. Removing various Strings variables made much smaller difference.

I noticed that few System.out.println(...) does not really increase the memory footprint.



You can check Activity Monitor to see if you have java process that quickly builds up "Real Memory" from original 40 MB to 176 MB as it is the case here. Notice I still have 845 MB of Free memory, yet the process crashes around 177 MB.