如何修复未完成释放的内存?

时间:2016-04-04 07:30:02

标签: android performance android-activity memory-leaks

我有一个3活动应用程序,其中有多个图像和数据,问题是每次我启动Activity 2并完成它,再次调用它将分配内存而不会在完成时释放旧内存,所以当活动2需要22MB内存,回来再调用10次将分配220MB。

有什么建议吗?或者我做错了什么?

编辑:

        name=(<user input>)
        AEX,CAI,CAR,CCA,CEL,CLM,CRE,ECH,FAV,FRE,GMP,INS,ROU,TAR,TAV,UAV,VEH,ERROR=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        name=part._name.split('_')
        if name[0]=='AEX':
            AEX+=1
            i=AEX
        elif name[0]=='CAI':
            CAI+=1
            i=CAI
        elif name[0]=='CAR':
            CAR+=1
            i=CAR
        elif name[0]=='CCA':
            CAR+=1
            i=CCA
        elif name[0]=='CEL':
            CEL+=1
            i=CEL
        elif name[0]=='CLM':
            CLM+=1
            i=CLM
        elif name[0]=='CRE':
            CRE+=1
            i=CRE
        elif name[0]=='ECH':
            ECH+=1
            i=ECH
        elif name[0]=='FAV':
            FAV+=1
            i=FAV
        elif name[0]=='FRE':
            FRE+=1
            i=FRE
        elif name[0]=='GMP':
            GMP+=1
            i=GMP
        elif name[0]=='INS':
            INS+=1
            i=INS
        elif name[0]=='ROU':
            ROU+=1
            i=ROU
        elif name[0]=='TAR':
            TAR+=1
            i=TAR
        elif name[0]=='TAV':
            TAV+=1
            i=TAV
        elif name[0]=='UAV':
            UAV+=1
            i=UAV
        elif name[0]=='VEH':
            VEH+=1
            i=VEH
        else:
            ERROR+=1
            i='error'+str(ERROR)
        output=name[0]+i
        print(output)

并在活动3上遇到同样的问题:

public class Main2Activity extends AppCompatActivity implements View.OnClickListener,OptionSelecterDialog.OnSelectingResult {

private boolean mShowingBack = false;
Runnable runnable;
Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    if (savedInstanceState == null) {
        getFragmentManager()
                .beginTransaction()
                .add(R.id.ad, new CardFlipActivity.CardFrontFragment())
                .commit();
    }

    findViewById(R.id.daily_deals).setOnClickListener(this);
    findViewById(R.id.companies).setOnClickListener(this);
    findViewById(R.id.manage_products).setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Intent intent = null;//=new Intent(Main2Activity.this,TodayDeals.class);
     switch (v.getId()) {
        case R.id.daily_deals:
            intent = new Intent(Main2Activity.this, TodayDeals.class);
            break;
        case R.id.companies:
            intent = new Intent(Main2Activity.this, Categories.class);
            break;
        case R.id.manage_products:
            OptionSelecterDialog dialog = new OptionSelecterDialog();
            String[] array = {"Deals","Products"};
            dialog.setOptions(array);
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            dialog.show(ft, null);
            break;
    }
    if (intent != null) {

        Main2Activity.this.startActivity(intent);
    }
}

@Override
protected void onResume() {
    handler = new Handler();
    runnable = new Runnable() {
        public void run() {
            try {
                flipCard();

            handler.postDelayed(this, 5000);
            }catch (Exception E){}
        }
    };
    runnable.run();
    super.onResume();
}

private void flipCard() {
    if (mShowingBack) {

            getFragmentManager().beginTransaction().setCustomAnimations(
                    R.animator.flip_in_left,
                    R.animator.flip_out_left,
                    R.animator.flip_in_left,
                    R.animator.flip_out_left).replace(R.id.ad, new CardFlipActivity.CardFrontFragment()).commit();
            mShowingBack = false;

        return;

    }

    mShowingBack = true;


    try {
        Fragment fragment = new CardFlipActivity.CardBackFragment();
        getFragmentManager()
                .beginTransaction()

                .setCustomAnimations(
                        R.animator.flip_in_left,
                        R.animator.flip_out_left,
                        R.animator.flip_in_left,
                        R.animator.flip_out_left)

                .replace(R.id.ad, fragment)

                .commit();
    } catch (Exception e) {
    }
}

@Override
protected void onDestroy() {
    handler.removeCallbacks(runnable);
    super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    handler.removeCallbacks(runnable);
    super.onSaveInstanceState(outState);
}

@Override
public void onSelectingResult(int result) {
    Intent intent = null;
    switch(result){
        case 0 :break;

        case 1:
            intent = new Intent(Main2Activity.this, ManageProducts.class);
            break;
    }

    if(intent!=null) {
        Main2Activity.this.startActivity(intent);
    }
}

}

0 个答案:

没有答案