I'm coding a tcp server on Debian and I'm hunting memory "leak". I create one thread per connected client and I could have 300 clients simultaneous.
I would like to know if I can trust the RES column (htop for example or task manager) indicating the memory usage of my program.
In fact, when the client thread cleanly closes, the memory usage doesn't decrease.
Here is a sample with threads and multiple declared structures inside :
Code: Select all
Structure Person ;(208 bytes)
a.i
b.i
c.i
d.i
e.i
f.i
g.i
h.i
i.i
j.i
k.i
l.i
m.i
n.i
o.i
p.i
q.i
r.i
s.i
t.i
u.i
v.i
w.i
x.i
y.i
z.i
EndStructure
ImportC ""
fopen(filename.p-utf8, mode.p-ascii)
fscanf(stream.i, format.i, size.i)
fclose(fp.i)
getpagesize()
EndImport
Global SC_PAGESIZE = getpagesize()
Procedure GetRSS()
Protected rss = 0
Protected fp = fopen("/proc/self/statm", "r")
If fp = #Null
ProcedureReturn -1
EndIf
Protected format.s = "%*s%ld"
Protected *buff = AllocateMemory(StringByteLength(format, #PB_Ascii))
PokeS(*buff, format, StringByteLength(format, #PB_Ascii), #PB_Ascii)
Protected ret = fscanf(fp, *buff, @rss)
FreeMemory(*buff)
If ret <> 1
fclose(fp)
ProcedureReturn -2
EndIf
fclose(fp)
ProcedureReturn rss * SC_PAGESIZE / 1024 ; ko
EndProcedure
Procedure Thread(val)
Protected mystruct1.Person
Protected mystruct2.Person
Protected mystruct3.Person
Protected mystruct4.Person
Protected mystruct5.Person
Debug "rss="+GetRSS()
Delay(10000)
EndProcedure
; main
Delay(2000)
Debug "first rss="+GetRSS()
For i = 1 To 20
thid = CreateThread(@Thread(), 1)
Delay(500)
Next
Debug "20 threads created"
WaitThread(thid)
Debug "last rss="+GetRSS()
Thanks.