Click here to Skip to main content
16,005,178 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using UIEditor from member properties Pin
Gangren17-Dec-04 5:39
Gangren17-Dec-04 5:39 
GeneralRe: Using UIEditor from member properties Pin
Heath Stewart17-Dec-04 7:21
protectorHeath Stewart17-Dec-04 7:21 
QuestionHow to call CreateFileMapping in C# Pin
Anonymous17-Dec-04 3:54
Anonymous17-Dec-04 3:54 
AnswerRe: How to call CreateFileMapping in C# Pin
Heath Stewart17-Dec-04 7:14
protectorHeath Stewart17-Dec-04 7:14 
QuestionMemory Leak? Pin
Federico Milano17-Dec-04 3:16
Federico Milano17-Dec-04 3:16 
AnswerRe: Memory Leak? Pin
Javier Lozano17-Dec-04 6:55
Javier Lozano17-Dec-04 6:55 
AnswerRe: Memory Leak? Pin
Daniel Turini17-Dec-04 7:05
Daniel Turini17-Dec-04 7:05 
AnswerRe: Memory Leak? Pin
Heath Stewart17-Dec-04 7:11
protectorHeath Stewart17-Dec-04 7:11 
Sorry to say, but that doesn't help us answer your question at all. More code is required since no one here would know what Sincronizer is doing.

Guessing, however, you're probably not disposing connections, commands, or any other object that implements IDisposable. While managed objects are managed by the garbage collector (GC), the GC doesn't run unless 1) your application is idle, 2) your machine is running low on memory, or has been exhausted of all memory, or 3) you call GC.Collect, which is not recommended because it will block your threat while it finalizes objects. Good coding practices can get around that last problem by disposing objects correctly.

So, lets say in your implementation you re-use the IDbConnection implementation (for brevity, lets say you're using the System.Data.SqlClient) like SqlConnection. Fine, just make sure you open and close it correctly. For each command that you create, however, you need to dispose it when done:
using (SqlCommand cmd = conn.CreateConnection())
{
  cmd.CommandText = "insert into MyTable (ID, Name) values (1, \"Bob\")";
  try
  {
    conn.Open();
    cmd.ExecuteNonQuery();
  }
  finally
  {
    conn.Close();
  }
}
The outer using statement makes sure the object is disposed, compiling to the following:
SqlCommand cmd = conn.CreateCommand();
try
{
  cmd.CommandText = "...";
  try
  {
    conn.Open();
    cmd.ExecuteNonQuery();
  }
  finally
  {
    conn.Close();
  }
}
finally
{
  cmd.Dispose();
}
As you should see, by expanding the try-finally blocks yourself instead of using using, you could eliminate an extra finally block which would improve performance a little in cases when an exception is thrown.

Also, if you're doing batch updates or inserts, use parameterized queries, re-using the same IDbCommand over and over. See SqlCommand.Parameters in the .NET Framework SDK for an example. While I'm at it, never use string concatentation for constructing SQL expressions - always use parameterized queries. This is far more secure (ever heard of SQL-injection attacks? string concatentation is the first step in making them possible) and actually alieviates quite a bit of your work when doing batch updates or inserts.

If you're using a DataSet to synchronize database, then you also need to dispose the DataSet otherwise it won't be disposed until your application is idle or until your memory is exhausted, which will happen long after you see a general drain on system resources (like CPU cycles).

Note that the disposable pattern doesn't mean the GC won't free your objects, like the "managed environment" means, but that you free the resources immediately. If you don't free them immediately it is not gauranteed when it will happen (taking into account the conditions I spoke of above and depending on the collection generation of the objects).

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: Memory Leak? Pin
Federico Milano17-Dec-04 8:42
Federico Milano17-Dec-04 8:42 
GeneralRe: Memory Leak? Pin
Heath Stewart17-Dec-04 9:51
protectorHeath Stewart17-Dec-04 9:51 
GeneralObject instance from propertyDescriptor type Pin
the last free name17-Dec-04 1:47
the last free name17-Dec-04 1:47 
GeneralRe: Object instance from propertyDescriptor type Pin
Heath Stewart17-Dec-04 6:49
protectorHeath Stewart17-Dec-04 6:49 
GeneralMappingName property Pin
qur17-Dec-04 1:23
qur17-Dec-04 1:23 
GeneralRe: MappingName property Pin
Heath Stewart17-Dec-04 6:39
protectorHeath Stewart17-Dec-04 6:39 
QuestionAn object reference is required for the nonstatic field, method, or property .... why?? Pin
Ponzano Paolo17-Dec-04 1:20
Ponzano Paolo17-Dec-04 1:20 
AnswerRe: An object reference is required for the nonstatic field, method, or property .... why?? Pin
Stefan Troschuetz17-Dec-04 1:46
Stefan Troschuetz17-Dec-04 1:46 
GeneralRe: An object reference is required for the nonstatic field, method, or property .... why?? Pin
Paolo Ponzano17-Dec-04 3:08
Paolo Ponzano17-Dec-04 3:08 
GeneralRe: An object reference is required for the nonstatic field, method, or property .... why?? Pin
J4amieC17-Dec-04 4:03
J4amieC17-Dec-04 4:03 
GeneralRe: An object reference is required for the nonstatic field, method, or property .... why?? Pin
Dave Kreskowiak17-Dec-04 8:27
mveDave Kreskowiak17-Dec-04 8:27 
GeneralDateTime Picker Pin
webhay17-Dec-04 0:58
webhay17-Dec-04 0:58 
GeneralRe: DateTime Picker Pin
Heath Stewart17-Dec-04 1:11
protectorHeath Stewart17-Dec-04 1:11 
GeneralPlacing a AxWebBrowser inside a TabPage Pin
Alberto Bencivenni16-Dec-04 22:48
Alberto Bencivenni16-Dec-04 22:48 
Generalbluetooth Pin
Mridang Agarwalla16-Dec-04 22:06
Mridang Agarwalla16-Dec-04 22:06 
GeneralInterfaces...duhhh! Pin
Mridang Agarwalla16-Dec-04 21:52
Mridang Agarwalla16-Dec-04 21:52 
GeneralRe: Interfaces...duhhh! Pin
Heath Stewart16-Dec-04 23:29
protectorHeath Stewart16-Dec-04 23:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.