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 /
avatar image
0
Question by warance · Sep 29, 2015 at 09:08 AM · databasethreadsthreadingmemory-leakmemory leaks

Memory leak with DataAdapter.Fill to DataSet

I have a simple program that grab data from the database and stored it in a DataSet through DataAdapter.Fill().

The problem I am facing is that the program's memory size keep increasing. Using process explorer, I monitor the virtual size of the program. The processes are run in a standalne build(not in unity editor).

2mins after the process is launch, the program sits at 824,444K virtual size, but leaving the program running for 30 mins, the program's virtual size increased to 1,722,340K.

(can't upload screenshot) https://drive.google.com/open?id=0B0DwzunTEqfKcDhHcXRmV2twUEE

The program only consist of 2 simple script: A helper class SQLController which manage the loading from database to dataset and a monobehavior script DataProducer that poll the database at regular interval for "update" using SQLController object.

SQLController:

 using UnityEngine;
 using System.Collections;
 using System.Data;
 using System.Data.Sql;
 using System.Data.SqlClient;
 using UnityEngine.UI;
 using System.Threading;
 
 public class SQLController {
 
     string connectionString;
     string dataSource, catalog, uid, pwd;
 
     DataSet dat_set;
 
     public SQLController(string dataSource, string catalog, string uid, string pwd)
     {
         this.dataSource = dataSource;
         this.catalog = catalog;
         this.uid = uid;
         this.pwd = pwd;
     }
 
     /// <summary>
     /// Open a connectio to the database and query it for data with the statement input
     /// </summary>
     /// <param name="statement"> The query statement to be used to query the server</param>
     /// <returns></returns>
     public DataSet Load(string statement, string name)
     {
         connectionString = string.Format("data source={0};initial catalog={1};uid={2};pwd={3}", dataSource, catalog, uid, pwd);
         
         using (SqlConnection dbcon = new SqlConnection(connectionString))
         using (SqlDataAdapter dataAdapter = new SqlDataAdapter(statement, dbcon))
         {
             dat_set = new System.Data.DataSet();
 
             dbcon.Open();
             dataAdapter.Fill(dat_set, name);  
         }
 
         return dat_set;
     }
 }

DataProducer

 using UnityEngine;
 using System.Collections;
 using System.Threading;
 using System.Data;
 using UnityEngine.UI;
 using SimpleJSON;
 
 public class DataProducer : MonoBehaviour {
 
 
     public GameObject textObj;
 
     private string[] _configData;
     private string _outputText;
     private SQLController _sqlController;
 
     private bool _toggle = true;
     private bool _updating = false;
 
     private DataSet _dataSetCache;
     private Thread _dataGrabThread;
 
     // Use this for initialization
     void Start () {
         _configData = new string[5];
 
         if (LoadFromConfigFile())
         {
             StartCoroutine(LoadFromDB()); 
         }
     }
     
     // Update is called once per frame
     void Update () {
         textObj.GetComponent<Text>().text = _outputText;    
     }
 
     public void OnDisable()
     {
         // stop any running thread
         if (null != _dataGrabThread && _dataGrabThread.IsAlive)
         {
             _dataGrabThread.Abort();
         }
     }
 
     
 
     IEnumerator LoadFromDB()
     {
         while (true)
         {
             if (_updating)
             {
                 Debug.Log("Data System Poll DataBase ignored");
             }
             else
             {
                 _updating = true;
 
                 _dataGrabThread = new Thread(Load);
 
                 _dataGrabThread.IsBackground = true;
                 _dataGrabThread.Start();
             }
 
             yield return new WaitForSeconds(10f);
         }
 
     }
 
     void Load()
     {
         string statement;
         if (_toggle)
         {
             _toggle = !_toggle;
             statement = "SELECT TOP 100000 [AIIDX],[LASTATTACKDATE],[LASTRECEIVEDDATE],[DEVICEID],[INSTANCES],[ATTACKTYPE],[SEVERITY],[STATUS] FROM AI (NOLOCK)";
         }
         else
         {
             _toggle = !_toggle;
             statement = "SELECT TOP 100000 [AIIDX],[LASTATTACKDATE],[LASTRECEIVEDDATE],[DEVICEID],[SEVERITY],[STATUS] FROM AI (NOLOCK)";
         }
 
         _sqlController = new SQLController(_configData[0], _configData[1], _configData[2], _configData[3]);
 
         _outputText = "Loading";
         _dataSetCache = _sqlController.Load(statement, "TestObject");
         PrintDataSet();
         _updating = false;
     }
 
     /// <summary>
     /// Convert datatable into string and print it out through a text object
     /// </summary>
     void PrintDataSet()
     {
         if (null == _dataSetCache)
         {
             return;
         }
 
         DataTable dt = _dataSetCache.Tables["TestObject"];
 
         if (null == dt)
         {
             return;
         }
 
         
         System.Text.StringBuilder builder = new System.Text.StringBuilder();
 
         for (int i = 0; i < 20; ++i)
         {
             builder.AppendFormat("{0,-5}", (i + 1) + ".");
             //comp.text += string.Format("{0,-5}", (i + 1) + ".");
             DataRow dr = dt.Rows[i];
             for (int j = 0; j < dt.Columns.Count; ++j)
             {
                 builder.AppendFormat("{0, -30}", dr[dt.Columns[j]].ToString());
             }
             builder.Append("\n");
         }
 
         _outputText = builder.ToString();
 
         builder = null;
 
     }
 
     bool LoadFromConfigFile()
     {
         string line;
         using (System.IO.StreamReader file = new System.IO.StreamReader("./config.txt"))
         {
             if (file == null)
             {
                 return false;
             }
 
             int index = 0;
             while ((line = file.ReadLine()) != null)
             {
                 if (index > _configData.Length)
                 {
                     Debug.LogError("Invalid Config file");
                     return false;
                 }
 
                 _configData[index++] = line;
             }
 
             //if the config file does not consist of 5 data
             if (index < _configData.Length)
             {
                 Debug.LogError("Invalid Config file");
                 return false;
             }
 
             return true;
         }
     }    
 }

I am not very sure what exactly causes the memory leak but when I changed the loading from threaded

 _dataGrabThread = new Thread(Load);

to running in the main thread,

 Load()

the process virtual size, though still increment, but at a slower rate. From 828,312K at 2 min to 1,083,908K at 40mins.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to warm-up job threads 0 Answers

[Problem] Use Playerpref.GetInt in other thread 0 Answers

Can I create a looping timer without separate threads? 1 Answer

How do i convert a 3dimensional array to be usable in a Job? 1 Answer

Efficiency of coroutine and what it does? 2 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