alexk-il wrote: > Hi, > > I'm coming to Java from C/C++ so I am a little bit confused about > achieving CPU/Battery/Memory efficiency in Android. > > I wonder, what the preferred way to store temporary variables would be > in case of the onRecieve() method in BroadcastReciever? I only refer > to the variables, which do not persist any data between executions of > the onRecieve method. Let's consider the following example: > > String temp; > temp = Double.toString(foo); > > As far as I can see there are two available options: > 1. Store the "temp" within the onRecieve scope. In general, this would > help C++ optimizers to achieve a better performance by inlinining or > by putting some variables in the stack. However, I'm not sure this is > the case in Java/Dalvik. Additionally, the short life of these > temporary variables may trigger unnecessary Garage collection in Java, > which is costly in terms of the performance/battery. > > 2. An alternative, would be to put the "temp" within the > BroadcastReciever scope. My non-educated :) guess is that this will > have less impact on Garbage Collector, and therefore is better. > > Any help would be greatly appreciated.
If you are referring to a manifest-registered BroadcastReceiver, those objects are created at the time there is a broadcast to handle. Hence, the two scopes you cite are effectively the same. If you are referring to a BroadcastReceiver registered through registerReceiver() (e.g., from an Activity), then you should be choosing your scope more by data visibility more than by GC rules. Bear in mind that broadcast Intents typically cross process boundaries, so the cost of the broadcast itself will swamp most GC costs. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy Android Training in NYC: 4-6 June 2010: http://guruloft.com -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en To unsubscribe, reply using "remove me" as the subject.

