Jump to content

Is there a way to free up memory being used?


Recommended Posts

I experience a similar problem as user MDCT.

I created an app using mostly standard scripts to look for PDF files in a directory and print them to the default printer.

The app also uses the default PDF application (Foxit Reader in my case).

The problem is that after each loop the app builds up memory.

Is there a way to free up the memory or is there something that I can do differently in the code?

PC Specs of two similar PCs I tested on:

Windows XP (Service Pack 3)

Intel Pentium 4, 3.00GHz, 1GB RAM

$i = 1
While $i > 0

Sleep(1000)

If FileExists("*.pdf") Then
 
  ; [This message can later be logged in a text file]
  ; MsgBox(4096, "PDF File(s)", "PDF Files Exists")
 
  ; [Shows the filenames of all files in the current directory.]
  $search = FileFindFirstFile('*.pdf') 
 
  ; [Check if the search was successful]
  If $search = -1 Then
   ; [This message can later be logged in a text file]
   ; MsgBox(0, "Error", "No PDF files were found")
   ; Exit
  EndIf
 
  While 1
   ; [Searches for the next PDF file]
   $file = FileFindNextFile($search)
  
   If @error Then ExitLoop
   ; [This message can later be logged in a text file]
   ; MsgBox(4096, "File:", "C:\PDFPrintSpool\" & $file)
  
   ; [Assign the Full path of the PDF file to variable]
   $PDFFileURL = "C:\PDFPrintSpool\"& $file
  
   ; [Prints the PDF file when found]
   shellexecuteWait($PDFFileURL,"","","print")
  
   ; [Call to close Foxit Reader]
   ProcessClose("Foxit Reader.exe")
  
   ; [Delete the PDF file that was last printed]
   FileDelete($PDFFileURL)
  WEnd
 
  ; [Close the search handle]
  FileClose($search)
 
  Else
  ; [This message can later be logged in a text file]
  ; MsgBox(4096,"Error", "No PDF files were found")

EndIf

Sleep(1000)

WEnd
Link to comment
Share on other sites

A work-around fix for the problem, I created. (Yoda!)

Another app that runs as a loop (a.k.a. my app service) calling the original Auto PDF printer app.

No stealing of memory, anymore. Yay! :D

(Still would like to know for future reference how to free up memory used by AutoIt... :oops: )

Link to comment
Share on other sites

Yes. You can free the RAM by using this:

Func _ReduceMemory($i_PID = -1)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1F0FFF, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    Return $ai_Return[0]
EndFunc   ;==>_ReduceMemory

Just use it _ReduceMemory() to reduce the current script, you can use it on other processes.

However, it will just hide a problem if there's a memory leak.

Edit: Oops, Mat beat me to it.

Edited by MDCT
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...