C# program with memory issues

    Date: 06/20/06 (C Sharp)    Keywords: programming, database, sql, web

    I'm writing a program in C# 1.1 that extracts documents from our current database to be then transferred into our new content manager/collaborator or whatever. Anyways I have basically written the program and it successfully extracts the documents that I tell it to, but the problem is that its a major memory hog, and when I monitor the memory and the heap size it is just staggering. The problem is that I basically have loops that are going and grabbing sometimes hundreds of documents at a time, and when the documents are over 1mb in size it almost starts to get scary. I should also point out that I've never written a program like this before and I have learned more about programming and memory management than I ever cared to know. I don't know if there's anything I'm programming wrong, or if I'm not releasing resources correctly. My "expertise" is more in Web development than this, and it doesn't help that not alot of people at my company know C#. Anyways below is the code I have for grabbing multiple documents.

    {
    con.Open();

    SqlDataAdapter da = new SqlDataAdapter("Select * FROM tblMics_Docs_Versions WHERE mvItemID = " + number + " ORDER BY mvVersionedDate DESC", con);

    int i = 0;
    int j = 1;

    byte[] MyData = new byte[0];

    da.Fill(ds.Tables[1]);
    da.Dispose();

    DataRow myRow;

    for (i = 0; i <= ds.Tables[1].Rows.Count - 1; i++)
    {

    myRow=ds.Tables[1].Rows[i];
    MyData = (byte[])myRow["mvContent"];

    string thefile;

    string theid;

    thefile = myRow["mvFileName"].ToString();

    theid = myRow["mvItemID"].ToString();

    int ArraySize = new int();
    ArraySize = MyData.GetUpperBound(0);

    string s=System.IO.Path.Combine(dept, "ID " + theid + " Version " + j + " " + thefile);
    DirectoryInfo di = Directory.CreateDirectory(@"c:\BMS Old\" + dept);
    FileStream fs = new FileStream(@"C:\BMS Old\"+ s, FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(MyData, 0,ArraySize);

    fs.Close();



    myRow = null;
    MyData = null;
    j++;
    s = null;
    di = null;
    thefile = null;
    theid = null;

    }
    ds.Dispose();
    ds.Tables[1].Dispose();
    ds.Tables[1].Clear();

    con.Close();

    //con.Dispose();
    //con = null;
    }

    I'm sure this code isn't great, but at the very least it WORKS. Now I need to clean it up so we can run this in a production environment. Thank you.

    Source: http://community.livejournal.com/csharp/65808.html

« A Collection of Objects... || Working with a screwed up... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home