Results 1 to 6 of 6

Thread: correct way to free memory

  1. #1

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Resolved correct way to free memory

    hello,

    can someone please tell me if the above example is totally corerct to free OLEDB connection and memory of asp.net applications?
    VB Code:
    1. 'connection
    2. Dim StrConnect as String, StrOpen as string
    3. Dim DBconn As OleDbConnection, DBcomm as OleDbCommand, DBread as OleDbDataReader
    4. StrConnect = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=mydb.mdb"
    5.  
    6. DBconn=New OleDbConnection(StrConnect)  'connection to DB
    7. DBconn.Open()
    8. 'connect
    9. Stropen="Select * " & _
    10.    "FROM structure " & _
    11.    "WHERE " & _
    12.    "(pid = " & pid & ")"
    13. DBcomm=New OleDbCommand(Stropen,DBconn)
    14. DBread=DBcomm.ExecuteReader()
    15.  
    16. if DBread.read() then
    17.    Title.text = DBread("name")
    18. end if
    19.  
    20. DBread.close()
    21. DBconn.close()
    22.  
    23. 'free memory
    24. DBcomm.dispose()
    25. DBconn.dispose()
    26. DBread = nothing
    thank you very much,

    wc.
    Last edited by wildcat_2000; Nov 8th, 2004 at 08:03 AM.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  2. #2
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I believe that you can dispose of DBread as well. Not sure because I havn't used this kind of data reader before. I use the SQL type of data read.

    For this line.
    VB Code:
    1. Dim StrConnect as String, StrOpen as string
    The more efficient way to do this would be
    VB Code:
    1. Dim StrConnect, StrOpen as string

    Besides that, it looks good to me. With .Net, the garbage collector will clean up the memory for you. However, it is still good practice to dispose of the variables like you are doing.

    Last edited by indydavid32; Nov 3rd, 2004 at 07:58 AM.
    David Wilhelm

  3. #3

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    thank you indydavid32,

    even though .net is a complete new programming environment, i'm pretty sure as well that manually settings objects to null is the best way to do so. i'm an old vb6 programmer

    but why
    VB Code:
    1. Dim StrConnect, StrOpen as string
    would be a more efficient way?

    in old (again) vb6, this would mean that the first variable would be declared as variant. is this different in .net?

    finally, i'm pretty sure you cannot dispose of a OleDbDataReader, this is why i'm setting it to nothing instead.

    cheers,

    wc.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  4. #4
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    In .Net, dim string1, string2, string3, string4 as String will set all of them to string.
    David Wilhelm

  5. #5

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    ok, thank you!
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  6. #6
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Talking

    Your welcome. Come to think of it, you can't dispose of the sql data reader either. I had a brain fart there. lol
    David Wilhelm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width