Newsgroup: it.comp.lang.visual-basic
Titolo: Applicazione ciuccia RAM

Ho fatto un'applicazione in VB molto semplice, senza form, con solo 2 moduli
in cui in uno c'è il main e qualche funzione e nell'altro sono definiti i
tipi custom e delle API.
Ora questo exe, grande 30K, si ciuccia in memoria la bellezza di 4MB!! Come
mai? Ci sarebbee un modo per ridurre questa occupazione di memoria?
--------------------------------
Inviato via 
http://arianna.libero.it/usenet/

 4MB!! Come mai?
diccelo tu come mai?!?! ;-)
Senza un accenno alle funzioni come si può sapere dove si "ciuccia" la
memoria?
ciaociao

Tolto 1,6 mb di runtime ... te ne avanzano 2,4 mb il che mi
lascia pensare che mandi a puttane qualche ciclo for e che
il sistema con i tanti doevents che metti non riesce a liberarsene.
-- 
Saluti
Andrea Moro


Vuoi una lista delle API che uso?
A domada subitamente rispondo:
Declare Function GetLastError Lib "kernel32" () As Long
Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal
dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal
dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long,
Arguments As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA"
(ByVal lpApplicationName As String, ByVal lpCommandLine As String,
lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As
SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As
Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String,
lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION)
As Long
Public Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As
Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As
Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias
"GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName
As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As
String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal
lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String)
As Long
Public Declare Function GetPrivateProfileSectionNames Lib "Kernel32.dll"
Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String,
ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function RegisterWindowMessage Lib "user32" Alias
"RegisterWindowMessageA" (ByVal lpString As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As
Long
Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA"
(ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As
String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal
nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu
As Long, ByVal hInstance As Long, lpParam As Any) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal
wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest
As Any, hpvSource As Any, ByVal cbCopy As Long)
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long,
ByVal lParam As Long) As Long
Public Declare Function GetWindowPlacement Lib "user32" (ByVal hWnd As Long,
lpWndPl As WINDOWPLACEMENT) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal
nCmdShow As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As
Long) As Long
Public Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal
wCmd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As
Long, lpdwProcessId As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As
Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal
dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As
Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As
Long, lppe As PROCESSENTRY32) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal
dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As
Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As
Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess
As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long)
As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess
As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As
Long) As Long
Public Declare Function GetModuleBaseName Lib "PSAPI" Alias
"GetModuleBaseNameA" (ByValhProcess As Long, ByVal hModule As Long, ByVal
lpBaseName As String, ByVal nSize As Long) As Long
Public Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long)
As Long
Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As
Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long,
ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPosition As Long, ByVal wFlags As Long) As Long
Bastano?


A domada subitamente rispondo:
Ehm, suppongo che Marg intendesse dire "come" le usi...
-- 
ciao, hal

 Bastano?
no, in effetti mi servirebbero altri 3Mb di post per valutare.... <eg>
non sono le dichiarazioni che occupano memoria, ma quello che tu fai quando
le richiami!
Puoi anche usare una sola API ma se poi sbagli un ciclo.....



L'applicazione non fa altro che:
1. Leggere un file ini
2. Interrogare un'altra applicazione tramite SendMessage
3. Attendere una risposta usando un ciclo While Not GotAnswer DoEEvents
4. A seconda della risposta ricevuta:
   a. Enumera tutti i processi e tutte le finestre usando la EnumWindows. Una
volta trovata la finestra che gli interessa le fa una showWindow
*oppure*
   b. Lancia un'altra applicazione
   c. Attende che l'applicazione finisca usando la waitForSingleObject
Come puoi vedere uso un solo DoEvents ma ho notato che se l'applicazione
interrogata non risponde il ciclo con il doevents si ciuccia tutta la CPU.
C'č una maniera piů elegante per aspettare l'arrivo di un messaggio prima di
proseguire l'esecuzione del programma?



Ah Allora l'ho spiegato nel post più sotto... :-P


 proseguire l'esecuzione del programma?
Dipende... sel il messaggio specifico prevede la possibilita' di usare una
funzione di callback (come EnumWindows) si, altrimenti temo tu debba
mantenere il tuo ciclo di polling; questo comunque non toglie che tu possa
minimizzare drasticamente l'occupazione della CPU inserendo una piccola
pausa tra un ciclo e l'altro:
Do While Not GotAnswer
    DoEvents
    Sleep 100
Loop
il valore puoi tararlo come credi, 100ms sono comunque gia' un buon
compromesso tra consumo di CPU e prontezza di risposta del programma... in
linea di massima con questo sistema dovrebbe passare a non piu' del 10% di
occupazione processore.
Sull'occupazione memoria, con queste informazioni e' difficile dire di piu';
giusto a titolo di curiosita' mi risulta che ogni "Declare" consumi 4 byte,
che la funzione sia effettivamente usata o meno, ma certamente, anche se ne
hai piazzate molte, non arrivi a sprecare nemmeno un 1Kb solo per quelle...
Ciao
-- 
Luca Dormio - 
luca....@tin.it
________________________________
"Marty, non stai ragionando in maniera quadridimensionale"
Dr. Emmett L. Brown