×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Question Handle low storage space condition

    Hi All,

    I want to handle low storage space condition(isolated storage not ram memory) in my application. So if this case happens do i get some notification from system? Or do i need to set up a timer to check this every now and then myself. Searching for this i got only about handling low ram memory conditions. Please help

    Regards

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    292

    Re: Handle low storage space condition

    Hello Kunal as far as I know, there is IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace which return the available space on the phone, and
    IsolatedStorageFile.GetUserStoreForApplication().IncreaseQuotaTo()
    which helps increase the quota allowed (returns a bool, true if increased else it returns false)

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Handle low storage space condition

    Hi Loukt,

    So i need to check and handle these conditions by myself using timer or some other logic right.? System will not convey me by raising some event when IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace becomes low.

    Regards

  4. #4
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    292

    Re: Handle low storage space condition

    Quote Originally Posted by Kunal Prime View Post
    Hi Loukt,

    So i need to check and handle these conditions by myself using timer or some other logic right.? System will not convey me by raising some event when IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace becomes low.

    Regards
    Hello Kunal,

    not necessary a timer, but each time you want to write on the IsolatedStorageFile

    use this snippet to check if there is enough disk space, (I think neededSpace needs to be the byte value, as for 1Mb => 1*1024*1024 bytes )
    Code:
    public void CheckDiskSpace(long neededSpace)
    {
        using (IsolatedStorageFile myISFile = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myISFile.AvailableFreeSpace > neededSpace)
            {
                if (!myISFile.IncreaseQuotaTo(myISFile.Quota + neededSpace)) // newSpace = oldSpace(myISFile.Quota) + neededSpace
                {
                    throw new Exception("No space available on the phone, please delete a file");
                }
            }
        }
    }
    I've just wrote it and didn't test it yet, you can run it and give us feedback.

    Yassine,
    Last edited by Loukt; 2013-03-12 at 11:28.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Handle low storage space condition

    Hi Yassine,

    Thanks, will test this snippet and let you know.

    Regards

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Handle low storage space condition

    Hi Yassine,

    Used below code
    using (IsolatedStorageFile myISFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
    if (myISFile.AvailableFreeSpace < neededSpace)
    {
    long newSpace = myISFile.Quota + 10485760L;
    if (!myISFile.IncreaseQuotaTo(newSpace))
    {
    throw new Exception("No space available on the phone, please delete a file");
    }
    else
    {
    Debug.WriteLine(myISFile.Quota);
    }
    }
    }

    But it throws me below exception.

    {System.ArgumentException: The new quota must be larger than the old quota.
    at System.IO.IsolatedStorage.IsolatedStorageFile.IncreaseQuotaTo(Int64 newQuotaSize)
    at SharedCode.IsolatedStorageAccess.FileStore.CheckDiskSpace(Int64 neededSpace)
    at Marketplace.LandingPage..ctor()} System.Exception {System.ArgumentException}
    While debugging i found myISFile.Quota returns 9223372036854775807 which is the maximum possible value for a signed long. So when we add anything to it appends a minus sign to indicate a bigger number. So initially myISFile.Quota = 9223372036854775807 and after adding 10MB, newSpace = -9223372036844290049. Which myISFile.IncreaseQuotaTo considers less than earlier quota and throws the exception. :P

    Any thoughts or suggestions on this.??

    And sorry i don't know how to embed code in reply so putting them in [QUOTE] tags.

    Regards
    Last edited by Kunal Prime; 2013-03-12 at 09:10.

  7. #7
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    292

    Re: Handle low storage space condition

    Hello Kunal (you just need to put your code in [CODE] tags),

    I've just tryed the snippet, and yes I get the same result as you, first you need to know that Quota returns the maximum

    after a small search I found this : "Windows Phone apps are not restricted to a particular quota. They should make careful use of storage based on their app scenario requirements."
    that's why we get 9223372036854775807 as a Quota.
    that snippet would work fine on a Silverlight app, but in the WP case there is no need, so basically since the Quota is not restricted all you'll have to do is to see if there is available space on the phone.

    Code:
    public void CheckDiskSpace(long neededSpace)
    {
       using (IsolatedStorageFile myISFile = IsolatedStorageFile.GetUserStoreForApplication())
       {
           if (myISFile.AvailableFreeSpace < neededSpace) // if true, means there is no space available in the phone.
           {
               throw new Exception("No space available on the phone, please delete a file");
           }
       }
    }

  8. #8
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Handle low storage space condition

    Hi Yassine,

    Ya i did that in meantime and arrived at the same code. BTW thanks for the [CODE] tag.

    Regards

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Melbourne, Australia
    Posts
    2,578

    Re: Handle low storage space condition

    Interesting question - Yassine, possibly a good example for a FAQ? :-)

    I've created a preliminary howto on this here: How_to_check_for_available_storage_space_on_Windows_Phone. Can you please check it. Note, normally I'd link to the official IsolatedStorageFile Howtos, but now most of these have been removed because this is a deprecated API for WP8.

    A few questions.
    1. Why check for availability rather than doing try-catch?
    2. In this case we're after something that works for WP7 and 8, so IsolatedStorageFile is best. However going forward people are supposed to use Windows.Storage. What would be the correct code for WP8 and later?

    Anyone game to create some example code we can attach.

    This is still in draft while we discuss.

  10. #10
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Handle low storage space condition

    Hi hamishwillee,

    Answering your questions.
    1. Why check for availability rather than doing try-catch?
    Ans. Say you have very important data in memory which should never be lost in any case. Say you need atleast "X"MB of space on disk to save that data. So you can keep checking for available space at regular intervals (maybe increase that interval when free space becomes < 100MB) and the moment available space goes less than "X+2"MB save the data and convey user of low storage space. But if you use a try catch block and you get an exception, which means there is no more space to store data. In this scenario max you can do is to convey user. Now if user do not free some space your data is doomed.

    2. In this case we're after something that works for WP7 and 8, so IsolatedStorageFile is best. However going forward people are supposed to use Windows.Storage. What would be the correct code for WP8 and later?
    Maybe Yassine is better candidate to answer this question.

    Regards

  11. #11
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    433

    Re: Handle low storage space condition

    Well, if you roughly know how large a dataset can get you could check upon creation wether there is enough space available. E.g. if you take a photo you could do the storage space check when opening the ViewFinder and prevent the user from taking pictures he can't store later on. I believe it would be best to tie the checks to certain actions rather than to some kind of "timer".

    try/catch should still be used so the user can be informed about what went wrong in case the estimate of how much space would be required was wrong, etc.

  12. #12
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Handle low storage space condition

    Hi SB Dev,
    Quote Originally Posted by SB Dev View Post
    Well, if you roughly know how large a dataset can get you could check upon creation wether there is enough space available.
    In my case its not quite possible to estimate data set size. It completely depends on user actions. But yes, if that's possible then checking available space at start up will be very useful. Good point.

    Quote Originally Posted by SB Dev View Post
    I believe it would be best to tie the checks to certain actions rather than to some kind of "timer".
    Absolutely. Even i did it on user actions.

    Regards

  13. #13
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    292

    Re: Handle low storage space condition

    For the first one, if my app uses Isolated Storage a lot I will check the availability in the app launch, why ? simply to show the user a message telling him that the phone is low on memory or after every change happended on the isolated storage.

    Regarding the second one, I made a little search and I found no method for retrieving disk space in Windows.Storage even if it is used by WP8 and WinRT apps. WinRT developpers are using a special function found in kernet32.dll GetDiskFreeSpaceEx, and as written in the docs, this api is supported in Windows Phone 8, which I found doubtful since you need to give the disk name as a parameter

Similar Threads

  1. Replies: 5
    Last Post: 2010-05-09, 22:32
  2. Storage space and processing a MIDLET??
    By quoctrang in forum Mobile Java General
    Replies: 6
    Last Post: 2009-01-13, 04:21
  3. Sent items (Storage space)
    By Gian82 in forum General Development Questions
    Replies: 1
    Last Post: 2008-11-28, 16:49
  4. Replies: 4
    Last Post: 2008-09-03, 16:11
  5. Hoow to handle low memory condition ?
    By chirag_cel in forum Symbian
    Replies: 3
    Last Post: 2007-12-29, 08:23

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •