SharedPreferences占用过多的堆内存

时间:2013-08-23 11:44:49

标签: java android memory heap

我正在使用SharedPreferences来存储我的游戏内容,例如您拥有的物品,当前的黄金,装备的物品等。

不使用SharedPreferences我得到25到27 MB的堆,但是当我使用它时,我得到32 MB堆,它增加了5MB。 这是正常还是我做错了什么?

package Util;
import android.app.Activity;

public class StorageInfo {
    public static StorageInfoHelper storage;
    public Activity _context;
    public StorageInfo (Activity context){
        _context = context;
        storage = new StorageInfoHelper(_context);
        storage.reset();
    }
}

package Util;

import android.app.Activity;
import android.content.SharedPreferences;


public class StorageInfoHelper {
    public static final String GoldKEY = "Gold";
    public static final String EquipedItensKEY = "EquipedItens";
    public static final String MyHeadItensKEY = "MyHeadItens";
    public static final String MyPowerUpItensKEY = "MyPowerUpItens";
    public static final String MyBodyItensKEY = "MyBodyItens";
    public static final String MyBoardItensKEY = "MyBoardItens";
    public static final String splitDefault = ",";
    public static final String goldDefault = "0";
    public static final String itenDefault = "0";
    public static final String equipItensDefault = "0,0,0,0";
    private static final int headCNT = 0;
    private static final int PowerUpCNT = 1;
    private static final int bodyCNT = 2;
    private static final int boardCNT = 3;
    private String myHead = new String();
    private String myPowerUp = new String();
    private String myBoard = new String();
    private String myBody = new String();
    private String myEquipedHead = new String();
    private String myEquipedPowerUp = new String();
    private String myEquipedBoard = new String();
    private String myEquipedBody = new String();
    private String myEquipedItens = new String();
    private SharedPreferences settings;
    private SharedPreferences.Editor editor;
    private Activity context;

    public StorageInfoHelper (Activity mycontext){
        context = mycontext;
    }

    private SharedPreferences getPreferences (){
        synchronized (StorageInfoHelper.class){
            if (settings != null){
                return settings;
            }else {
                settings = context.getPreferences(0);
                return settings; 
            }
        }
    }

    private SharedPreferences.Editor getEditor (){
        synchronized (StorageInfoHelper.class){
            if (editor != null){
                return editor;
            }else {
                editor = getPreferences().edit();
                return editor; 
            }
        }
    }

    protected String getStoredFiles(String key){
       // Restore preferences
       return settings.getString(key, new String());
    }

    protected void saveStoredFiles(String key, String value){
      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      getEditor().putString(key, value);

      // Commit the edits!
      getEditor().commit();
    }

    public String getMyGold (){
        return getPreferences().getString(GoldKEY, goldDefault);
    }

    public String addGold (int value){
         getEditor().putString(GoldKEY, String.valueOf(Long.decode(getMyGold()) + value));
         getEditor().commit();
        return getMyGold();
    }

    public String getMyEquipedItens (){
        myEquipedItens = getPreferences().getString(EquipedItensKEY, equipItensDefault);
        return myEquipedItens;
    }

    public void setMyEquipedItens (String equipedItens){
        getEditor().putString(EquipedItensKEY, equipedItens);
        getEditor().commit();
        myEquipedItens = equipedItens;
    }

    public String getMyEquipedHead (){
        myEquipedItens = getMyEquipedItens();
        myEquipedHead = myEquipedItens.split(splitDefault)[headCNT];
        return myEquipedHead;
    }

    public void setMyEquipedHead (String head){
        myEquipedItens = getMyEquipedItens();
        myEquipedPowerUp = myEquipedItens.split(splitDefault)[PowerUpCNT];      
        myEquipedBody = myEquipedItens.split(splitDefault)[bodyCNT];
        myEquipedBoard = myEquipedItens.split(splitDefault)[boardCNT];
        myEquipedItens = head + splitDefault + myEquipedPowerUp + splitDefault + myEquipedBody + splitDefault + myEquipedBoard;
        setMyEquipedItens(myEquipedItens);
    }

    public String getMyEquipedPowerUp (){
        myEquipedItens = getMyEquipedItens();
        myEquipedPowerUp = myEquipedItens.split(splitDefault)[PowerUpCNT];
        return myEquipedPowerUp;
    }

