[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Question about Java Garbage Collection

Posted on 2007-07-26
16
Medium Priority
?
223 Views
Last Modified: 2013-11-23
I have a program that processes a java object using a resultset and then inserts the object into a table.  The follwoing is a snippet of my code

            while (queryResults.next()) {
                HistorySalesData hsrow = new HistorySalesData(queryResults);
               .
               . processing the results
               .  
            }

My question concerns the new instance of HIstorySalesData at the top of my loop.  If I have a result set of 10,000 rows, the HistorySalesData objaect will get instanciated 10,000 times.  Will the JVM garbage collection take care of freeing the memeory allocated for this class.  If not, is there a programming solution to release the memory?

Thanks
Kevin

0
Comment
Question by:kmccusker
  • 7
  • 5
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19576902
>> If I have a result set of 10,000 rows, the HistorySalesData objaect will get instanciated 10,000 times.

Yes - now why would you want that to happen?
0
 

Author Comment

by:kmccusker
ID: 19576953
My questions are.....

Will the JVM garbage collection take care of freeing the memeory allocated for this class?  If not, is there a programming solution to release the memory?  PLease clarify your comment.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19577000
>> PLease clarify your comment.

Why would you instantiate that for every row of the results, passing the whole ResultSet?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:kmccusker
ID: 19577196
I assume you want to turn this into a programming technique question.  Let me ask the question since you are trying to suggest a better way.  I have enter this under the "new to java category".  Is there a better way of doing this?  Why don't you suggest another method instead of trying to make the person admit they are new to the language.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19577270
>>instead of trying to make the person admit they are new to the language.

I'm not trying to make you admit anything. I'm just trying to find out what you're trying to do. Obviously i've some idea, since you mention it in your question.

Generally speaking you need to construct a TableModel. The question of garbage collection actually shouldn't enter into it. This should be the pattern

Vector allData = new Vector();
while(queryResults.nexy()) {
    Vector hsrow = new Vector();
    // fill it, then
    allData.add(hsrow);
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
ID: 19577302
Full example:

      public TableModel resultSetToTableModel(ResultSet rs) {
            try {
                  ResultSetMetaData metaData = rs.getMetaData();
                  int numberOfColumns = metaData.getColumnCount();
                  Vector columnNames = new Vector();
                  // Get the column names
                  for (int column = 0; column < numberOfColumns; column++) {
                        columnNames.addElement(metaData.getColumnLabel(column + 1));
                  }
                  // Get all rows.
                  Vector rows = new Vector();
                  while (rs.next()) {
                        Vector newRow = new Vector();
                        for (int i = 1; i <= numberOfColumns; i++) {
                              newRow.addElement(rs.getObject(i));
                        }
                        rows.addElement(newRow);
                  }
                  return new DefaultTableModel(rows, columnNames);
            }
            catch (Exception e) {
                  e.printStackTrace();
                  return null;
            }
      }
0
 

Author Comment

by:kmccusker
ID: 19577407
Thanks,

  If you use the table model and the table is large 50 columns x 10,000 rows or more.  Should you be concerned with the amount of memory the program is going to use?  I thought processing one row at a time and then writing it back to the table will limit memory problems.  The downside will be with performance on the amount of IO used to write to the table.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19577469
>> I thought processing one row at a time and then writing it back to the table will limit memory problems.

What kind of 'table' do you mean?  My examples centre on a JTable
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 19577480
In short: the Garbage Collector will free all memory that you release.

Less short: if you are able to do your work in memory and it is not too much,
then you will speed up things.
But if you use more than 64Mb, you got to tell JAVA that you're going to,
so start the program with a first argument like      -Xm512M
to get allowance for 512 Mb to use at once.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 19577492
Correction:
     -XmX512M

;JOOP!
0
 

Author Comment

by:kmccusker
ID: 19577704
For CEHJ  

This is a  commissions table using JDBC ODBC as the access method.  We  create a table from a foxpro Database runing our POS (point of Sales) system.  Basically we combine 4 to 5 tables into a commissions table running on running MS-ACCEES.    The data is updated, added or changed based upon some rules.  The original system was written in ASP pages using vbscript and javascript.  We ran into memory problems using with IIS.  I have to bit the bullet and rewrite the programs in JAVA.   The ammount of rows in my main table are increasing.  I am going from 5000 inserts a month to 10,000 to 40,000 a month.   We will be going to SQL server next year.     I decided to us Java classes as data objects to represent my commissions table.
0
 

Author Comment

by:kmccusker
ID: 19577738
for Sciuriware.
>> In short: the Garbage Collector will free all memory that you release.

      while (queryResults.next()) {
                HistorySalesData hsrow = new HistorySalesData(queryResults);
               .
               . processing the results
               .  
            }

Do I have to manually free up the instance of  HistorySalesData each time the loop completes?

If so, How would I do it?

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19577909
>>I decided to us Java classes as data objects to represent my commissions table.

I wouldn't - it won't scale well and it won't be portable. Use generic JDBC and SQL on a db from which your can export
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 1200 total points
ID: 19580226
>>>
      while (queryResults.next()) {
                HistorySalesData hsrow = new HistorySalesData(queryResults);
               .
               . processing the results
               .  
            }
<<<

That's OK. Let JAVA detect that you released the old objects.
That works like a charm (unless the released objects were referenced elsewhere and not
   released, but that is not visible in your source).

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19580994
:-)

If you're collecting rows in the object you mention, in the manner you outline, there won't be any garbage collection
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 19583681
:)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question