简体   繁体   中英

My Android Application runs slow and stops responding and crashes and logcat message Unable to allocate xxxxx Bytes of Memory

I am new to Android Programming, I have written Huge amount of code in 2 Activites nearly 6500 lines ,when i am accessing (using) these activites it works perfect , but after some time it runs slow and crashes , i have seen in logcat it shows Unable to allocate xxxxx(some value) bytes of memory. I have initialized null values to all the views which i have used in those activites before comming out(or completing the work) of the activities and called System.gc(); but still i get this error. How to avoid these flaws.

number of Views using in my file:

EditText et_af_loc;
Spinner spn_astStatus, spn_af_astName;
TableRow tr_af_newAst;
ArrayAdapter<String> adp_status, adp_reason;
Button btn_takePic, btn_save, btn_send, btn_gps;
String gpsData = "";
MyLocationListener listener = null;
LocationManager locationManager;
Location loc = null;
ImageView iv_MEPhoto;
Button btn_METakePhoto;
ProgressDialog progressBar, progDailog;
String SENDING_IMAGE_PATH = "";
Spinner spn_af_area;
String image_PATH = "", image_str, ImgString = "";
boolean photoTaken = false;
ArrayAdapter<String> adp_subCompo, adp_drainExi, adp_platExi;
ProgressDialog progServerHit;
String strResponse, sendingString;
DBHelper dbHelper;
SQLiteDatabase db;
TableRow tr_reason;
Spinner spn_reason;
Spinner spn_asstCate, spn_af_drainExist, spn_af_platFormExist,
        spn_af_cisType;
Button btn_af_Send;
ArrayAdapter<String> adp_asstCate, adp_new, adp_area, adp_cisType, adp_sub,
        adp_soFor;
EditText et_other, et_af_capi;
// String newSourId[];
EditText et_af_noOfFill;
TableRow tr_af_capacity, tr_af_noOfFills, tr_af_drainExist,
        tr_af_platExist, tr_af_capaLPM, tr_af_disCharge, tr_af_loc,
        tr_af_areaCoverage, tr_af_make, tr_af_yeildLMP, tr_af_cistern;

EditText et_af_capiLmp, et_af_disCharge, et_af_make, et_af_yeildLMP;

TableRow tr_af_sub, tr_af_MiSsTankCapi, tr_af_soFor;
Spinner spn_af_sourSubType, spn_af_soFor;
TableRow tr_af_sourHeading;
LinearLayout ll_af_sourL, ll_af_sourR;
ArrayList<String> allChkBox;

TableRow tr_af_diaOfBwell, tr_af_deptOfBoreWell, tr_af_noOfGIpipes,
        tr_af_drainCondition, tr_af_watQualPara, tr_af_dtComOfStr,
        tr_af_pumpSetCap, tr_af_pmpSetDesi, tr_af_pmpSetDesHead,
        tr_af_pmpingHrs, tr_af_InfiWellDia, tr_af_InfiWellDepth,
        tr_af_InfiGalLenDia, tr_af_InfiGalLoc, tr_af_InTakStrDiame,
        tr_af_InTakStrDepth, tr_af_OtClWatQualPar, tr_af_IntSumpCap;

EditText et_af_MiSsTankCapi, et_af_diaOfBorewell, et_af_deptOfBwell,
        et_af_noOfGIpipes, et_af_wQualPara, et_af_dtOfComStr,
        et_af_pumpSetCap, et_af_pumpSetDes, et_af_pumpSetDesHead,
        et_af_pumpHrs, et_af_InfiWellDia, et_af_InfiWellDepth,
        et_af_InfiGalLenDia, et_af_InfiGalLoc, et_af_InTakStrDiame,
        et_af_InTakStrDepth, et_af_OtClWatQualPar, et_af_IntSumpCap;
Spinner spn_af_drainCondi;
String sourFrmToFlag;
TextView tv_af_gpsLati, tv_af_gpsLongi;
int count = 0;
String newACCount, newSCCount;
String newAssetCode, newSourceCode, district_id, sourAstID;
String HAB_ID;

TableRow tr_tacassCode_pc, tr_tacassCode_dd, tr_tacassCode_ph,
        tr_tacassCode_dh;
EditText et_tac_psCapInHP, et_tac_psDesDisch, et_tac_psDesHead,
        et_tac_psPumpingHrs;
CheckBox cb_ps;
Spinner spn_tac_subMer;
ArrayAdapter<String> adp_subMer = null, adp_goodBad;
TableRow tr_af_rsFil, tr_af_ssFil, tr_af_clorniation, tr_af_ariea,
        tr_af_miFil, tr_af_claFil, tr_tacassCode_subMer;
String psAvi = "NA", uid;
CheckBox cb_af_rsFil, cb_af_ssFil, cb_af_clori, cb_af_ariea, cb_af_miFil,
        cb_af_claFic;
