Link to home
Start Free TrialLog in
Avatar of kmccusker
kmccusker

asked on

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>> 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?
Avatar of kmccusker
kmccusker

ASKER

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.
>> PLease clarify your comment.

Why would you instantiate that for every row of the results, passing the whole ResultSet?
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.
>>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);
}
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.  
>> 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
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!
Correction:
     -XmX512M

;JOOP!
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.
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
>>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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)

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