Search code examples
c#mysql.netmemory-managementpicturebox

I want to delete process memory when insert an image into db c#


I have this below code to put an image into picturebox:

OpenFileDialog f = new OpenFileDialog();
            f.Filter = "JPG(*JPG)|*.jpg";
            if (f.ShowDialog() == DialogResult.OK)
            {

                pictureBox4.Image = Image.FromFile(f.FileName);
            }

and this code below to insert the Image into database:

 public void Team()//insert into db new teammate

    {
        try
        {
            MemoryStream ms = new MemoryStream();
            pictureBox4.Image.Save(ms, pictureBox4.Image.RawFormat);
            byte[] a = ms.GetBuffer();
            ms.Close();


            SqlConnection con = new SqlConnection(stringcon); //CONNECTION


            cmd.Parameters.Clear();
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO team(lastname,firstname,phonenumber,email,[password],[function],[role],registerdata,personaldescription,profilepicture) VALUES(@lastname,@firstname,@phonenumber,@email,@password,@function,@role,@registerdata,@personaldescription,@profilepicture)";

            cmd.Parameters.AddWithValue("@lastname", lastname_textbox.Text);
            cmd.Parameters.AddWithValue("@firstname", firstname_textbox.Text);
            cmd.Parameters.AddWithValue("@phonenumber", "+"+phone_textbox.Text);
            cmd.Parameters.AddWithValue("@email", email_textbox.Text);
            cmd.Parameters.AddWithValue("@password", repeatpassword_textbox.Text);
            cmd.Parameters.AddWithValue("@function", function_textbox.Text);
            cmd.Parameters.AddWithValue("@role", role_dropbox.selectedValue);
            cmd.Parameters.AddWithValue("@registerdata", DateTime.Now.ToString("yyyy-MM-dd HH: mm:ss"));
            cmd.Parameters.AddWithValue("@personaldescription", personaldescription_textbox.Text);
            cmd.Parameters.AddWithValue("@profilepicture", a);


            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
            ex.ToString();
            return;
        }
    }

Now, after insert image in db in want to clear process memory because increase process memory this and that's not good because I want to insert 10 image like this every time I insert into db. ex: I have process memory 120mb at initialize after I insert an image into picturebox I have 150 mb but when I insert 10 image I have 120mb+30mb*10..but I think I can clear this memory after insert into db but I don't know how.


Solution

  • You should go for using code block

    The using statement simplifies the code that you have to write to create and then finally clean up the object. The using statement obtains the resource specified, executes the statements and finally calls the Dispose method of the object to clean up the object.

    Move your code in using as

      using (SqlConnection con = new SqlConnection(connectionString))
      {
            MemoryStream ms = new MemoryStream();
            pictureBox4.Image.Save(ms, pictureBox4.Image.RawFormat);
            byte[] a = ms.GetBuffer();
            ms.Close();
            ...........
      }
    

    Behind the scene using statement gets translated into

    try 
    {
       .....
    }
    finally
    {
       .....
    }