如何在黑莓中的持久性存储上查找存储的对象的大小(以字节或kb,mb为单位)

时间:2011-06-22 10:29:53

标签: blackberry persistence persistent-storage

我试图找到我在持久性存储上存储的对象的大小。我以编程方式找到了对象的大小,如代码所示,但是当它存储在持久性存储中时,我无法找到该对象的大小。 数据在存储时会自动压缩。 我正在使用Memory.getFlashStats()。getFree();在commitnig要存储的对象之前和之后获取持久性存储的可用大小,并且两个值之间的差异应该等于我已经计算的对象的大小。 请参阅代码

package Bean;

import java.util.Enumeration;
import java.util.Vector;

import net.rim.device.api.system.Memory;
import net.rim.device.api.system.MemoryStats;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.MainScreen;
import bean.UserCredentials;

public class MyStoreScreen extends MainScreen implements FieldChangeListener
{
    private int nObjectSize;
    static final long PERSISTENT_STORE_DEMO_ID = 0x42c16456ab0b5eabL;
    private static PersistentObject oPersistenStore;

    private  int nFreePersistenceInStarting=Memory.getPersistentStats().getFree();
    private int nFreePersistenceAtEnd;
    ButtonField oCalculateMemButton ;

    private MenuItem saveItem = new MenuItem("Save ", 110, 10) 
    {

        public void run()
        {
            Dialog.alert("initially free memory ----------------------"+nFreePersistenceInStarting);

            oPersistenStore = PersistentStore.getPersistentObject(PERSISTENT_STORE_DEMO_ID);
            Vector storeVector = (Vector)oPersistenStore.getContents(); 
            Vector userinfo ;
            int size = (storeVector == null) ? 0 : storeVector.size();
            if(size == 0)
            {
                userinfo = new Vector();
            }
            else
            {
                userinfo = storeVector;
            }

            UserCredentials oUserCredentials = new UserCredentials("akanksha","chandra",1,3434.3434,343545646);
            for(int i =0;i<=100;i++)
            {
                userinfo.addElement(oUserCredentials);
            }
            nObjectSize= fnCalculateSizeOfObject(userinfo);
            Dialog.alert("size of  object is "+ nObjectSize);

            synchronized(oPersistenStore)
            {
                oPersistenStore.setContents(userinfo);
                oPersistenStore.commit();
            }
        }
    };

    private MenuItem getItem = new MenuItem( "Get item", 110, 11 ) 
    {
        public void run()
        {
            oPersistenStore = PersistentStore.getPersistentObject(PERSISTENT_STORE_DEMO_ID);
            synchronized(oPersistenStore) 
            {
                Vector arrCredential = (Vector)oPersistenStore.getContents();
                if(arrCredential != null)
                {
                    String dataContents = "";
                    int nSize = (arrCredential == null) ? 0 : arrCredential.size();
                    if(nSize != 0)
                    {
                        for (int i = 0; i < nSize; i++)
                        {
                            UserCredentials oUserCredentials = (UserCredentials)arrCredential.elementAt(i);
                            dataContents+="\n size of vector is "+nSize+ " username : "+oUserCredentials.getStrUsername()+"\n password : "+oUserCredentials.getStrPassword();
                            dataContents += "\n\nUser sal  : "+oUserCredentials.getdSalary();
                            dataContents += "\n amount : "+oUserCredentials.getlAmount();
                            dataContents += "\n  s no "+oUserCredentials.getnSerialNo();
                        }
                        Dialog.alert(dataContents);
                    }
                    else
                    {
                        Dialog.alert("Zero Elements ");
                    }
                }
                else
                {
                    Dialog.alert("No contents ");
                }
            }
        }
    };

    private MenuItem resetStoreItem = new MenuItem( "Delete Store", 110, 11 )
    {
        public void run()
        {
            int choice = Dialog.ask(Dialog.D_OK_CANCEL, "Do you want to delete ?");
            if(choice == Dialog.D_OK)
            {
                //              oPersistenStore = PersistentStore.getPersistentObject(PERSISTENT_STORE_DEMO_ID);
                PersistentStore.destroyPersistentObject(PERSISTENT_STORE_DEMO_ID);
            }
            else
            {

            }
        }
    };
    private MenuItem CalculateTotalFlashUsed =  new MenuItem("calculate used flash size ", 0, 7)
    {
        public void run()
        {
            Dialog.alert("used  size of Persistence Store is "+fnUsedPersistenceSize());
        };
    };

    public MyStoreScreen()
    {
        oCalculateMemButton = new ButtonField("calculate free flash memory in starting", ButtonField.CONSUME_CLICK);
        oCalculateMemButton.setChangeListener(this);
        this.add(oCalculateMemButton);

        this.addMenuItem(saveItem);
        this.addMenuItem(getItem);
        this.addMenuItem(resetStoreItem);
        this.addMenuItem(CalculateTotalFlashUsed);
        oPersistenStore = PersistentStore.getPersistentObject(PERSISTENT_STORE_DEMO_ID);

    }

    public void fieldChanged(Field field, int context) 
    {
        if(field ==oCalculateMemButton)
        {
            //                      nFreeFlashInStarting =Memory.getFlashTotal();
            //          nFreeFlashInStarting =Memory.getFlashStats().getFree();
            //          nFreeFlashAtEnd =Memory.getFlashStats().getFree();

            //          String message = "total flash is Memory.getFlashStats().getAllocated(); "+nFreeFlashInStarting+" Memory.getFlashTotal()  is : "+Memory.getFlashTotal();
            String message = "total free flash memory in starting is :"+ nFreePersistenceInStarting;
            Dialog.alert(message);

        }   

    }

    private int  fnCalculateSizeOfObject(Vector userInfo ) 
    {
        int nSize = 0;
        Enumeration oEnumeration = userInfo.elements();
        while(oEnumeration.hasMoreElements())
        {
            UserCredentials oUserCredentials = (UserCredentials) oEnumeration.nextElement();
            String UserName = oUserCredentials.getStrUsername();
            String password = oUserCredentials.getStrPassword();
            int nSerialNo = oUserCredentials.getnSerialNo();
            double dSalary = oUserCredentials.getdSalary();
            long lAmount = oUserCredentials.getlAmount();

            nSize+= 4+8+8+fnCalculateSizeOfString(UserName)+fnCalculateSizeOfString(password);      
        }
        return nSize;       

    }

    private int fnCalculateSizeOfString(String strInputString)
    {
        //convert String to char array
        char[] characterArray = strInputString.toCharArray();
        int nlength = characterArray.length;        

        return nlength;     
    }

    public int  fnUsedPersistenceSize()
    {
        nFreePersistenceAtEnd = Memory.getPersistentStats().getFree();
        int nUsedPersistenceMemory = nFreePersistenceInStarting -nFreePersistenceAtEnd;

        return nUsedPersistenceMemory;
    }
}

1 个答案:

答案 0 :(得分:0)

Memory.getFlashStats().getFree()不准确,你可以在多次测量同一物体的存储时看到它 - 每次的值可能不同。

测量对象大小的最佳方法是计算其字段大小(您实际执行的操作)。