Page 1 of 1

Is there anyway to recover the memory that requesters eat up

Posted: Fri Jan 04, 2013 8:15 pm
by Nituvious
So, I have a program that I thought had a memory leak. I narrowed it down and found that it wasn't actually any of my programs code that was causing the problem but the open and save requesters that I used! Any way to clean up the used memory? It uses a surprisingly large amount of ram.

Code: Select all

Structure PROCESS_MEMORY_COUNTERS
	
  cb.i
  PageFaultCount.i
  PeakWorkingSetSize.i
  WorkingSetSize.i
  QuotaPeakPagedPoolUsage.i
  QuotaPagedPoolUsage.i
  QuotaPeakNonPagedPoolUsage.i
  QuotaNonPagedPoolUsage.i
  PagefileUsage.i
  PeakPagefileUsage.i
	
EndStructure

Prototype.l pGetProcessMemoryInfo(process.l, psmemCounters, cb.l)

Global lPsapi = OpenLibrary(#PB_Any, "Psapi.dll")

If lPsapi

	Global GetProcesMemoryInfo_.pGetProcessMemoryInfo = GetFunction(lPsapi, "GetProcessMemoryInfo")

EndIf

Procedure.f GetMemoryUsage(type.l)
	
	Define pMem.PROCESS_MEMORY_COUNTERS
	Define.f ret
	
	GetProcesMemoryInfo_(GetCurrentProcess_(), @pMem, SizeOf(PROCESS_MEMORY_COUNTERS))
	
	Select type.l

		Case 0 ; in kilobytes
			
			ret = (pMem\WorkingSetSize / 1024)
			
		Default ; in bytes

			ret = pMem\WorkingSetSize

	EndSelect
	
	ProcedureReturn ret
	
EndProcedure

#MAX_PASS = 10

final.s = "Starting Memory: " + Str(GetMemoryUsage(0)) + "KB" + #LF$

For x = 1 To #MAX_PASS
	OpenFileRequester("", "", "", 0)
; 	InputRequester("", "", "")
; 	PathRequester("", "")
; 	SaveFileRequester("", "", "", 0)

	final.s + "Memory @ pass " + Str(x) + ": " +  Str(GetMemoryUsage(0)) + "KB" + #LF$
	
Next x

MessageRequester("", final.s)
[edit] removed an array

Re: Is there anyway to recover the memory that requesters ea

Posted: Fri Jan 04, 2013 8:22 pm
by IdeasVacuum
...I think the most obvious solution is to DIY - you can have better defined requesters that exactly fit the needs of your app and you have full control of memory usage.

Re: Is there anyway to recover the memory that requesters ea

Posted: Fri Jan 04, 2013 8:31 pm
by Nituvious
:evil:
I guess so...

Re: Is there anyway to recover the memory that requesters ea

Posted: Fri Jan 04, 2013 9:05 pm
by Fred
PB requesters don't allocate extra memory and are thin wrapper to native WinAPI one. Try with the API and you will experience the same, it's because the requester instanciate a whole 'explorer' as a control, with all explorer intergration plugins activated (like TortoiseSVN etc.)

Re: Is there anyway to recover the memory that requesters ea

Posted: Fri Jan 04, 2013 9:28 pm
by Nituvious
Good to know, I suppose. :P

Re: Is there anyway to recover the memory that requesters ea

Posted: Fri Jan 04, 2013 11:16 pm
by luis

Re: Is there anyway to recover the memory that requesters ea

Posted: Sat Jan 05, 2013 10:13 am
by xorc1zt
there is no memory leak. GetOpenFileName (winapi) load a lot a dlls and also create a fair amount a of threads and kernel objects which are not freed. most of them are needed for the thumbnails (ex: load the appropriate codecs for the videos.)

Code: Select all

Import "Kernel32.lib"
    GetProcessHandleCount(a.i, *b)
EndImport
Define handle_count.i
Define handle_process.i = GetCurrentProcess_()
Define handle_tsnap.i = CreateToolhelp32Snapshot_(#TH32CS_SNAPTHREAD, 0)
Define id_process.l = GetCurrentProcessId_()
Define counter_thread.i
te.THREADENTRY32
te\dwSize = SizeOf(te)
Thread32First_(handle_tsnap, @te)
If (te\th32OwnerProcessID = id_process)
        counter_thread = counter_thread + 1
EndIf
While (Thread32Next_(handle_tsnap, @te))
    If (te\th32OwnerProcessID = id_process)
        counter_thread = counter_thread + 1
    EndIf
    
Wend
CloseHandle_(handle_tsnap)
GetProcessHandleCount(handle_process, @handle_count)
Debug "Threads: " + Str(counter_thread)
Debug "kernel objects: " + Str(handle_count)
OpenFileRequester("", "", "", 0)
Debug "-OpenFileRequester-"
counter_thread = 0
handle_tsnap = CreateToolhelp32Snapshot_(#TH32CS_SNAPTHREAD, 0)
Thread32First_(handle_tsnap, @te)
If (te\th32OwnerProcessID = id_process)
        counter_thread = counter_thread + 1
EndIf
While (Thread32Next_(handle_tsnap, @te))
    If (te\th32OwnerProcessID = id_process)
        counter_thread = counter_thread + 1

    EndIf
    
Wend
GetProcessHandleCount(handle_process, @handle_count)
Debug "Threads: " + Str(counter_thread)
Debug "kernel objects: " + Str(handle_count)
output on w8 x64

Code: Select all

Waiting for executable to start...
Executable type: Windows - x64  (64bit, Unicode)
Executable started.
[Debug] Threads: 5
[Debug] kernel objects: 63
[Debug] -OpenFileRequester-
[Debug] Threads: 29
[Debug] kernel objects: 357
The Program execution has finished.
dll loaded on GetOpenFileName call

Code: Select all

ModLoad: 000007fa`8bb40000 000007fa`8bbd6000   C:\Windows\SYSTEM32\clbcatq.dll
ModLoad: 000007fa`89050000 000007fa`89113000   C:\Windows\system32\OLEAUT32.dll
ModLoad: 000007fa`7dc60000 000007fa`7dcf5000   C:\Program Files\Common Files\microsoft shared\ink\tiptsf.dll
ModLoad: 000007fa`819f0000 000007fa`81cca000   C:\Windows\system32\explorerframe.dll
ModLoad: 000007fa`89ed0000 000007fa`89fae000   C:\Windows\system32\ADVAPI32.dll
ModLoad: 000007fa`86350000 000007fa`86407000   C:\Windows\system32\DUser.dll
ModLoad: 000007fa`87040000 000007fa`871f9000   C:\Windows\system32\DUI70.dll
ModLoad: 000007fa`86c00000 000007fa`86d8a000   C:\Windows\SYSTEM32\WindowsCodecs.dll
ModLoad: 000007fa`88b10000 000007fa`88b25000   C:\Windows\SYSTEM32\profapi.dll
ModLoad: 000007fa`7de40000 000007fa`7e0fc000   C:\Windows\SYSTEM32\MsftEdit.dll
ModLoad: 000007fa`7dd50000 000007fa`7de3c000   C:\Windows\System32\Windows.Globalization.dll
ModLoad: 000007fa`86210000 000007fa`86271000   C:\Windows\System32\Bcp47Langs.dll
ModLoad: 000007fa`84060000 000007fa`841ba000   C:\Windows\SYSTEM32\PROPSYS.dll
ModLoad: 000007fa`89bb0000 000007fa`89d80000   C:\Windows\system32\SETUPAPI.dll
ModLoad: 000007fa`88be0000 000007fa`88c2f000   C:\Windows\system32\CFGMGR32.dll
ModLoad: 000007fa`88de0000 000007fa`88e02000   C:\Windows\system32\DEVOBJ.dll
ModLoad: 000007fa`846e0000 000007fa`8471f000   C:\Windows\SYSTEM32\XmlLite.dll
ModLoad: 000007fa`85520000 000007fa`8552b000   C:\Windows\SYSTEM32\LINKINFO.dll
ModLoad: 000007fa`883c0000 000007fa`883da000   C:\Windows\SYSTEM32\CRYPTSP.dll
ModLoad: 000007fa`88040000 000007fa`88089000   C:\Windows\system32\rsaenh.dll
ModLoad: 000007fa`7dae0000 000007fa`7dbc0000   C:\Windows\system32\SearchFolder.dll
ModLoad: 000007fa`7dbc0000 000007fa`7dc52000   C:\Windows\System32\StructuredQuery.dll
ModLoad: 000007fa`7f4f0000 000007fa`7f4fb000   C:\Windows\System32\Secur32.dll
ModLoad: 000007fa`888b0000 000007fa`888dc000   C:\Windows\System32\SSPICLI.DLL
ModLoad: 000007fa`85630000 000007fa`8564c000   C:\Windows\system32\mssprxy.dll
ModLoad: 000007fa`84210000 000007fa`84420000   C:\Windows\System32\actxprxy.dll
ModLoad: 000007fa`7f570000 000007fa`7f5a5000   C:\Windows\System32\thumbcache.dll
ModLoad: 000007fa`83750000 000007fa`837ef000   C:\Windows\SYSTEM32\apphelp.dll
ModLoad: 000007fa`72b70000 000007fa`72c2b000   C:\Windows\SYSTEM32\ntshrui.dll
ModLoad: 000007fa`88810000 000007fa`88834000   C:\Windows\SYSTEM32\srvcli.dll
ModLoad: 000007fa`80280000 000007fa`80291000   C:\Windows\SYSTEM32\cscapi.dll
ModLoad: 000007fa`6f120000 000007fa`6f1da000   C:\Windows\system32\mssvp.dll
ModLoad: 000007fa`70880000 000007fa`7089d000   C:\Windows\system32\MAPI32.dll
ModLoad: 000007fa`85f00000 000007fa`85f39000   C:\Windows\System32\shdocvw.dll
ModLoad: 000007fa`70540000 000007fa`706db000   C:\Windows\system32\NetworkExplorer.dll
ModLoad: 000007fa`80d30000 000007fa`80de0000   C:\Windows\System32\twinapi.dll
ModLoad: 000007fa`82280000 000007fa`82297000   C:\Windows\SYSTEM32\MPR.dll
ModLoad: 000007fa`82d50000 000007fa`82d5a000   C:\Windows\System32\drprov.dll
ModLoad: 000007fa`88910000 000007fa`8895e000   C:\Windows\System32\WINSTA.dll
ModLoad: 000007fa`826c0000 000007fa`826d3000   C:\Windows\System32\ntlanman.dll
ModLoad: 000007fa`80c20000 000007fa`80c3e000   C:\Windows\System32\davclnt.dll
ModLoad: 000007fa`829a0000 000007fa`829ab000   C:\Windows\System32\DAVHLPR.dll
ModLoad: 000007fa`86db0000 000007fa`86dc5000   C:\Windows\SYSTEM32\wkscli.dll
ModLoad: 000007fa`87e20000 000007fa`87e2e000   C:\Windows\SYSTEM32\netutils.dll
ModLoad: 000007fa`829e0000 000007fa`82a55000   C:\Windows\System32\dlnashext.dll
ModLoad: 000007fa`709b0000 000007fa`70af4000   C:\Windows\System32\Windows.Media.Streaming.dll
ModLoad: 000007fa`88980000 000007fa`889c3000   C:\Windows\System32\POWRPROF.dll
ModLoad: 000007fa`89180000 000007fa`891d8000   C:\Windows\system32\WS2_32.dll
ModLoad: 000007fa`89400000 000007fa`89409000   C:\Windows\system32\NSI.dll
ModLoad: 000007fa`841f0000 000007fa`84200000   C:\Windows\System32\DevDispItemProvider.dll
ModLoad: 000007fa`7c6b0000 000007fa`7c6e6000   C:\Windows\System32\EhStorShell.dll
ModLoad: 000007fa`82fc0000 000007fa`8305f000   C:\Windows\System32\cscui.dll
ModLoad: 000007fa`85a50000 000007fa`85a5b000   C:\Windows\System32\CSCDLL.dll
ModLoad: 000007fa`6ef20000 000007fa`6f118000   C:\Windows\system32\wpdshext.dll
ModLoad: 000007fa`8a2d0000 000007fa`8a470000   C:\Windows\WinSxS\amd64_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.9200.16384_none_72771d4ecc1c3a4d\gdiplus.dll
ModLoad: 000007fa`828d0000 000007fa`8296b000   C:\Windows\System32\PortableDeviceApi.dll
ModLoad: 000007fa`88e10000 000007fa`88e65000   C:\Windows\system32\WINTRUST.dll
ModLoad: 000007fa`88e70000 000007fa`89047000   C:\Windows\system32\CRYPT32.dll
ModLoad: 000007fa`88bc0000 000007fa`88bd6000   C:\Windows\system32\MSASN1.dll
ModLoad: 000007fa`809c0000 000007fa`809f6000   C:\Windows\System32\PortableDeviceTypes.dll
ModLoad: 000007fa`80be0000 000007fa`80c05000   C:\Windows\System32\EhStorAPI.dll
ModLoad: 000007fa`888f0000 000007fa`88901000   C:\Windows\System32\WTSAPI32.dll

Re: Is there anyway to recover the memory that requesters ea

Posted: Sun Jan 06, 2013 1:21 am
by electrochrisso
xorc1zt wrote:there is no memory leak. GetOpenFileName (winapi) load a lot a dlls and also create a fair amount a of threads and kernel objects which are not freed. most of them are needed for the thumbnails (ex: load the appropriate codecs for the videos.)
If I run the code again, it seems the threads and objects are freed, as it starts back at 5 threads and about 52 objects, every time I run, on the system I am using.

Re: Is there anyway to recover the memory that requesters ea

Posted: Sun Jan 06, 2013 9:38 am
by xorc1zt
each time your run the code you start a new process. this code get infos from the current process so you cannot keep data from the previous run which was from a another process.

from msdn :
When a process terminates, the system automatically closes handles and deletes objects created by the process. However, when a thread terminates, the system usually does not close handles or delete objects. The only exceptions are window, hook, window position, and dynamic data exchange (DDE) conversation objects; these objects are destroyed when the creating thread terminates.
http://msdn.microsoft.com/en-us/library ... 85%29.aspx