Liberar memoria ao fechar Formulario com objeto criado
Enviado: 28 Set 2022 09:41
Num resumo simples: sei lá qual opção tem a ver com Harbour ou com sistema operacional.
Em homenagem a Paulo Cesar Toledo
https://pctoledo.org/
Código: Selecionar todos
#include "hbmemory.ch"
#define MEM_TEST HB_MEM_VM
#pragma -w0
PROCEDURE Main
LOCAL nCont
SetMode(40,100)
CLS
? "Main: " + Ltrim( Str( Memory(MEM_TEST) ) )
FOR nCont = 1 TO 3
?
? "Main before subtest: " + Ltrim( Str( Memory(MEM_TEST) ) )
SubTest()
? "Main after subtest: " + Ltrim( Str( Memory(MEM_TEST) ) )
NEXT
?
? "Main after for/next: " + Ltrim( Str( Memory(MEM_TEST) ) )
Inkey(0)
RETURN
FUNCTION SubTest()
LOCAL aList, F
aList := {}
? "Before array: " + Ltrim( Str( Memory(MEM_TEST) ) )
FOR F := 1 TO 1000000
AAdd( aList, { F, Hb_RandomInt(99) , hb_RandomInt(99), Date() - F, F % 2 == 0, hb_RandomInt(99), F * 10 } )
NEXT
? "After array: " + Ltrim( Str( Memory(MEM_TEST) ) )
aList := {}
? "Empty List: " + Ltrim( Str( Memory(MEM_TEST) ) )
aList := Nil
? "Nil: " + Ltrim( Str( Memory(MEM_TEST )) )
RETURN Nil
Código: Selecionar todos
#include "hbmemory.ch"
#define MEM_TEST HB_MEM_VM
#pragma -w0
PROCEDURE Main
LOCAL nCont, nM
SetMode(40,100)
CLS
nM := Memory( MEM_TEST )
? "Main: " + Ltrim( Str( nM ) )
FOR nCont = 1 TO 4
?
? "Main before subtest: " + ShowMemory( nM )
SubTest( nM )
? "Main after subtest: " + ShowMemory( nM )
NEXT
?
? "Main after for/next: " + ShowMemory( nM )
Inkey(0)
RETURN
FUNCTION SubTest( nM )
LOCAL aList := {}, F
? "Before array: " + ShowMemory( nM )
FOR F := 1 TO 1000000
AAdd( aList, { F, Hb_RandomInt(99) , hb_RandomInt(99), Date() - F, F % 2 == 0, hb_RandomInt(99), F * 10 } )
NEXT
? "After array: " + ShowMemory( nM )
StoreValue( @aList, {} )
? "Empty List: " + ShowMemory( nM )
StoreValue( @aList, {}, .T. )
? "Nil: " + ShowMemory( nM )
RETURN Nil
FUNCTION StoreValue( xVar, xValue, lNil )
IF lNil != Nil .AND. lNil
xVar := Nil
ENDIF
xVar := xValue
RETURN Nil
FUNCTION ShowMemory( nM )
RETURN Ltrim( Str( nM - Memory( MEM_TEST ) ) ) + " in use"
Para NÃO ficar linguição tem que DESmarcar essa caixa.JoséQuintas escreveu:Nota: O copiar/colar do console no windows 10 tá uma bost... vira um linguição.
Código: Selecionar todos
#include "MiniGui.ch"
//#include "Hmg.ch"
//---------------------------------------------------------
Function MAIN
//---------------------------------------------------------
Public nMemoriaInicial:=ltrim(transform(Memory(0),"999 999 999 999 999"))
Public cMensagem:="Data: "+Dtoc(date())+" Hora: "+Time()+ " Mem. Inicial antes dos Forms -> "+nMemoriaInicial+" Kb."+Hb_Eol()
Public aLista:={}, lineFeed:=Hb_Eol() // +Hb_Eol()
DEFINE WINDOW Form_Main;
TITLE "Form_Main" MAIN WIDTH 850 HEIGHT 300 ;
ON GOTFOCUS Atualiza_botao()
@ 145,10 LABEL Lbl_1 WIDTH 170 HEIGHT 25 SIZE 12 VALUE "Quant. de elementos"
@ 145, 185 textBox Get_1 WIDTH 100 HEIGHT 25 value "1000000" On Enter (Form_Main.btn01.action)
@ 135,320 button btn01 caption "Gerar array - (Memoria Inicial = "+nMemoriaInicial +"Kb.)" Width 480 Height 50 action CriaArray()
END WINDOW
ON KEY ESCAPE of Form_Main ACTION Form_Main.Release
ACTIVATE WINDOW Form_Main
RETURN
//---------------------------------------------------------
Function CriaArray()
//---------------------------------------------------------
Local cForm:="FORM_"+ALLTRIM(SUBST(Arq_Temporario(),3,10))
Local F, nVoltas:=3, nMemoria
DECLARE WINDOW &(cForm)
DEFINE WINDOW &cForm width 740 height 650 Title "cForm = "+cForm;
ON INIT Atualiza_Botao();
ON RELEASE Atualiza_Botao()
@ 15, 20 LABEL Lbl_Memoria FontColor BLUE WIDTH 720 HEIGHT 500 SIZE 12;
VALUE cMensagem
END WINDOW
cMensagem:=cMensagem+"Memoria apos criacao do Form_Main e do "+cForm+" -> "+ltrim(transform(Memory(0),"999 999 999 999 999"))+" Kb."+Hb_Eol()
FOR F=1 TO nVoltas
Preenche_Array(.T., cForm, nVoltas) // .T. = Preenche .F. = Zera ela
Preenche_Array(.F., cForm, nVoltas) // .T. = Preenche .F. = Zera ela
NEXT
&(cForm).Lbl_Memoria.value:=GetProperty(cForm,"Lbl_Memoria","Value")+LineFeed+ ;
"Antes da execucao de 'hb_gcAll()' -> Memoria = "+ltrim(transform(Memory(0),"999 999 999 999 999"))+" Kb."
DO EVENTS
hb_gcAll()
DO EVENTS
wApi_Sleep(500)
nMemoria=Memory(0)
&(cForm).Lbl_Memoria.value:=GetProperty(cForm,"Lbl_Memoria","Value")+LineFeed+ ;
"Apos a execucao de 'hb_gcAll()' -> Memoria = "+ltrim(transform(Memory(0),"999 999 999 999 999"))+" Kb."
// ATREPL("abc",cVariavel,"123") Troca os abc por 123 na variavel mencionada
&(cForm).Lbl_Memoria.value:=GetProperty(cForm,"Lbl_Memoria","Value")+LineFeed+Hb_Eol()+ ;
"Diferenca de memoria = "+ltrim(transform( Val(ATREPL(" ",nMemoriaInicial,""))-nMemoria ,"999 999 999 999 999"))+" Kb."+Hb_Eol()+;
"Compilador : "+Hb_Compiler()+Hb_Eol()+;
"Harbour : "+Version()+Hb_Eol()+;
"Sistema : "+Os() +" - "+IF(hb_osIs64bit(),"64 Bits","32 Bits")
cMensagem="Data: "+Dtoc(date())+" Hora: "+Time()+ " Mem. Inicial antes dos Forms -> "+nMemoriaInicial+" Kb."+Hb_Eol()
@ 520,420 button btn02 parent &cForm caption "Copiar p/ ClipBoard" Width 120 Height 30 action System.Clipboard := GetProperty(cForm,"Lbl_Memoria","Value") // CopyToClipboard(GetProperty(cForm,"Lbl_Memoria","Value"))
ON KEY ESCAPE OF &cForm ACTION &(cForm).Release
ACTIVATE WINDOW &cForm
RETURN
//---------------------------------------------------------
//---------------------------------------------------------
Function Preenche_Array(lPreencher, cForm, nVoltas)
//---------------------------------------------------------
Static nVezes:=1, cTimeInicial
Local cString:="", nQuantElem:= Val(GetProperty("Form_Main","Get_1","value"))
IF lPreencher
Atualiza_Botao()
WAIT WINDOW "Adicionando "+transform(nQuantElem, '99 999 999 999')+" elementos em aLista{}. Etapa "+Alltrim(str(nVezes))+"/"+alltrim(str(nVoltas)) NOWAIT
cString=alltrim(Str(nVezes))+" - Antes de criar aLista com "+transform(nQuantElem, '99 999 999 999')+" elementos: Memoria="+ltrim(transform(Memory(0),"999 999 999 999 999"))+" Kb."
cMensagem=cMensagem+LineFeed+cString
cTimeInicial=Time()
FOR F := 1 TO nQuantElem
AAdd( aLista, { F, Hb_RandomInt(99) , RandStr( 10 ), Date() - F, if( F % 2 == 0, .T., .F. ), RandStr( 10 ), F*10 } )
NEXT
WAIT CLEAR
cString=alltrim(Str(nVezes))+" - APOS criar aLista com "+transform(nQuantElem, '99 999 999 999')+" elementos: Memoria="+ltrim(transform(Memory(0),"999 999 999 999 999"))+" Kb."
cMensagem=cMensagem+LineFeed+cString+" Tempo: "+ElapTime(cTimeInicial,Time())
ELSE
Release aLista
Public aLista:={}
cString=alltrim(Str(nVezes))+" - Apos Release, e nova criacao de 'aLista' com 'Zero' elementos: Memoria="+ltrim(transform(Memory(0),"999 999 999 999 999"))+" Kb."
cMensagem=cMensagem+LineFeed+cString+Hb_Eol()
nVezes=IF(nVezes=3,1,nVezes+1)
ENDIF
&(cForm).Lbl_Memoria.value:=cMensagem+Hb_Eol()
Atualiza_Botao()
Return
//---------------------------------------------------------
//---------------------------------------------------------
Function Atualiza_Botao()
//---------------------------------------------------------
SetProperty("Form_Main","btn01","caption","Gerar Array ( Memoria Inicial = "+nMemoriaInicial+" Memoria Atual = "+ltrim(transform(Memory(0),"999 999 999 999 999"))+" Kb.")
Return NIL
//---------------------------------------------------------
//---------------------------------------------------------
FUNCTION RandStr( nLen )
//---------------------------------------------------------
LOCAL cSet := "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
LOCAL cPass := ""
LOCAL i
FOR i := 1 TO nLen
cPass += SubStr( cSet, Random( 52 ), 1 )
NEXT
RETURN cPass
//-----------------------------------------------------------
//-----------------------------------------------------------
FUNCTION Arq_Temporario() // Gera nome de arquivo temporario
//-----------------------------------------------------------
RETURN "Y_"+ALLTRIM(RIGHT(NETNAME(),2))+ ALLTRIM( STR(INT(SECOND())) )
//-----------------------------------------------------------
Código: Selecionar todos
Data: 09/28/22 Hora: 17:42:50 Mem. Inicial antes dos Forms -> 1 311 260 Kb.
Memoria apos criacao do Form_Main e do FORM_5063781 -> 1 313 504 Kb.
1 - Antes de criar aLista com 5 000 000 elementos: Memoria=1 313 504 Kb.
1 - APOS criar aLista com 5 000 000 elementos: Memoria=212 916 Kb. Tempo: 00:01:40
1 - Apos Release, e nova criacao de 'aLista' com 'Zero' elementos: Memoria=327 892 Kb.
2 - Antes de criar aLista com 5 000 000 elementos: Memoria=328 660 Kb.
2 - APOS criar aLista com 5 000 000 elementos: Memoria=293 012 Kb. Tempo: 00:01:37
2 - Apos Release, e nova criacao de 'aLista' com 'Zero' elementos: Memoria=406 432 Kb.
3 - Antes de criar aLista com 5 000 000 elementos: Memoria=660 088 Kb.
3 - APOS criar aLista com 5 000 000 elementos: Memoria=497 992 Kb. Tempo: 00:01:38
3 - Apos Release, e nova criacao de 'aLista' com 'Zero' elementos: Memoria=625 016 Kb.
Antes da execucao de 'hb_gcAll()' -> Memoria = 625 016 Kb.
Apos a execucao de 'hb_gcAll()' -> Memoria = 612 492 Kb.
Diferenca de memoria = 698 768 Kb.
Compilador : Borland C++ 5.8.2 (32-bit)
Harbour : Harbour 3.2.0dev (r2104281802)
Sistema : Windows 10 10.0 - 64 Bits
Código: Selecionar todos
Data: 09/29/22 Hora: 07:53:22 Mem. Inicial antes dos Forms -> 1 175 912 Kb.
Memoria apos criacao do Form_Main e do FORM_5028410 -> 1 170 888 Kb.
1 - Antes de criar aLista com 5 000 000 elementos: Memoria=1 170 888 Kb.
1 - APOS criar aLista com 5 000 000 elementos: Memoria=112 580 Kb. Tempo: 00:07:16
1 - Apos Release, e nova criacao de 'aLista' com 'Zero' elementos: Memoria=718 160 Kb.
2 - Antes de criar aLista com 5 000 000 elementos: Memoria=729 056 Kb.
2 - APOS criar aLista com 5 000 000 elementos: Memoria=199 056 Kb. Tempo: 00:07:03
2 - Apos Release, e nova criacao de 'aLista' com 'Zero' elementos: Memoria=783 232 Kb.
3 - Antes de criar aLista com 5 000 000 elementos: Memoria=815 904 Kb.
3 - APOS criar aLista com 5 000 000 elementos: Memoria=187 820 Kb. Tempo: 00:07:00
3 - Apos Release, e nova criacao de 'aLista' com 'Zero' elementos: Memoria=667 132 Kb.
Antes da execucao de 'hb_gcAll()' -> Memoria = 667 724 Kb.
Apos a execucao de 'hb_gcAll()' -> Memoria = 669 692 Kb.
Diferenca de memoria = 506 412 Kb.
Compilador : MinGW GNU C 7.3 (32-bit)
Harbour : Harbour 3.2.0dev (r2011030937)
Sistema : Windows 10 10.0 - 64 Bits