String cbRsFill = "", cbSsFill = "", cbClori = "", cbAriea = "",
        cbmiFill = "", cbClafi = "";
String otherMandLink = "NA";
EditText et_af_REMARKS;

String ASSET_CODE_GEN, SOURCE_CODE_GEN;
String mandalID;
String panchID, todayDate;

Spinner spn_af_linking;
ArrayAdapter<String> adp_linking;

TableRow tr_af_linking, tr_tacassCode_ssOW, tr_tacassCode_ssHp,
        tr_tacassCode_ssSubSurAss;

// openwell
RadioGroup rg_owIsThereLat10mOfWell, rg_owIsTheLatHighGrThanWell,
        rg_owIsTherSouOfPollWithin10mOfWell,
        rg_IsTherDrainPoorCausStWat2mOfWell, rg_owIsTherFaultDrainChannel,
        rg_owIsTherAdeqParapet, rg_owIsThereCemeFloorLesThan1m,
        rg_owAreWallOfWellInadwqSealed, rg_owAreTherAnyCraksOnCemFloor,
        rg_owAreTheRopeAndBucket, rg_owIsTheWelCovered;

RadioButton rg_owIsThereLat10mOfWell1, rg_owIsTheLatHighGrThanWell1,
        rg_owIsTherSouOfPollWithin10mOfWell1,
        rg_IsTherDrainPoorCausStWat2mOfWell1,
        rg_owIsTherFaultDrainChannel1, rg_owIsTherAdeqParapet1,
        rg_owIsThereCemeFloorLesThan1m1, rg_owAreWallOfWellInadwqSealed1,
        rg_owAreTherAnyCraksOnCemFloor1, rg_owAreTheRopeAndBucket1,
        rg_owIsTheWelCovered1, rg_owIsThereLat10mOfWell2,
        rg_owIsTheLatHighGrThanWell2, rg_owIsTherSouOfPollWithin10mOfWell2,
        rg_IsTherDrainPoorCausStWat2mOfWell2,
        rg_owIsTherFaultDrainChannel2, rg_owIsTherAdeqParapet2,
        rg_owIsThereCemeFloorLesThan1m2, rg_owAreWallOfWellInadwqSealed2,
        rg_owAreTherAnyCraksOnCemFloor2, rg_owAreTheRopeAndBucket2,
        rg_owIsTheWelCovered2;

String owIsThereLat10mOfWell = "NA", owIsTheLatHighGrThanWell = "NA",
        owIsTherSouOfPollWithin10mOfWell = "NA",
        IsTherDrainPoorCausStWat2mOfWell = "NA",
        owIsTherFaultDrainChannel = "NA", owIsTherAdeqParapet = "NA",
        owIsThereCemeFloorLesThan1m = "NA",
        owAreWallOfWellInadwqSealed = "NA",
        owAreTherAnyCraksOnCemFloor = "NA", owAreTheRopeAndBucket = "NA",
        owIsTheWelCovered = "NA";

// handPump
RadioGroup rg_hpIsThereLat10m_mtsOfHP, rg_hpIsTheNeaLatOnHiGr,
        rg_hpIsTheAnyOthSou, rg_hpIsTheDrainPooSta,
        rg_hpIsTheHpDraiChanFaulty, rg_hpIsTheCemFlorles,
        rg_hpIsTherAnyPondingOnCem, rg_hpIsTherAnyCrackOnCem,
        rg_tac_hpIsABuckUsed, rg_hpIsTheHpLooseAtPt,
        rg_hpIsTheCoverOfWelInsani, rg_hpAreTheWalOfWellInadeSea;

RadioButton rg_hpIsThereLat10m_mtsOfHP1, rg_hpIsTheNeaLatOnHiGr1,
        rg_hpIsTheAnyOthSou1, rg_hpIsTheDrainPooSta1,
        rg_hpIsTheHpDraiChanFaulty1, rg_hpIsTheCemFlorles1,
        rg_hpIsTherAnyPondingOnCem1, rg_hpIsTherAnyCrackOnCem1,
        rg_tac_hpIsABuckUsed1, rg_hpIsTheHpLooseAtPt1,
        rg_hpIsTheCoverOfWelInsani1, rg_hpAreTheWalOfWellInadeSea1,

        rg_hpIsThereLat10m_mtsOfHP2, rg_hpIsTheNeaLatOnHiGr2,
        rg_hpIsTheAnyOthSou2, rg_hpIsTheDrainPooSta2,
        rg_hpIsTheHpDraiChanFaulty2, rg_hpIsTheCemFlorles2,
        rg_hpIsTherAnyPondingOnCem2, rg_hpIsTherAnyCrackOnCem2,
        rg_tac_hpIsABuckUsed2, rg_hpIsTheHpLooseAtPt2,
        rg_hpIsTheCoverOfWelInsani2, rg_hpAreTheWalOfWellInadeSea2;

