Question by
Shyam-Chawla · Jul 27, 2017 at 07:47 AM ·
databasecrashingsqlitememory managementmemory-leak
Sqlite memory leak causing game to crash
I have been having a problem with SQLite in android and ios devices. The app is crashing cos of RAM memory is getting filled and does not function further.
Can someone help by telling me what I have been doing wrong and how to fix it? And how to memory management. I have been using.Dispose() but still, the memory gets filled. Please, download the project file HERE. The app is a simple version of the final app.
Just open the scene and run the app enter a number higher than 700 and device with a ram of 256 will crash. If you put higher number then even on a device with 1gb ram it'll crash.
The code for database connection in below
using System;
using System.Collections;
using System.IO;
using System.Data;
using Mono.Data.Sqlite;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ToRead : MonoBehaviour
{
string DatabaseName = "TestBot.db";
private string dataToWrite;
public Text forMsg;
private int counter;
public GameObject toAct;
void Start()
{
print(Count());
DelteAllAA();
}
public void LetsGo(InputField number)
{
int no = Convert.ToInt32(number.text);
forMsg.text = 0 +" "+ DateTime.Now.ToString("mm:ss");
try
{
for (int i = 0; i < no; i++)
{
dataToWrite = NameSearchResults();
StartCoroutine(EnterReportList(dataToWrite, i));
}
}
catch (Exception e)
{
forMsg.text = e.ToString();
throw;
}
toAct.SetActive(true);
}
//Name That search according TableName from Typed name
public string NameSearchResults()
{
string queryToSend = "SELECT Some FROM TableA";
string toAdd = "";
using (
IDbConnection dbConnection =
new SqliteConnection(GetSQLDBPath()))
{
dbConnection.Open();
//print(" Checking Read ----------------------------- ");
IDbCommand dbCmd;
using (dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = queryToSend;
dbCmd.CommandText = sqlQuery;
IDataReader reader;
using (reader = dbCmd.ExecuteReader())
{
while (reader.Read())
{
toAdd = reader.GetString(0);
}
reader.Close();
// reader = null;
dbCmd.Dispose();
// dbCmd = null;
dbConnection.Close();
dbConnection.Dispose();
}
}
}
return toAdd;
}
//Name That search according TableName from Typed name
public int Count()
{
string queryToSend = "SELECT Count(*) FROM TableB";
int toAdd = 0;
using (
IDbConnection dbConnection =
new SqliteConnection(GetSQLDBPath()))
{
dbConnection.Open();
//print(" Checking Read ----------------------------- ");
IDbCommand dbCmd;
using (dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = queryToSend;
dbCmd.CommandText = sqlQuery;
IDataReader reader;
using (reader = dbCmd.ExecuteReader())
{
while (reader.Read())
{
toAdd = reader.GetInt32(0);
}
reader.Close();
// reader = null;
dbCmd.Dispose();
// dbCmd = null;
dbConnection.Close();
dbConnection.Dispose();
}
}
}
return toAdd;
}
public void DelteAllAA()
{
using (IDbConnection dbConnection = new SqliteConnection(GetSQLDBPath()))
{
dbConnection.Open();
using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = String.Format("DELETE FROM TableB");
dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar();
dbCmd.Dispose();
dbConnection.Close();
dbConnection.Dispose();
}
}
}
IEnumerator EnterReportList(string Word, int i)
{
/* forMsg.text = forMsg.text+" -- " + i +" "+ DateTime.Now.ToString("mm:ss");
yield return new WaitForSeconds(0.05f);*/
using (IDbConnection dbConnection = new SqliteConnection(GetSQLDBPath()))
{
dbConnection.Open();
using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = String.Format(
"INSERT INTO TableB (Some2) Values(\"{0}\")", Word);
dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar();
dbCmd.Dispose();
dbConnection.Close();
dbConnection.Dispose();
}
}
yield return null;
}
public void LoadScene()
{
SceneManager.LoadScene(0);
}
public String GetSQLDBPath()
{
#if UNITY_EDITOR
var dbPath = string.Format(@"Assets/StreamingAssets/{0}", DatabaseName);
#else
// check if file exists in Application.persistentDataPath
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, DatabaseName);
if (!File.Exists(filepath))
{
Debug.Log("Database not in Persistent path");
// if it doesn't ->
// open StreamingAssets directory and load the db ->
#if UNITY_ANDROID
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName); // this is the path to your StreamingAssets in android
while (!loadDb.isDone) { } // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDb.bytes);
#elif UNITY_IOS
var loadDb = Application.dataPath + "/Raw/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WP8
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WINRT
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#else
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#endif
Debug.Log("Database written");
}
var dbPath = filepath;
#endif
return "URI=file:" + dbPath;
}
}
Comment