    public void setMyEquipedPowerUp (String PowerUp){
        myEquipedItens = getMyEquipedItens();
        myEquipedHead = myEquipedItens.split(splitDefault)[headCNT];        
        myEquipedBody = myEquipedItens.split(splitDefault)[bodyCNT];
        myEquipedBoard = myEquipedItens.split(splitDefault)[boardCNT];
        myEquipedItens = myEquipedHead + splitDefault + PowerUp + splitDefault + myEquipedBody + splitDefault + myEquipedBoard;
        setMyEquipedItens(myEquipedItens);
    }

    public String getMyEquipedBoard (){
        myEquipedItens = getMyEquipedItens();
        myEquipedBoard = myEquipedItens.split(splitDefault)[boardCNT];
        return myEquipedBoard;
    }

    public void setMyEquipedBoard (String board){
        myEquipedItens = getMyEquipedItens();
        myEquipedHead = myEquipedItens.split(splitDefault)[headCNT];
        myEquipedPowerUp = myEquipedItens.split(splitDefault)[PowerUpCNT];      
        myEquipedBody = myEquipedItens.split(splitDefault)[bodyCNT];
        myEquipedItens = myEquipedHead + splitDefault + myEquipedPowerUp + splitDefault + myEquipedBody + splitDefault + board;
        setMyEquipedItens(myEquipedItens);
    }

    public String getMyEquipedBody () {
        myEquipedItens = getMyEquipedItens();
        myEquipedBody = myEquipedItens.split(splitDefault)[bodyCNT];
        return myEquipedBody;
    }

    public void setMyEquipedBody (String body){
        myEquipedItens = getMyEquipedItens();
        myEquipedHead = myEquipedItens.split(splitDefault)[headCNT];
        myEquipedPowerUp = myEquipedItens.split(splitDefault)[PowerUpCNT];      
        myEquipedBoard = myEquipedItens.split(splitDefault)[boardCNT];
        myEquipedItens = myEquipedHead + splitDefault + myEquipedPowerUp + splitDefault + body + splitDefault + myEquipedBoard;
        setMyEquipedItens(myEquipedItens);
    }

    public String getMyHeads (){
        myHead = getPreferences().getString(MyHeadItensKEY, itenDefault);
        return  myHead;
    }

    public void addInMyHeads (String head) {
        myHead = getPreferences().getString(MyHeadItensKEY, itenDefault);
        myHead = myHead + splitDefault + head;
        getEditor().putString(MyHeadItensKEY, myHead);
        getEditor().commit();
    }

    public String getMyPowerUps (){
        myPowerUp = getPreferences().getString(MyPowerUpItensKEY, itenDefault);
        return myPowerUp;
    }

    public void addInMyPowerUps (String PowerUp) {
        myPowerUp = getPreferences().getString(MyPowerUpItensKEY, itenDefault);
        myPowerUp = myPowerUp + splitDefault + PowerUp;
        getEditor().putString(MyPowerUpItensKEY, myPowerUp);
        getEditor().commit();
    }

    public String getMyBoards (){
        myBoard = getPreferences().getString(MyBoardItensKEY, itenDefault);
        return myBoard;
    }

    public void addInMyBoard (String board) {
        myBoard = getPreferences().getString(MyBoardItensKEY, itenDefault);
        myBoard = myBoard + splitDefault + board;
        getEditor().putString(MyBoardItensKEY, myBoard);
        getEditor().commit();
    }

    public String getMyBodys (){
        myBody = getPreferences().getString(MyBodyItensKEY, itenDefault);
        return myBody;
    }

    public void addInMybodys (String body) {
        myBody = getPreferences().getString(MyBodyItensKEY, itenDefault);
        myBody = myBody + splitDefault + body;
        getEditor().putString(MyBodyItensKEY, myBody);
        getEditor().commit();
    }

    public void reset() {
         getEditor().clear();
         editor.commit();

    }

}

当应用启动时,我只启动一次StorageInfo:

 public class Splash extends Activity {
ImageView imgDuck;
 public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        setContentView(R.layout.splash);

        FadeInDuck();
        new StorageInfo(this);

 }
}

我以这种方式使用它:

Util.MyTextView imgView = (Util.MyTextView) findViewById(R.id.txtCoins);       
        imgView.setText(StorageInfo.storage.getMyGold());

我从不同的上下文(net.ui和net.game)中调用它。 这是一个问题吗?

2 个答案:

答案 0 :(得分:0)

如果您在共享首选项中存储许多不同的值,我相信它是众所周知的。

将数据存储在SQLite数据库中可能更好。

答案 1 :(得分:0)

根据this回答,SharedPreference一旦加载就存储在内存中,在Context中(一旦特定的Context被销毁就会被删除)