HMG_Upper() function causes a memory leak

Moderator: Rathinagiri

User avatar
srvet_claudio
Posts: 2044
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 35 times
Been thanked: 146 times
Contact:

Re: HMG_Upper() function causes a memory leak

Post by srvet_claudio » Thu Sep 22, 2016 2:48 pm

Rathinagiri wrote:
serge_girard wrote:Hello,

What I meant is :

Is it necessary to put a 'RELEASE MEMORY' on (via TIMER) in a program ?
OR
should it be done automatically in HMG-sources
OR
Is it a temporarily problem?

I think only Claudio or Rathi knows the right answer?

Regards, Serge
IMHO, the memory resources once not needed shall be released automatically.

For some memory hungry programs (with huge image manipulation), I think we shall use release memory.
+1

Also it is not convenient to be constantly releasing memory because it increases the memory pages failures.

If there are real memory leaks could do something like:

Code: Select all


DEFINE TIMER TimerMemoryTest  OF MainWin  INTERVAL 5000  ACTION ReleaseMemoryTest()


PROCEDURE ReleaseMemoryTest
STATIC IniMem := 0
LOCAL  EndMem
   IF IniMem == 0
      IniMem := GetProcessMemoryInfo()[3]
   ENDIF
   EndMem := GetProcessMemoryInfo()[3]
   #define MEM_LIMIT  3 // for example 3 times the initial amount of memory used
   IF (EndMem / IniMem) > MEM_LIMIT
      RELEASE MEMORY
      IniMem := GetProcessMemoryInfo()[3]
   ENDIF 
RETURN

Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

KDJ
Posts: 231
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland
Been thanked: 264 times

Post by KDJ » Thu Sep 22, 2016 7:41 pm

On Win-XP, as I mentioned earlier, after using HMG_Upper() function, "RELEASE MEMORY" does not work.
There is no any effect, memory usage continues to grow.

User avatar
srvet_claudio
Posts: 2044
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 35 times
Been thanked: 146 times
Contact:

Post by srvet_claudio » Thu Sep 22, 2016 9:42 pm

KDJ wrote:On Win-XP, as I mentioned earlier, after using HMG_Upper() function, "RELEASE MEMORY" does not work.
There is no any effect, memory usage continues to grow.
I will check.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Thu Sep 22, 2016 9:53 pm

I've tested with following code:

Code: Select all

#include 'hmg.ch'

FUNCTION Main()
SET FONT TO 'MS Shell Dlg', 8

  DEFINE WINDOW MUT_WA;
    COL    200;
    ROW    100;
    WIDTH  340;
    HEIGHT 120;
    TITLE  'Memory usage test';
    MAIN
	
   ON KEY SHIFT+M ACTION UpdateStatus()

    DEFINE BUTTON Test_BU
      COL      10
      ROW      20
      WIDTH   150
      HEIGHT   25
      CAPTION 'Test: HMG_Upper()'
      ACTION  DoTest()
    END BUTTON

    DEFINE BUTTON Exit_BU
      COL     170
      ROW      20
      WIDTH   150
      HEIGHT   25
      CAPTION 'Exit'
      ACTION  MUT_WA.RELEASE
    END BUTTON

    DEFINE STATUSBAR
      STATUSITEM ''
    END STATUSBAR

    ON KEY ESCAPE ACTION MUT_WA.RELEASE

  END WINDOW //MUT_WA

  // UpdateStatus()
  MUT_WA.ACTIVATE

RETURN NIL


FUNCTION DoTest()
  LOCAL n
  LOCAL cString := 'abcdefgh'

  FOR n := 1 TO 10000
       HMG_Upper(cString)
       // Upper(cString)
  NEXT

  UpdateStatus()

RETURN NIL


FUNCTION UpdateStatus()
MUT_WA.STATUSBAR.Item(1) := 'Memory usage: ' + LTrim(Str(GetProcessMemoryInfo()[3] / 1024, 10, 0) + ' KB')
RETURN NIL
I've removed TIMER just to isolate the problem and replaced with Shift+M to watch memory usage. And in the fact there is difference between HMG_Upper() and Upper().

The difference is around 620kb (always increasing) and Upper() quite always increases and sometimes release memory itself.

Tested on Windows 7 32 bits, Win XP (under VM) and in Windows 10 32 bits when displaying memory thru Shit-M always change memory consuption even Upper or HMG_Upper have not been actioned. So, propably some wrong reading in GetProcessMemoryInfo function ? :?

Rgds
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
andyglezl
Posts: 1179
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Has thanked: 36 times
Been thanked: 83 times
Contact:

Post by andyglezl » Fri Sep 23, 2016 2:42 pm

Hola

Ahora que se están mencionando problemas con la funcion HMG_Upper(),
he estado relacionando eso, con los problemas aleatorios que he tenido en cuanto al manejo
de arreglos con la función HMG_LEN(), por ejemplo:

nFrase := HMG_LEN( aFrases )

En ocasiones me da "error de acceso al array" sin haber encontrado el porqué.
Tratare de hacer un ejemplo para determinar si es eso.
-----------------------------------------------------------------------------------------------------------------
Hello

Now that being mentioned problems HMG_Upper() function,
I've been relating that, with random problems I've had in the management
arrangements with HMG_LEN() function, for example:

nFrase: = HMG_LEN (aFrases)

Sometimes I get "Error accessing array" without finding why.
Try to make an example to determine if that.
Andrés González López
Desde Guadalajara, Jalisco. México.

KDJ
Posts: 231
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland
Been thanked: 264 times

Post by KDJ » Fri Sep 23, 2016 8:09 pm

I improved the program for memory usage test with HMG_Upper() function:
- added Upper() test,
- added MY_Upper() function and its test,
- added info about execution time.

