Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8599815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:28:38+00:00 2026-06-12T01:28:38+00:00

I have never posted on this forum before but use it a lot for

  • 0

I have never posted on this forum before but use it a lot for research, but this time I could not find an answer… maybe I am just not wording it correctly.

I am using the SqlCeCommand quite a lot in a Compact Framework project I have been working on for a while and have had a number of issues with running out of memory, so I’m trying to optimize the unmanaged code parts better.

Take a look at this:

Dim SQLCmd as SQLCeCommand

SQLCmd = New SQLCeCommand
SQLCmd.Connection = conndb
... Process db stuff

SQLCmd = New SQLCeCommand
SQLCmd.Connection = conndb
... Process db stuff

SQLCmd = New SQLCeCommand
SQLCmd.Connection = conndb
... Process db stuff

SQLCmd.Dispose()

Is this OK, or do I lose the memory each time I call New on the same object ? The reason why I was doing this rather than keeping the same instance of the object was so I did not have to explicitly set the SQLCmd properties each time. As some might use parameters and some might not so I thought using new would be an easier way of making sure everything was clear.

Any thoughts or a better way to approach this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:28:39+00:00Added an answer on June 12, 2026 at 1:28 am

    In your code, everytime you call New the previous reference is lost and then it is the garbage collector work that frees, the now, unreferenced object. This happens when the Garbage Collector framework decides that it is time to get back the memory and thus it doesn’t happen immediately

    To do a better work you could use the Using statement

    Dim SQLCmd as SQLCeCommand 
    
    Using SQLCmd = New SQLCeCommand 
        SQLCmd.Connection = conndb 
        ... Process db stuff 
    End Using
    
    Using SQLCmd = New SQLCeCommand 
        SQLCmd.Connection = conndb 
        ... Process db stuff 
    End Using
    
    Using SQLCmd = New SQLCeCommand 
        SQLCmd.Connection = conndb 
        ... Process db stuff 
    End Using
    

    In this way you call Dispose automatically for the previouse SqlCmd and give to the Garbage Collector a strong hint to collect the unused memory.

    From MSDN

    Managed resources are disposed of by the .NET Framework garbage collector (GC) without any 
    extra coding on your part. You do not need a Using block for managed resources. However, 
    you can still use a Using block to force the disposal of a managed resource instead of 
    waiting for the garbage collector.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never posted before so I am sorry if I am not clear.
Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
Already posted this in Google Analytics help forum, but noone could help. Hopefully I
I have never come across this issue but most recently I noticed that a
I have never thought about until recently, but I'm not sure why we call
So I have been here before asking about this issue but no one has
Even though the solution is so obvious I should have never have posted this,
(Note: I originally posted this on drupal.org before remembering that I never get a
while programming throughout the years I have never posted a question on this website,
Even though the solution is so obvious I should have never have posted this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.