6 Replies Latest reply: Sep 21, 2013 5:08 AM by User_Sep RSS

    Increasing Heap Space programatically


      Dear Experts,


      Am using JDev In my project am running a java class (testproduct.class) with main(), to generate excel file from Oracle Database.


      Am passing a Query in my java code as shown below,


      select * from testproduct where rownum<=100000;


      But, it shows.


      java.lang.OutOfMemoryError: Java heap space


      How to increase the heap space through program. (i.e) How to increase my heapspace in (testproduct.class) code



        • 1. Re: Increasing Heap Space programatically

          heap space is reserved when JVM is instantiated so you can't extend this programmatically.

          You can add -xms and -xmx parameters to command line when your application starts.



          • 2. Re: Increasing Heap Space programatically

            Thanks kdario,


            Is it possible free the memory space programatically.

            • 4. Re: Increasing Heap Space programatically
              Timo Hahn

              Garbage collection won't help here. You have to optimize your design to make this work. As long as you read 100000 rows of data into memory without managing this correctly no garbage collector will ever free one byte of memory.


              Do you use jdbc directly or adf bc to get the data? 

              Please post some of your code.

              In sdf you need to optimize the vo you use to read the data by setting the read more to range paging.  There's more to do but as I don't know which technology you use, I can't say exactly what.



              • 5. Re: Increasing Heap Space programatically

                Thanks Timo,

                I use JDBC in my code. My code is,

                public class Excel{



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





                        try {

                            Excel cexl = new Excel();

                            cexl.Pass_Params(args[0], args[1], args[2],args[3], args[4]).get("Query");

                        } catch (OutOfMemoryError e) {

                            // TODO: Add catch code






                    public static HashMap Pass_Params(

                            String url, String user, String password, String slno, String path) throws Exception{

                        String val="Error";      

                        try {

                            Connection connection = null;


                            connection = DriverManager.getConnection(url, user, password);

                            Statement s1 = connection.createStatement();

                            ResultSet rs1 =

                                s1.executeQuery("SELECT QRY FROM TEST1 WHERE SNO=" + slno);        

                            String p = null;

                            String filename = path;

                            if (rs1.next()) {

                                p = rs1.getString(1);


                            ResultSet result = s1.executeQuery(p);

                            HSSFWorkbook hwb = new HSSFWorkbook();

                            HSSFSheet sheet = hwb.createSheet("new sheet");

                            ResultSetMetaData rsmd = result.getMetaData();

                            int cols = rsmd.getColumnCount();

                            int vals = 0;

                            HSSFRow rowhead = sheet.createRow(0);

                            for (int i = 1; i <= cols; i++) {

                                rowhead.createCell((int)i -



                            HSSFRow row;

                            while (result.next()) {

                                vals = vals + 1;

                                row = sheet.createRow(vals);

                                for (int i = 1; i <= cols; i++) {

                                    row.createCell((int)i -




                            FileOutputStream fileOut = new FileOutputStream(filename);         




                            System.out.println("Your excel file has been generated!");

                            Process proc =

                                Runtime.getRuntime().exec(" cmd /c start " + filename );



                            HashMap map = new HashMap();


                            return map;

                        } catch (Exception ex) {

                            // TODO: Add catch code

                            val=new String(ex.getMessage());


                        finally {





                        return null;





                • 6. Re: Increasing Heap Space programatically

                  You will need to identify where is exactly OutOfMemoryError thrown(on result.next(), sheet.createRow(vals) or on row.createCell() ).

                  If your ResultSet raises exception(for example, on result.next()) then you can try to optimize cursor with:

                  Statement s1 = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);


                  If error is thrown by Apache POI, then you will need to use streaming api(SXSSF):

                  The New Halloween Document