开发者

how to find the size of object stored (in bytes or kb,mb) on persistence store in blackberry

开发者 https://www.devze.com 2023-03-14 19:38 出处:网络
I am trying to find the size of the object that I store on persistence store .I have programatically found out the size of object as shown in code but I can not find out the size of this object when i

I am trying to find the size of the object that I store on persistence store . I have programatically found out the size of object as shown in code but I can not find out the size of this object when it is stored on persistence store. Does data get compressed automatically when it is comitted to store. I m using Memory.getFlashStats().getFree(); to get the free size of persistence store before and after commitnig the object to store and the difference between the two values should be equal to the size of object that i have calculated. please see the code

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);
 开发者_C百科       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;
    }
}


Memory.getFlashStats().getFree() is not that accurate, you can see it when measuring storage of the same object several times - values may be different every time.

The best way to measure object size is to calculate its fields size (what you actually doing).

0

精彩评论

暂无评论...
验证码 换一张
取 消