Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:12:48+00:00 2026-06-01T09:12:48+00:00

Put value as a field makes sense only if this one represents some object’s

  • 0

Put value as a field makes sense only if this one represents some object’s state.

I wonder if this “rule” should be avoid in certain case.

Here an example, suppose this class :

public class DbCar {

    private ResultSet rs;

    public DbMapper(ResultSet rs) {
        this.rs = rs;
    }

    public Car buildObject(){
        //.....does some mappings, then returns the builded car ....
    }
}

So we see that ResultSet is stored as a member variable and it makes sense since every DbMapper like DbCar manipulates a retrieved ResultSet from JDBC query.

We would have a caller looking like as follows:

while (rs.next()) {
   items.add(new DbCar(rs)).buildObject();
}

But imagine that the current query returned 15000 records.

To put it in a nutshell => 15000 instances of DbCar objects were created.

So my question is : Is the garbage collector efficient enough so that I shouldn’t worry about this huge number of instances ?

Of course, to avoid all these instances, we can refactor the code as follows:

public class DbCar {

    public Car buildObject(ResultSet rs) {
        //.....does some mappings, then returns the builded car ....
    }
}

In this case, a single instance of DbCar (in the current thread and in the current method) would be created, and the caller looking like as follows:

DbCar dbCar = new DbCar();
while (rs.next()) {
   items.add(dbCar.buildObject(rs));
} 

So, which solution should I choose ? Trust on garbage collector with a more elegant code or coding like a more procedural coding-style with local parameter method ?

In order to make choice harder, imagine that the DbCar class divides its “build” method into elegant small methods, each one dedicated to a specific responsibility like for instance :

“buildEngine”, “buildDoors” etc… In case of local parameter choice, I would have to pass ResultSet into all these methods… boring and redondant isn’t it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:12:49+00:00Added an answer on June 1, 2026 at 9:12 am

    That does not constitute a problem for the GC but it all boils down to your application requirements.

    In a previous project, I was involved in developing a very big “near real time” application that runs on a Solaris server (requires 10GB of RAM to start) the application is creating something like 150000 DTO objects every 4 seconds or so. This has no impact on the GC at first glance but after some working hours, the users started complaining about the software loosing data coming out of the hardware. We spent a long night investigating the problem and we finally found out that the GC was taking full CPU to clean up the unused objects which made the application look like it hanged for a second or so (trust me, a second of data loss costs more than 1000$)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to put a value into a DATE field which is invalid,
I'm trying to hash the value of the password input field and put the
The value of ig is being transfered fine, but I need to put this
I wonder whether someone may be able to help me please. I've put some
In one of my views I got an INPUT field where I put my
Here is the way I can put float value to the stack(in C#): ILGenerator
If I put a DateTime value into an Excel cell using Range.set_value through .Net
When I put a string value into an onclick the console returns 'Uncaught SyntaxError:
I have a drop down list and I want the selected value to put
I have an ajax call. I put the return value (data) in a variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.