Releasing and Managing Memory After An App Is Exited

Mahares

Expert
Licensed User
Longtime User
I have a central app that uses a listview with 10 items. Each item, when clicked, opens a different app using StartActivity(MyIntent). Whener an item is clicked an INTENT is triggerred to open another application. All 10 applications access the same database. Some graph data and some display it. Once I am finished with one app, I press the back key which returns me to the calling central app which has the 10 listview choices. Every once in a while I get SQLite database locked error despite closing the cursors.
Do I need to have the following code in each app to exit each app graciously so memory is released so another app can use it or is there a better way to do it:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean  
   If KeyCode = KeyCodes.KEYCODE_BACK Then Activity.Finish
End Sub
I hope you can put an end to my confusion. Thank you
 

mc73

Well-Known Member
Licensed User
Longtime User
Hello my friend. Are you also closing the sql object apart from the cursors? Also, I've notice a similar behavior today, and actually tomorrow I have to really solve this, cause I need to upload dbs. Uploading dbs with db-journal existing, is problematic.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
No, I do not close the SQL object. But, I declare the SQL object in process global for each one of the 10 apps separately, because at times I run them independently of each other. Each app has this:
B4X:
Dim SQL as SQL1   'process global
and in Activity_Create I have the following:
B4X:
If SQL1.IsInitialized =False Then  
         SQL1.Initialize(DBFilePath, DBFileName, False)
End If
All 10 apps use the same database. Therefore, I do not close SQL object after each app is exited because each of the 10 apps when exited, return to the central app, where I choose another app that uses the same instance of SQL.
Thank you
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
How can different apps use the same SQL object? Are they just different activities of the same app? If not, these objects are not the same. Anyway, if you test SQL.close and this doesn't solve the problem, you can post 'suspicious' pieces of your code, I am sure there will be a solution.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@mc73:
How can different apps use the same SQL object? Are they just different activities of the same app

No. They do not use the same SQL object. They are not different activities. They are full fledged applications. ALl apps are independent of each other. I just use SQL1 as the name of the object in each, but they do not have any interaction with each other. I just do not close the object when I finish with one and go to the other. Do I need to close the object each time even though The problem only happens very sporadically. Do I need to close the SQL object each time I exit even though I use the same variable name SQL1?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I am always closing the sql object. After all, I think it takes up memory until the os frees it. I am also using activity.finish.
Here's a related answer.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Do you really need to close the SQL object when you know you are immediately starting another application that uses the same database as the app you just exited, particularly when today's devices have plenty of memory. Can someone answer this for me and mc73 with complete certainty?
 
Upvote 0
Top