Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IOS unity taking lot of memory 0 Answers

issues with SQLITE DB, Getting the next data in the same column? 0 Answers

Database Error After Building 0 Answers

Release memory in Addressable not working in webgl 0 Answers

How to read a database (sqlite) on Android? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges