1

I am not sure about one particular android optimization tip, that suggests to avoid unnecesary object creations. I'm unsure about thet "creation" part. In my application i started to assign several objects (context, resources etc) to activity fields with the intention to avoid calling the same get functions (getBaseContext(), getResources()) multiple times in each lifecycle.

So my question would be, when i assign those objects to activity fields, do i create new objects (and use extra space) or am i making a new reference to already created object?

1 Answer 1

3

When calling getBaseContext(), getResouces() you are not creating any new objects. You are obtaining objects that the Android OS has created whenever your application's process is first created.

And in regards to watching how many objects you create, I wouldn't worry about that at all unless you are creating a huge amount of objects (and I mean magnitude orders above 100s). 100s may even be too low.

A good practice would be too always keep your heap size in mind, if it you seeing it growing larger as you build your application, do your best to manage it. You can find out information about your heap size by looking at the DDMS (Dalvik Debug Monitor Server) view in your IDE.

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

2 Comments

Thanks for answer. A quick followup question: is it worth using extra fields for context, resources to use them as function parameters with the intention to avoid calling the same get functions multiple times?
Not really. I mean you avoid the system overhead of putting another function on the stack but that gain is probably very minimal at best. But, if your already doing it, I would say just leave it.

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.