Question about Java Garbage Collection

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

kmccuskerAsked:
Who is Participating?
 
sciuriwareConnect With a Mentor Commented:
>>>
      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
 
CEHJCommented:
>> 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
 
kmccuskerAuthor Commented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
CEHJCommented:
>> PLease clarify your comment.

Why would you instantiate that for every row of the results, passing the whole ResultSet?
0
 
kmccuskerAuthor Commented:
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
 
CEHJCommented:
>>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
 
CEHJConnect With a Mentor Commented:
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
 
kmccuskerAuthor Commented:
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
 
CEHJCommented:
>> 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
 
sciuriwareCommented:
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
 
sciuriwareCommented:
Correction:
     -XmX512M

;JOOP!
0
 
kmccuskerAuthor Commented:
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
 
kmccuskerAuthor Commented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
:-)

If you're collecting rows in the object you mention, in the manner you outline, there won't be any garbage collection
0
 
sciuriwareCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.