Need Solution to reduce object creation, used spring - but have a problem , method explained in detail
I am using spring to reduce new objects creation, for the number of resultset available, I will create a row object inside and add the row object in a rowset object, which maintains a list of rows.
My code goes like this.
while(rs.next()) {
Row r = new Row();
//Logic
rowset.addRow(r);
}
My rs will have atleast a minimum of 3000 rows, so there will be a 3000 row object injected, so to avoid it, I used spring injection like
while(rs.next()) {
Row r = (Row)getApplicationContext().getBean("row");
//Logic
rowset.addRow(r);
}
but the problem here is when the second row object is added to the rowset, the first row object's values are overriden with second row object and when last row is added , all the 1 to last-1 row objects values are updated as last row object values.
This is my rowset calss
public class RowSet {
private ArrayList m_alRow;
public RowSet() {
m_alRow = new ArrayList();
}
public void addRow(Row row) {
m_alRow.add(r开发者_开发百科ow);
}
}
I need a solution , such that the new row object creation is avoided inside the while loop with the same logic involved.
Spring injection is not your answer here. You aren't avoiding any of the problems with keeping 3,000 rows in memory. You're chewing up the same RAM whether you call "new" to create them or the bean factory creates.
I'd question why you need all 3,000 rows in memory at once. I can see where might ask for them in smaller bites, or perhaps ask the database server to crunch them into a useable piece of data instead of hauling all that data to the middle tier.
I'd rethink your design.
A RowSet might be a fine abstraction for a persistence tier, but I don't see any business objects here. What are you doing with that data? How is it ultimately consumed?
You should follow Google's example if you simply send it to a user interface for display. Google might get back millions of responses to your query, but it only shows them to you 25 at a time, with the best answers coming up first. They're betting that you won't need the rest of those million results if the first one meets your need. Maybe you need to try that for your problem, too.
If you are looking for 3000 records with 3000 different values then 3000 objects are the only way to go. If you are retrieving data only from one column then this could be mitigated by adding them to a list (Which internally autoboxes to objects for primitives so it also ends up with 3000 objects). Why not try ORM tools like Hibernate and not worry about too much data wiring?
By default Spring returns a single instance. So you are simply fetching the same row over and over. This is why the values are overwritten, you only have 1 instance of Row. If your goal was to create less objects, you have achieved it, of course your logic has suffered for it 8-)
As was mentioned, if you are going to store 3000 unique values, you will need 3000 objects, its that simple. Even what you were attempting to do with Spring was still going to create those objects, only you were making a very simple new statement into an obfuscated call to Spring to do the new for you.
This is a horrible misuse of Spring, it is meant for dependency injection not to simply replace new, you made a very simple and clear statement complex by doing it this way.
PS - You can create a new object each time with Spring, but you have to configure it to do so. I won't elaborate, since this is not the solution to your problem.
精彩评论