String hpIsThereLat10m_mtsOfHP = "NA", hpIsTheNeaLatOnHiGr = "NA",
        hpIsTheAnyOthSou = "NA", hpIsTheDrainPooSta = "NA",
        hpIsTheHpDraiChanFaulty = "NA", hpIsTheCemFlorles = "NA",
        hpIsTherAnyPondingOnCem = "NA", hpIsTherAnyCrackOnCem = "NA",
        tac_hpIsABuckUsed = "NA", hpIsTheHpLooseAtPt = "NA",
        hpIsTheCoverOfWelInsani = "NA", hpAreTheWalOfWellInadeSea = "NA";

// subsurface assets

RadioGroup rg_tac_ssIsTherLat30m, rg_tac_ssIsTheNearlatUnsewe,
        rg_tac_ssIsTherAnyOthSouWit10m, rg_tac_ssIsTherAnUnCapped,
        rg_tac_ssIsTheDraiAreaPhFaulty, rg_tac_ssIsTheFencAroTheInstDamag,
        rg_tac_ssIsTheFlrOfHpPerTowat, rg_tac_ssIsTheWellSealInsani,
        rg_tac_ssIsTheChlorRecShow, rg_tac_ssIsTheFreeChlResi;

RadioButton rg_tac_ssIsTherLat30m1, rg_tac_ssIsTheNearlatUnsewe1,
        rg_tac_ssIsTherAnyOthSouWit10m1, rg_tac_ssIsTherAnUnCapped1,
        rg_tac_ssIsTheDraiAreaPhFaulty1,
        rg_tac_ssIsTheFencAroTheInstDamag1, rg_tac_ssIsTheFlrOfHpPerTowat1,
        rg_tac_ssIsTheWellSealInsani1, rg_tac_ssIsTheChlorRecShow1,
        rg_tac_ssIsTheFreeChlResi1, rg_tac_ssIsTherLat30m2,
        rg_tac_ssIsTheNearlatUnsewe2, rg_tac_ssIsTherAnyOthSouWit10m2,
        rg_tac_ssIsTherAnUnCapped2, rg_tac_ssIsTheDraiAreaPhFaulty2,
        rg_tac_ssIsTheFencAroTheInstDamag2, rg_tac_ssIsTheFlrOfHpPerTowat2,
        rg_tac_ssIsTheWellSealInsani2, rg_tac_ssIsTheChlorRecShow2,
        rg_tac_ssIsTheFreeChlResi2;

String ssIsTherLat30m = "NA", ssIsTheNearlatUnsewe = "NA",
        ssIsTherAnyOthSouWit10m = "NA", ssIsTherAnUnCapped = "NA",
        ssIsTheDraiAreaPhFaulty = "NA", ssIsTheFencAroTheInstDamag = "NA",
        ssIsTheFlrOfHpPerTowat = "NA", ssIsTheWellSealInsani = "NA",
        ssIsTheChlorRecShow = "NA", ssIsTheFreeChlResi = "NA";

CheckBox cb_tacassCode_ssOW, cb_tacassCode_ssHp, cb_tacassCode_ssSubSurAss;
LinearLayout ll_tac_owRbs, ll_tac_hpRbs, ll_tac_ssRbs;

String owSanataryExisting = "NA", hpSanataryExisting = "NA",
        ssurSanataryExisting = "NA", sanataryValues = "";

TextView tv_af_assetMarker;
TableRow tb_tacassCode_psAvi;

after this i am reading views by using findviewbyId(); then according to business logic i am hide/show some view's and using lot of strings for validations and reading values from EditText and other views for saving data, capturing Image and converting Image to String

ex:

if(editText.getText().toString().equals("")){
Toast.makeText(this,"Enter Value for xxx",5000).show();
}else if(){...... so on ...!   

at last after completing the operation/task i nullify all the views, bitmap=null and call System.gc();. But still App lacks in performance(Not responding,crashes) .

Still what i can do for this code ?

You need to seriously break out this code into other activities or fragments and create a proper flow within your application. You are loading every single object here into memory and most Android devices are not set up to handle this sort of mass allocation of your whole application within one view.

There is no good "how you fix this" answer to your question. Reconsider the design of your application and only keep objects loaded into memory that are needed at that exact moment.

Reuse the same spinner and adjust its properties or deallocate the spinner when you don't need it and create a new one when you need it. Do not allow your view to allocate unnecessary objects and handle that programmatically when you do need it. For example through fragments that contain like code and using a fragment manager to swap out these component views as necessary.

http://developer.android.com/design/patterns/app-structure.html http://developer.android.com/guide/components/fundamentals.html http://developer.android.com/tools/debugging/debugging-ui.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM