Android Question Trapping Out Of Memory Exception

Del

Member
Licensed User
Longtime User
I have created an app which scrapes text from a website but sometimes the amount of data is larger than the Android device can handle and the app stops dead with 'Unfortunately, AppName has stopped.'

The following code simulates this :-
B4X:
    Dim sBigString As String = "XXXXXXXXXXX"
    Do Until sBigString = "never"
        Try
            sBigString = sBigString & sBigString
        Catch
            Log (LastException)
        End Try
    Loop

How do I trap for this so that I can warn the user and recover from the exception ?.

Note, I am not asking about how to avoid out-of-memory per se.

Best Regards

Del, England +++
 

Informatix

Expert
Licensed User
Longtime User
I have created an app which scrapes text from a website but sometimes the amount of data is larger than the Android device can handle and the app stops dead with 'Unfortunately, AppName has stopped.'

The following code simulates this :-
B4X:
    Dim sBigString As String = "XXXXXXXXXXX"
    Do Until sBigString = "never"
        Try
            sBigString = sBigString & sBigString
        Catch
            Log (LastException)
        End Try
    Loop

How do I trap for this so that I can warn the user and recover from the exception ?.

Note, I am not asking about how to avoid out-of-memory per se.

Best Regards

Del, England +++
Out of Memory is not an exception but an error. You cannot catch it with B4A.
 
Upvote 0

Del

Member
Licensed User
Longtime User
Dear Erel

Thanks for this but as I said in my first post I am looking for a way around the way my app stops dead when it gets an out-of-memory error.

The code I posted simply creates this situation and demonstrates what happens. You have to look in the unfiltered log to see the Java error. There is no B4A entry in the log because Java is doing it. It's nothing to do with StringBuilder.

The app runs out of memory because it is reading a whole website into a string but when I parse that string to extract the data the extra memory needed for that is not enough. So far I have not made it crash because the website string itself is too large, but it may happen. I have no idea in advance how big the website string may be and I have no idea (at the moment) how much memory I still have to play with. That may be the subject of another thread.

What I want to do is to restart the app after the crash and tell the user not to be so ambitious in the future.

What I had in mind was some sort of service that would look to see if the app had failed and then restart it.

By the way to Informatix in my software house we do not talk about errors because if we do someone starts looking for someone to blame. So we have exceptions, that word is neutral.

Regards

Del +++
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The pseudo code you posted will lead to out of memory exceptions.

If you post the relevant code then we might help you optimize it and get rid of the memory issue (maybe).

There are two types of "exceptions" in Java. Exceptions and Errors. Errors are usually not caught as they mean that there is a severe error which is probably unrecoverable.
 
Upvote 0
Top