Code: Select all

// Press 'Test: ...' and observe memory usage at status bar

#include 'hmg.ch'


FUNCTION Main()

  SET FONT TO 'MS Shell Dlg', 8

  DEFINE WINDOW MUT_WA;
    COL    200;
    ROW    100;
    WIDTH  180;
    HEIGHT 210;
    TITLE  'Memory usage test';
    MAIN;
    MINBUTTON .F.;
    MAXBUTTON .F.

    DEFINE BUTTON Test_BU1
      COL      10
      ROW      20
      WIDTH   150
      HEIGHT   25
      CAPTION 'Test: HMG_Upper()'
      ACTION  DoTest(1, 'HMG_Upper()')
    END BUTTON

    DEFINE BUTTON Test_BU2
      COL      10
      ROW      50
      WIDTH   150
      HEIGHT   25
      CAPTION 'Test: Upper()'
      ACTION  DoTest(2, 'Upper()')
    END BUTTON

    DEFINE BUTTON Test_BU3
      COL      10
      ROW      80
      WIDTH   150
      HEIGHT   25
      CAPTION 'Test: MY_Upper()'
      ACTION  DoTest(3, 'MY_Upper()')
    END BUTTON

    DEFINE BUTTON Exit_BU
      COL      10
      ROW     110
      WIDTH   150
      HEIGHT   25
      CAPTION 'Exit'
      ACTION  MUT_WA.RELEASE
    END BUTTON

    DEFINE STATUSBAR
      STATUSITEM ''
    END STATUSBAR

    DEFINE TIMER MUT_WA_TI;
      INTERVAL 1000;
      ACTION   UpdateStatus()

    ON KEY ESCAPE ACTION MUT_WA.RELEASE

  END WINDOW //MUT_WA

  UpdateStatus()
  MUT_WA.ACTIVATE

RETURN NIL


FUNCTION DoTest(nTest, cTitle)
  LOCAL cLowerStr := 'abcdefghijklmnop'
  LOCAL cUpperStr
  LOCAL nSeconds
  LOCAL cTime
  LOCAL n

  nSeconds := Seconds()

  SWITCH nTest
    CASE 1
      FOR n := 1 TO 100000
        cUpperStr := HMG_Upper(cLowerStr)
      NEXT
      EXIT
    CASE 2
      FOR n := 1 TO 100000
        cUpperStr := Upper(cLowerStr)
      NEXT
      EXIT
    CASE 3
      FOR n := 1 TO 100000
        cUpperStr := MY_Upper(cLowerStr)
      NEXT
  ENDSWITCH

  cTime := Str((Seconds() - nSeconds) * 1000, 5)

  UpdateStatus()
  MsgBox('Lower: ' + cLowerStr + CRLF + 'Upper: ' + cUpperStr + CRLF + 'Time: ' + cTime + ' ms', cTitle)

RETURN NIL


FUNCTION UpdateStatus()

  MUT_WA.STATUSBAR.Item(1) := 'Memory usage: ' + LTrim(Str(GetProcessMemoryInfo()[3] / 1024, 10, 0) + ' KB')

  //RELEASE MEMORY does not work in Win-XP, in Win-7 works
  //RELEASE MEMORY

RETURN NIL


FUNCTION MY_Upper(cString)

  IF ! (cString == '')
    HMG_CallDLL('User32.dll', 0, 'CharUpper', @cString)
  ENDIF

RETURN cString
Conclusions from the test:
- HMG_Upper() each time increases memory usage (on Win-XP "RELEASE MEMORY" is ineffective). Upper() and MY_Upper() works properly.
- Upper() does not support Unicode. HMG_Upper() and MY_Upper() supports Unicode and ANSI.
- Upper() is the fastest, HMG_Upper() is about 3 times slower, MY_Upper() is about 18 times slower.

Why HMG_Upper() function is important in the context of memory usage?
Because it is used in the functions GetProperty(), SetProperty() and DoMethod().
It seems to me that in these cases, it can be replaced with Upper().

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Wed Sep 28, 2016 12:47 pm

andyglezl wrote:problemas aleatorios que he tenido en cuanto al manejo
de arreglos con la función HMG_LEN(), por ejemplo:

nFrase := HMG_LEN( aFrases )

En ocasiones me da "error de acceso al array" sin haber encontrado el porqué.
Tratare de hacer un ejemplo para determinar si es eso.
Hola Andrés,

Yo tengo como costumbre de usar el Len() en lugar de HMG_Len() para casos de retorno de tamaños en ARRAYs.

Para casos de STRINGs en UNICODE, veo que HMG_Len, funciona bastante bien y no tengo problemas con liberacion de memória de esa funcion. Por lo menos consume poco y libera algo.

Ya con hb_BLen() en esta ultima version de Harbour [3.2.0dev (r1601151502)], hay errores y consume más memória. Y como siempre acompaño el ChangeLog del Harbour y hay bugs corregidos. Fijate en: viewtopic.php?f=7&t=4199&p=46667#p46667
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
andyglezl
Posts: 1179
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Has thanked: 36 times
Been thanked: 83 times
Contact:

Post by andyglezl » Wed Sep 28, 2016 1:32 pm

Gracias Pablo Cesar

Si, he estado haciendo pruebas con eso precisamente, cambiando por Len().
Andrés González López
Desde Guadalajara, Jalisco. México.

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Fri Sep 30, 2016 6:18 pm

Si es UNICODE Andrés, utilizá hb_ULen
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Wed Oct 05, 2016 12:20 pm

There are also some more "Harbour native functions" that could be tested:

hb_UPadL(), hb_UPadR() and hb_UPadC()

In my tests the replacement of these above mentioned function by HMG_ functins are few differences and it in redundance in my opinion.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

Post Reply