none
Not able to free memory after calling GlobalFree() / Close Handle. Every time memory is increasing in Task Manager RRS feed

  • Question

  • Issue Type: Serious

    Problem Subject: - Not able to free memory after calling GlobalFree() / Close Handle. Every time memory is increasing in Task Manager

    Problem Description: I have written a program in C#, which work is to convert tiff file into doc in which I am using IntPtr. This work fine for single or 4 to 5 files.

    Requirement: This is exe project. Its exe name is ConvertDoc.exe when I am running this program for 5 – 6 documents, it works properly. And for fewer documents I was not able to figure out problem.

    But when I executed it for more than 8000 documents. It is having problem in Task Manager .ConvertDoc.exe is visible in process and its size is increasing after convert each file. Because of that ConvertDoc.exe has taken a lot of size in memory. And system stuck completely.

     I am not Figure out problem. I think it is because of leak of memory.

    What changes should I apply for running this program properly?

    Please see in program code when I am freeing the memory of pointers. Is this right way to do it? Problem can be because of this.

    Program Code:

     

     

    [DllImport("kernel32.dll", SetLastError = true)]

    static extern int _lopen(string lpPathName, int iReadWrite);

    [DllImport("kernel32.dll", SetLastError = true)]

    [return: MarshalAs(UnmanagedType.Bool)]

    static extern bool CloseHandle(IntPtr hObject);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]

     public static extern IntPtr GlobalFree(IntPtr handle);

         

    int iStatus = 1; 

    if (iStatus == 1)

    {

     try

        {

        int numCountTiff = 0;

          String newTiffFile = "";

          String tempTiffFileExtension = "";

          String tempTiffGetDirectoryName = "";

          String tempGetFileNameWithoutExtension = "";

          tempTiffFileExtension = Path.GetExtension(sTiffFileName).ToString();

          tempTiffFileExtension = ".doc";

        sTiffFileName = @"C:\Documents and Settings\myuser\Local Settings\Temp\WorkingFTSDirectory\656.tif";

     

    if (tempTiffFileExtension == ".tiff" || tempTiffFileExtension == ".tif")

          {

            tempTiffGetDirectoryName = Path.GetDirectoryName(sTiffFileName).ToString();

          tempGetFileNameWithoutExtensionPath.GetFileNameWithoutExtension(sTiffFileName).ToString();

            newTiffFile = tempTiffGetDirectoryName + "\\" + tempGetFileNameWithoutExtension + "s" + tempTiffFileExtension;

            int num = Tiff_GetPageIniff(sTiffFileName, ref numCountTiff);

                                                               

    for (int itiff = 1; itiff <= numCountTiff; itiff++)

    {

          LPTif6Properties lpTif6Properties = new LPTif6Properties();

          IntPtr hFile = IntPtr.Zero;

          IntPtr hTif = IntPtr.Zero;

          IntPtr hDIB = IntPtr.Zero;

          int processstoreTiff;

             try{

                      hTif = new IntPtr(_lopen(sTiffFileName, 0x00000002));

                      int tiffProperties = Tiff_GetTiffProperties(hTif, 1, 1, ref lpTif6Properties);

                      int aa = Tiff_RetrievePageTif6(hTif, itiff, ref hFile);

                      Tiff_MemTiffToDIB(hFile, ref hDIB);

                      COMPINFO CompInfo = new COMPINFO();

                      processstoreTiff = Tiff_ConvertAndStoreToDoc(hDIB, newTiffFile, (ushort)itiff, 0, ref CompInfo);

                 }

                catch (Exception ex){

                Log.writeDebugLog("", "    Compression scheme has some problem ", ex.Message);

                }

     

          finally{

          if (hDIB != IntPtr.Zero){    

          try   { GlobalFree(hDIB); }

                catch (Exception ex1) { }

                hDIB = IntPtr.Zero;    

          }

                if (hTif != IntPtr.Zero){

          try{CloseHandle(hTif);}

                catch (Exception ex1) { }

                hTif = IntPtr.Zero;

          }

                if (hFile != IntPtr.Zero){

          try{CloseHandle(hFile);}

                catch (Exception ex1){ }

                hFile = IntPtr.Zero;

                }

          }

    }// for is closing

    }                                                                      

    }

    catch (IOException ex)

    {

          Log.writeDebugLog("", "", ex.Message);

    }

                                                         

    }

     

     

     

     

     

    It is really urgent.

    Please help me out in this case. If you require any thing please tell me.

     

    I will be thankful.

     


    • Edited by S.P Singh Friday, June 8, 2012 5:29 AM
    Friday, June 8, 2012 5:01 AM

Answers

  • If you start searching on Internet you will find that your question is asked a million times.

    .Net is using managed code, the memory is released by that in the best time. If the computer would do that every nanosecond your computer would become very slow and in fact there is not one advantage on releasing memory as long as there is enough.

    Also be aware that Task manager is an enduser tool so it does not even report the correct state of the computer, it is also not bound to the computer in a way it would prevent other processes from running.


    Success
    Cor

    Friday, June 8, 2012 6:47 AM
  • You're calling GlobalFree(hDIB).  So i'm assuming that the memory is allocated by Tiff_MemTiffToDIB(hFile, ref hDIB);

    Have you read the documentation on Tiff_MemTiffToDIB to determine the correct way to free the memory it allocates?


    This signature unintentionally left blank.

    Friday, June 8, 2012 11:21 AM

All replies

  • If you start searching on Internet you will find that your question is asked a million times.

    .Net is using managed code, the memory is released by that in the best time. If the computer would do that every nanosecond your computer would become very slow and in fact there is not one advantage on releasing memory as long as there is enough.

    Also be aware that Task manager is an enduser tool so it does not even report the correct state of the computer, it is also not bound to the computer in a way it would prevent other processes from running.


    Success
    Cor

    Friday, June 8, 2012 6:47 AM
  • You're calling GlobalFree(hDIB).  So i'm assuming that the memory is allocated by Tiff_MemTiffToDIB(hFile, ref hDIB);

    Have you read the documentation on Tiff_MemTiffToDIB to determine the correct way to free the memory it allocates?


    This signature unintentionally left blank.

    Friday, June 8, 2012 11:21 AM