none
Is it possible to use Garbage Collection to release all memory use to run a process when it finish? RRS feed

  • Question

  • Hi guys

     

    i was given a task which require me to release the memory use to run a particular process whenever it finish and the aid that i got is to use garbage collection to solve it. I have try using garbage collection but from what i know garbage collection is able to reclaim only memory that is not managed and not reclaim all the memory use. However i did try putting garbage collection in my code but i not quite sure whether i have code it correctly. Anyone can show me an example or can help me check whether i have code it correctly? thank in advance=D

     

    Start button code for MyDocumentUploader

     

    private void Start_Click(object sender, System.EventArgs e)

    {

    //sym files location

    Config.SourceDir = SourceLoc.Text;

    //extract files location

    Config.ExtractDir = ExtractLoc.Text;

    //working location

    Config.WorkingDir = WorkingLoc.Text;

    PreProcess();

    GC.Collect(GC.MaxGeneration);

    return;

    }

    Tuesday, January 22, 2008 1:11 AM

Answers

  •  

    Hi,

     

    "I have try using garbage collection but from what i know garbage collection is able to reclaim only memory that is not managed and not reclaim all the memory use"

     

    The GC will not collect your unmanaged memory, it knows nothing about that memory, hence it is unmanaged :-)  This memory will only be reclaimed as a side effect of a GC occurring if you have an object that explicitly cleans up the unmanaged memory in its finalizer.

     

    Why exactly do you have to decrease the memory usage?  If the CLR feels that there is memory pressure in the system then it will run a garbage collection.  If you want to clean up unmanaged resources deterministically (Rather than waiting until an unknown time for the GC to kick in) then you can implement the IDispose pattern and put the resource cleanup code in the associated Dispose() method.

     

     

    Mark.

    Tuesday, January 22, 2008 1:58 AM

All replies

  •  

    Hi,

     

    "I have try using garbage collection but from what i know garbage collection is able to reclaim only memory that is not managed and not reclaim all the memory use"

     

    The GC will not collect your unmanaged memory, it knows nothing about that memory, hence it is unmanaged :-)  This memory will only be reclaimed as a side effect of a GC occurring if you have an object that explicitly cleans up the unmanaged memory in its finalizer.

     

    Why exactly do you have to decrease the memory usage?  If the CLR feels that there is memory pressure in the system then it will run a garbage collection.  If you want to clean up unmanaged resources deterministically (Rather than waiting until an unknown time for the GC to kick in) then you can implement the IDispose pattern and put the resource cleanup code in the associated Dispose() method.

     

     

    Mark.

    Tuesday, January 22, 2008 1:58 AM
  • hi thank mark=D ya i did use finalizer and implement dispose method=) thank alot

    Tuesday, January 22, 2008 5:19 AM