I have created a custom keyboard app My Input service looks like this Softkeyboard.java , My app is consuming high RAM around 150-200 MB,when I tried to dump memory info from
adb shell dumpsys meminfo
this is what I get even when my keyboard is not opened.
Objects
Views: 364 ViewRootImpl: 1
AppContexts: 3 Activities: 0
Assets: 5 AssetManagers: 5
Local Binders: 45 Proxy Binders: 32
Parcel memory: 16 Parcel count: 64
Death Recipients: 4 OpenSSL Sockets: 0
Views: 364 //number increases when I open and close keyboard for few times.
My main methods from keyboard class :
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public View onCreateInputView() {
/*abc logo call back and volley request*/
// abcLogoManager.getInstance().abcLogoCallBack(this, this);
// abcLogoModel = new abcLogoModel();
// abcLogoManager.getInstance().getabcLogo();
BaseActivity.printLog(TAG, "onCreateInputView invoked!");
LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
isCapsOnOff = true;
themevalue = appSession.getThemeValue();
this.onChangeKeyBoardTheme();
RelativeLayout abcKeyboardContainer;
if (themevalue == 1) {
abcKeyboardContainer = (RelativeLayout) inflater.inflate(R.layout.abc_keyboard_container, null, false);
if (isNumericKeyboard) {
mInputView = (KeyboardView) inflater.inflate(R.layout.input_numeric_light_gray, null, false);
} else {
mInputView = (KeyboardView) inflater.inflate(R.layout.input, null, false);
}
} else {
abcKeyboardContainer = (RelativeLayout) inflater.inflate(R.layout.abc_keyboard_container_grey, null);
if (isNumericKeyboard) {
mInputView = (KeyboardView) inflater.inflate(R.layout.input_numeric_drak_gray, null);
} else {
mInputView = (KeyboardView) inflater.inflate(R.layout.inputgrey, null);
}
}
abcKeyboardContainerLayout = (abcKeyboardContainerLayout) abcKeyboardContainer.findViewById(R.id.abcKeyboardContainerLayout);
this.mInputView.setKeyboard(mQwertyKeyboard);
this.mInputView.setOnKeyboardActionListener(this);
if (getBeforeCursorText() != null && getBeforeCursorText().length() > 1 && doubleTapOnCaps.equals("")) {
this.mInputView.setShifted(false);
mQwertyKeyboard.setShiftOnOff(getResources(), mInputView.isShifted(), doubleTapOnCaps, themevalue);
} else {
doubleTapOnCaps = "";
this.mInputView.setShifted(true);
mQwertyKeyboard.setShiftOnOff(getResources(), mInputView.isShifted(), doubleTapOnCaps, themevalue);
}
tabsList = new ArrayList<>();
tabsList.addAll(getDefaultTabs());
ivLogoabc = (ImageView) abcKeyboardContainer.findViewById(R.id.ivLogoabc);
/*view find from tabs hidden relative layout*/
rlHiddenTabs = (RelativeLayout) abcKeyboardContainer.findViewById(R.id.rlHiddenTabs);
ibFourDots = (ImageButton) rlHiddenTabs.findViewById(R.id.ibFourDots);
textViewSuggestion1 = (abcTextView) rlHiddenTabs.findViewById(R.id.textViewSuggestion1);
textViewSuggestion2 = (abcTextView) rlHiddenTabs.findViewById(R.id.textViewSuggestion2);
textViewSuggestion3 = (abcTextView) rlHiddenTabs.findViewById(R.id.textViewSuggestion3);
view1 = rlHiddenTabs.findViewById(R.id.view1);
view2 = rlHiddenTabs.findViewById(R.id.view2);
imageViewSmileyToKeyboard = (ImageView) rlHiddenTabs.findViewById(R.id.imageViewSmileyToKeyboard);
/*view find from tabs shown realtive layout */
rlShownTabs = (RelativeLayout) abcKeyboardContainer.findViewById((R.id.rlShownTabs));
mImageViewKeyboard = (ImageButton) rlShownTabs.findViewById(R.id.imageButtonKeyboard);
horizontalScrollView = (CenterLockHorizontalScrollview) rlShownTabs.findViewById(R.id.hscroll);
LinearLayout lnrLytTabs = (LinearLayout) rlShownTabs.findViewById(R.id.keyboard_tab_layout);
rlShownTabs.setVisibility(View.GONE);
/*Click on Four Dots*/
ibFourDots.setOnClickListener(this);
imageViewSmileyToKeyboard.setOnClickListener(this);
mImageViewKeyboard.setOnClickListener(this);
ivLogoabc.setOnClickListener(this);
for (int i = 0; i < tabsList.size(); i++) {
RelativeLayout rlTabs = new RelativeLayout(this);
rlTabs.setLayoutParams(new ActionBar.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
int tabMargin = (int) getResources().getDimension(R.dimen.height_10);
TextView tvTab = new TextView(this);
abcConstants.setTvFont(this, tvTab);
tvTab.setAllCaps(true);
tvTab.setTypeface(tvTab.getTypeface(), Typeface.BOLD);
tvTab.setTextSize(14);
ImageView ivLogoSetting = new ImageView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rlTabs.getLayoutParams().WRAP_CONTENT, rlTabs.getLayoutParams().MATCH_PARENT);
layoutParams.setMargins(tabMargin, 0, tabMargin, 0);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tvTab.setLayoutParams(layoutParams);
if (themevalue == 1) {
tvTab.setTextColor(getResources().getColor(R.color.textColor));
rlTabs.setBackgroundColor(getResources().getColor(R.color.background_popup));
} else {
tvTab.setTextColor(getResources().getColor(android.R.color.white));
rlTabs.setBackgroundColor(getResources().getColor(R.color.grey_bgColor_grey_theme));
}
tvTab.setId(tabsList.get(i).getId());
if (tabsList.get(i).getTabName().equals("SETTING")) {
tvTab.setText("");
if (themevalue == 1) {
tvTab.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_addmoretab, 0, 0, 0);
} else {
tvTab.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_addmoretab_graytheme, 0, 0, 0);
}
} else {
tvTab.setText(tabsList.get(i).getTabName());
}
tvTab.setGravity(Gravity.CENTER);
tvTab.setOnClickListener(this);
rlTabs.addView(tvTab);
rlTabs.addView(ivLogoSetting);
tabsList.get(i).setTvTabs(tvTab);
lnrLytTabs.addView(rlTabs);
}
abcKeyboardContainerLayout.addView(mInputView);
xHeight = mInputView.getMeasuredHeight();
return abcKeyboardContainer;
}
public View onCreateCandidatesView() {
BaseActivity.printLog(TAG, "onCreateCandidatesView invoked! ");
this.mCandidateView = new CandidateView(this);
this.mCandidateView.setService(this);
return this.mCandidateView;
}
@Override
public void onFinishInputView(boolean finishingInput) {
BaseActivity.printLog(TAG, "onFinishInputView invoked! ");
String isFromMain = PreferenceManager.getDefaultSharedPreferences(this).getString("MAINACTIVITY", "MainActivity");
super.onFinishInputView(finishingInput);
if (mConnector != null) {
mConnector.onDialogDismiss("onFinishInputView");
}
if(appSession.getIsUserVisitToMainActivity())
{
BaseActivity.printLog(TAG , "Process got killed from on finish Input view");
android.os.Process.killProcess(android.os.Process.myPid());
}
}
@Override
public void onStartInput(EditorInfo attribute, boolean restarting) {
BaseActivity.printLog(TAG, "onStartInput invoked!");
super.onStartInput(attribute, restarting);
mInputConnection = getCurrentInputConnection();
this.mComposing.setLength(0);
this.updateCandidates();
// GPSTracker.getInstance(this);
android_id = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID);
//google analytics // for testing
/* abcApplication mabcApplication = (abcApplication)getApplication();
Tracker mTracker = mabcApplication.getDefaultTracker();
mTracker.setScreenName("Image~ " + getResources().getString(R.string.app_screen_name_keyboard_start));
mTracker.send(new HitBuilders.ScreenViewBuilder().build());*/
if (attribute.hintText != null) {
strHintText = String.valueOf(attribute.hintText);
} else {
strHintText = "";
}
if (!restarting) {
this.mMetaState = 0;
}
this.mPredictionOn = false;
this.mCompletionOn = false;
this.mCompletions = null;
switch (attribute.inputType & InputType.TYPE_MASK_CLASS) {
case InputType.TYPE_CLASS_NUMBER: // 2
this.mCurKeyboard = this.mNumericKeyboard;
this.isNumericKeyboard = true;
break;
case InputType.TYPE_CLASS_DATETIME: // 4
this.mCurKeyboard = this.mSymbolsKeyboard;
this.isNumericKeyboard = false;
break;
case InputType.TYPE_CLASS_PHONE: // 3
this.mCurKeyboard = this.mNumericKeyboard;
this.isNumericKeyboard = true;
break;
case InputType.TYPE_CLASS_TEXT: // 1
this.mCurKeyboard = this.mQwertyKeyboard;
this.isNumericKeyboard = false;
this.mPredictionOn = true;
int variation = attribute.inputType & InputType.TYPE_MASK_VARIATION;
if (variation == InputType.TYPE_TEXT_VARIATION_PASSWORD || variation == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
this.mPredictionOn = false;
}
//||variation==EditorInfo.IME_ACTION_SEARCH
if (variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS || variation == InputType.TYPE_TEXT_VARIATION_URI || variation == EditorInfo.TYPE_TEXT_VARIATION_FILTER) {
this.mPredictionOn = false;
}
if ((attribute.inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) {
this.mPredictionOn = false;
this.mCompletionOn = this.isFullscreenMode();
}
updateShiftKeyState(attribute);
break;
default:
this.mCurKeyboard = this.mQwertyKeyboard;
this.isNumericKeyboard = false;
this.updateShiftKeyState(attribute);
break;
}
if (mCurKeyboard != null) {
this.mCurKeyboard.setImeOptions(getResources(), attribute.imeOptions, themevalue);
} else {
return;
}
if (attribute != null) {
try {
inputType = attribute.inputType;
switch (attribute.inputType) {
case 33: // Text_email Type
this.mInputView.setShifted(false);
this.mQwertyKeyboard.setShiftOnOff(getResources(), mInputView.isShifted(), doubleTapOnCaps, themevalue);
this.isNumericKeyboard = false;
break;
case 129: // Text_password Type
this.mInputView.setShifted(false);
this.mQwertyKeyboard.setShiftOnOff(getResources(), mInputView.isShifted(), doubleTapOnCaps, themevalue);
this.isNumericKeyboard = false;
break;
case 145: // Text_password_visible Type
this.mInputView.setShifted(false);
this.mQwertyKeyboard.setShiftOnOff(getResources(), mInputView.isShifted(), doubleTapOnCaps, themevalue);
this.isNumericKeyboard = false;
break;
case 225: // Text_password_web Type
this.mInputView.setShifted(false);
this.mQwertyKeyboard.setShiftOnOff(getResources(), mInputView.isShifted(), doubleTapOnCaps, themevalue);
this.isNumericKeyboard = false;
break;
case 18: // Number_password Type
this.mCurKeyboard = this.mNumericKeyboard;
this.isNumericKeyboard = true;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
How to clear these views? I tried keep null to all view when keyboard is hidden but that is not working!!
I tried calling
android.os.Process.killProcess(android.os.Process.myPid());
it drops the memory consumption directly to 10 MB and clear all the views but in this case , my keyboard open very slowly.
Here is lifecyle,every time when I open keyboard (when I kill process on onFinishInputview) :
I/FA: App measurement is starting up, version: 9256
I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
I/FirebaseInitProvider: FirebaseApp initialization successful
I/MultiDex: install
I/MultiDex: VM has multidex support, MultiDex support library is disabled.
D/SoftKeyboard: onCreate invoked!
D/SoftKeyboard: onInitializeInterface invoked!
D/SoftKeyboard: onStartInput invoked!
Thanks