2

I have an object which will be constructed from fields in an SQLite database cursor.

Methods in the object will be used to then test against an object of the same type and return a score. There will be many of these tests executed and I'd like to reuse this object - just re-initialising it from the database each time it's needed. I'm wanting to do this to avoid allocating an excessive amount of memory.

Is there a way to reuse the constructor to rebuild the object each time without creating a new one?

Or do I need to create a public method to populate the fields from a cursor and then call that in the constructor?

thanks, m

2
  • Use the singleton design pattern. Commented Oct 11, 2011 at 9:53
  • If you have many similar objects of same type you can use the Flyweight design pattern. Commented Oct 11, 2011 at 10:00

1 Answer 1

1

You can't use the constructor - that is only used when creating new objects. So you'd need to write a method, as per the end of your answer.

However, I would question whether you really need to do this - how often are you querying the database, and have you actually established that this is a performance bottleneck? It may be (I'm aware of the constraints of mobile development) but it will also make the code less elegant and could easily lead to subtle bugs... at the very least, you should measure performance before and after the change to see what the improvement is.

Sign up to request clarification or add additional context in comments.

9 Comments

Hi Jon, thanks for your answer. At present I have this object holding a number of primative types and Strings which are populated by user entered criteria that builds up a search. Then the getter methods are used by another class to compare the search items against the relative fields in the database. It runs fairly quickly. as it is. I was thinking of moving the scoring methods into the class definition of this particular method and having the method accept an object of the same type and then producing a score based on how similar the objects are.
Are you suggesting i should just make new objects and let GC get rid of them as needed?
@mAndroid: Yes - until you've found that the GC isn't performing well enough for you, you should write the clearest, most maintainable code which gets the job done.
I just read here developer.android.com/guide/practices/design/performance.html that you should not allocate memory unnecessarily and it seems in this case to be so. - it does advise bench marking as you have suggested too.
@mAndroid: Yes - in particular, it states: "This document is about Android-specific micro-optimization, so it assumes that you've already used profiling to work out exactly what code needs to be optimized, and that you already have a way to measure the effect (good or bad) of any changes you make." How many objects are you likely to create?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.