locked
GC Use in c# RRS feed

  • Question

  • I use a for loop in that the loop is run half and some time complete due to memory leakege. i want to use GC and i am new and never use it already. Here is my code where error come.

    for (int i = 0; i < tempFiles.Count; i++)
                            {
                                
                                counter++;
                                //image.AddPage(codecs.Load(tempFiles[i]));
                                image = codecs.Load(tempFiles[i]);
                                System.Threading.Thread.Sleep(50);
                               
                                codecs.Save(image,
                                   _fileName,
                                   _format,
                                   _bitsPerPixel,
                                   image.Page,
                                   image.PageCount,
                                   -1,
                                   CodecsSavePageMode.Append);



                                image.Dispose();}

    Please help me out.

    Thanx: Ali Raza


    • Edited by HotKiller Tuesday, April 3, 2012 4:48 AM
    • Moved by Reed KimbleMVP Tuesday, April 3, 2012 9:19 PM C# question in VB (From:Visual Basic Language)
    Tuesday, April 3, 2012 4:48 AM

Answers

  • Hi HotKiller,

      You can fill image.Dispose(); into finally block if you need to use GC collection. I think you may try using try{}catch(Exception ex){}Finally{} syntax to implement it.

    Sincerely,

    Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 4, 2012 6:04 AM
    Moderator
  • You don't "use" the GC.  The GC works silently in the background and does it's thing on it's own.  Virtually anything that you do to try to mess with it will make things worse, not better.  It was designed to be left alone.

    Since you're dealing with images, it's possible (but I couldn't say without profiling the code to know for sure) that your issue is not releasing unmanaged resources.  Unmanaged resources, by definition, are not managed by the GC; they are managed manually by the programmer (using the IDisposable interface).  You can use a try/finally, or a using block to ensure that dispose is always called even in the event of an exception.

    I suggest you use a profiler to see what is taking up so much memory in your program that shouldn't be.  I would also say that you just shouldn't bother if the program isn't using too much memory.  Don't prematurely optimize.

    Wednesday, April 4, 2012 2:12 PM
  • Hi Ali,

    It is better practice avoid calling GC.Collect, and its leaving GC run on its own accord and fine-tune its generation budgets based on application behavior. But in some circumstances it can be called but with GCCollectionMode as Optimized.

    How do you think there is a MemoryLeak in your case? I see you are already calling Dispose on image object.

    Is it throwing OutOfMemoryException?

    Please provide more details so that we will be able to help you..


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    Wednesday, April 4, 2012 2:34 PM
  • True  using is sugar,  but I like my code sweet not bitter.

    It also scopes the variable automatically, so it will not exist outside the using clause which you have to do if you use try finally and you cannot use var.  Consider the following two similar pieces of code.

    Image tempImage;

    try

    {

        tempImage = new Image();

    }

    finally

    {

    tempImage.Dispose();

    }

    tempImage.RunTimeError();

    versus

    using( var tempImage = new Image())

    {

    }

    tempImage.VariableUnDeclared();

    So stir my using sugar in please.

    Also consider that if you can wrap more items in using then you are going to dispose more items.


    Ta Ken

    Wednesday, April 11, 2012 5:08 AM

All replies

  • Hi Ali

    you would probably want one of the C# Forums, as this is a VB.NET Forum

    http://social.msdn.microsoft.com/Forums/en-US/category/visualcsharp


    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    • Proposed as answer by Heslacher Tuesday, April 3, 2012 5:59 AM
    • Unproposed as answer by Reed KimbleMVP Tuesday, April 3, 2012 9:19 PM
    Tuesday, April 3, 2012 5:58 AM
  • Hi HotKiller,

      You can fill image.Dispose(); into finally block if you need to use GC collection. I think you may try using try{}catch(Exception ex){}Finally{} syntax to implement it.

    Sincerely,

    Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 4, 2012 6:04 AM
    Moderator
  • You don't "use" the GC.  The GC works silently in the background and does it's thing on it's own.  Virtually anything that you do to try to mess with it will make things worse, not better.  It was designed to be left alone.

    Since you're dealing with images, it's possible (but I couldn't say without profiling the code to know for sure) that your issue is not releasing unmanaged resources.  Unmanaged resources, by definition, are not managed by the GC; they are managed manually by the programmer (using the IDisposable interface).  You can use a try/finally, or a using block to ensure that dispose is always called even in the event of an exception.

    I suggest you use a profiler to see what is taking up so much memory in your program that shouldn't be.  I would also say that you just shouldn't bother if the program isn't using too much memory.  Don't prematurely optimize.

    Wednesday, April 4, 2012 2:12 PM
  • Hi Ali,

    It is better practice avoid calling GC.Collect, and its leaving GC run on its own accord and fine-tune its generation budgets based on application behavior. But in some circumstances it can be called but with GCCollectionMode as Optimized.

    How do you think there is a MemoryLeak in your case? I see you are already calling Dispose on image object.

    Is it throwing OutOfMemoryException?

    Please provide more details so that we will be able to help you..


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    Wednesday, April 4, 2012 2:34 PM
  • Look up the using clause.

    using( var image = codecs.Load(tempFiles[i])) {...}

    The advantage is that the dispose is implicit and the use of the variable is very obvious to the compiler making it easier to optimise.

    Why sleep?   Is the placement of the sleep in the best position,  do you want to use this after you have processed the file?

    Is there a way to stream the codec (eg by page for a multipage tiff file) rather than loading the whole thing into memory.


    Ta Ken

    Wednesday, April 4, 2012 10:34 PM
  • Look up the using clause.

    using( var image = codecs.Load(tempFiles[i])) {...}

    The advantage is that the dispose is implicit and the use of the variable is very obvious to the compiler making it easier to optimise.

    Why sleep?   Is the placement of the sleep in the best position,  do you want to use this after you have processed the file?

    Is there a way to stream the codec (eg by page for a multipage tiff file) rather than loading the whole thing into memory.

    'using' is just syntactic sugar for a try/finally with a dispose in the finally.  That's really all it is...
    Thursday, April 5, 2012 1:43 PM
  • True  using is sugar,  but I like my code sweet not bitter.

    It also scopes the variable automatically, so it will not exist outside the using clause which you have to do if you use try finally and you cannot use var.  Consider the following two similar pieces of code.

    Image tempImage;

    try

    {

        tempImage = new Image();

    }

    finally

    {

    tempImage.Dispose();

    }

    tempImage.RunTimeError();

    versus

    using( var tempImage = new Image())

    {

    }

    tempImage.VariableUnDeclared();

    So stir my using sugar in please.

    Also consider that if you can wrap more items in using then you are going to dispose more items.


    Ta Ken

    Wednesday, April 11, 2012 5:08 AM
  • I use the 'using' block as well, but you made the assertion that it's easier to optimize.  Since it's syntactic sugar for the same thing, no, it's not any easier to optimize.

    As for scopes, you can add in your own scopes wherever you want.  If one is suitably motivated you can add an extra set of braces around the try/finally block with the variable declaration, that is if there's a compelling need.  'using' is nice in that regard as it makes it simpler to type out and read, and for that reason I always use it over a try/finally, but that's all it helps, typing and reading, not performance or effect.

    Wednesday, April 11, 2012 2:06 PM
  • Thanks to all guys for help me. I am very happy to see all the answers but my problem solved with out using GC.

    Thanks Again. Love you all

    Thursday, April 26, 2012 9:39 AM