performance with top-level variable and top-function in flutter - android

I am using top-level variable and function for access
to them in all class.(like homeFetch variable in above image)
Do these variables and functions have an impact on performance?

Related

zero value after build in kotlin

in my class extend ConstraintLayout. I have defined val-type variables in the class.
After running the program in the debug mode, I see that all the variables are zero.
Why is this going to happen?
When you define variables with initializers, the Kotlin compiler will generate a constructor which will execute all the initializers in sequence. Before the initializers are executed, the values of the corresponding fields will be 0, and this is the state that is shown on your screenshot.
For values such as IMEOPTIONS_ACTION_DONE, which seem to be constants, you shouldn't use regular class properties. Instead, you should put them into a companion object or on the top level of a file.

Android memory performance

I have two questions regarding Android memory optimization:
Which is more memory expensive in Android, to use a global field or local field?
Dependency injection with dagger- is it better to use objects (services, view models.. ) in an #applicationScope or #activityScope
Local variables are stored on the stack and when the function is finished, the local variables are gone as well. Global variables always exist and use their memory during the life time of the entire program. Its always better to declare a variable nearest to where it's used. So local variables are to be preferred.
About dagger 2 custom scope, the instances scoped in #ApplicationScope lives as long as Application object and #ActivityScope keeps references as long as Activity exists. So the objects should be under the scopes in which it is required. If it is required only in the activity or its hosted fragments use #ActivityScope or if you need the singleton object in an application scope define it #ApplicationScope.
Hope this helped you.

difference between view reference to member variable and local variable

suppose I have an activity, and it contains a TextView. I can initialize the TextView either to a member variable or to a local variable. is there any memory wise difference between these to initialization ?
example :
Activity with local view reference:
public class MainActivity extends Activity{
#OVerride
public void onCreate(Bundle b){
TextView textView = (TextView)findViewById(R.id.my_text_view_id);
}
}
Activity with member view reference:
public class MainActivity extends Activity{
TextView mTextView;
#OVerride
public void onCreate(Bundle b){
mTextView = (TextView)findViewById(R.id.my_text_view_id);
}
}
You should always use the minimal scope. So when you declare a variable you should ask yourself:
"Will I need this variable later in a different function?"
Yes -> Use a member variable
No -> Use a local variable
Edit:
What also to consider is the cost of object creation:
If a function does get called repeatedly it is a good practice to instanatiate an object only once, store it as a member variable and reuse it instead of creating a new instance every time the function gets called.
So the 2nd important question is:
"Will this function get called a lot and do I really need a new instance of the object stored in the variable?"
Yes, often, and no, I can reuse the same object over -> use a member variable. This way the same memory is used and no garbage gets piled up. Use only for large Arrays or objects, it is not needed for simple int vars in loops.
Memory wise global variables are much more prone to memory leaks. The scope any variable depends on its scope. For local variable, the scope is closing braces of the respected method, and variable is automatically garbage collected after the execution of closing braces. Where as global variable will reside in memory until the any object of that class is in memory.
Generally, try to avoid using 'global' variables unless you have good
reason to do .
You can use Local or Global Variable depends upon your Requirement .
Mainly, local variables are that they work within a limited scope means they are declared when a function is called and after ending it the memory taken up by the variable is released.
Finally usage of global variables leads to wastage of memory .

Is there any way to release (clean) static fields

Is there any way to release(clean) static fields ect. Singletons or constant values when exit Android app?
When an Android application exits, all of its data is released. You can release the fields also by simply assiging null to them.
You can assign the fields of types that extend Object to null.
This way, if there are no more references they can be collected by GC.
The primitive types will be released only when their owner class Object is collected.
No I dont think there's any other way except assigning individual variables to null ,release your resources in the onStop() or onPause()

JniHandleOwnership in Mono for Android

Can someone explain to me what the exact meanings of the three JniHandleOwnership enum values in Mono for Android are? What's the difference between them?
My apologies for not updating the class library documentation yet.
The Binding Android Types documentation states what the various JniHandleOwnership values mean in the Wrapping with Java.Lang.Object section.
Update based on comments:
JniHandleOwnership.DoNotTransfer should be used if nothing should be done with the handle parameter. This should always be used within connector methods.
JniHandleOwnership.TransferLocalRef should be used when you have a local reference (e.g. you called JNIEnv.CallObjectMethod(), which returns a local reference) and you want to pass ownership of the local reference to a wrapper.
JniHandleOwnership.TransferGlobalRef should be used when you have a global reference and you want to pass ownership of the global reference to a wrapper:
IntPtr grefFoo = JNIEnv.FindClass("Foo"); // FindClass() returns a gref
var Foo = Java.Lang.Object.GetObject<Java.Lang.Class>(grefFoo, JniHandleOwnership.TransferGlobalRef);

Categories

Resources