#1 2016-11-29 15:36:12

milos.orlic
Member
From: Serbia
Registered: 2014-03-13
Posts: 4

Growing amount of the memory used on server for PUT calls

Client is sending text file using TSQLRestClientURI.CallBackPut()

Server side does nothing at the moment, just a method for handling this call:

procedure TSxRESTExtService.ImportFromFile(Ctxt: TSQLRestServerURIContext);
begin
  Ctxt.Returns('', HTTP_SUCCESS);
end;

During run time, server application is using more and more memory, with each call, until server crashes.
If I shutdown server, no memory leak is reported by FastMM4. But, if I don't free
TSQLHttpServer, it will report (among expected things), these large blocks (they are not that big in practice, but made like this for testing):

--------------------------------2016/11/29 15:25:49--------------------------------
A memory block has been leaked. The size is: 84737892

This block was allocated by thread 0x2214, and the stack trace (return addresses) at the time was:
407162 [System.pas][System][@GetMem$qqri][4391]
40BE44 [System.pas][System][@NewAnsiString$qqrius][23974]
40C08F [System.pas][System][@LStrFromPCharLen$qqrr27System.%AnsiStringT$us$i0$%pcius][24707]
9121C6 [SynCommons.pas][SynCommons][CompressSynLZ$qqrpvo][57793]
1162504 [SynCrtSock.pas][SynCrtSock][THttpApiServer.Execute$qqrv][6396]
4D3678 [System.Classes.pas][System.Classes][Classes.ThreadProc$qqrxp22System.Classes.TThread][14161]
40BD4E [System.pas][System][ThreadWrapper$qqspv][23677]
753A7C04 [BaseThreadInitThunk]
7797AB8F [Unknown function at RtlInitializeExceptionChain]
7797AB5A [Unknown function at RtlInitializeExceptionChain]

The block is currently used for an object of class: AnsiString

The allocation number is: 19179237

Current memory dump of 256 bytes starting at pointer address 9BAC0090:
E2 04 01 00 01 00 00 00 EF 58 0C 05 42 41 53 4B 45 54 42 41 4C 4C 2C 42 41 53 4B 45 54 42 41 4C
4C 20 2D 20 42 61 6C 74 69 63 20 4C 65 61 67 75 65 2C 30 39 2E 31 31 2E 32 30 31 37 20 32 33 3A
33 30 2C 31 30 33 30 2C 58 54 56 61 6C 6D 69 65 72 61 28 2B 39 2E 35 29 2C 58 54 50 69 65 6E 6F
20 5A 2E 2C 33 2E 30 38 2C 31 35 2E 32 33 2C 31 2E 34 30 0A 42 41 53 4B 45 54 42 41 4C 4C 2C 42
41 53 4B 45 54 42 41 4C 4C 20 2D 20 42 72 61 7A 69 6C 2C 30 38 2E 31 31 2E 32 30 31 37 20 32 32
3A 30 30 2C 38 30 34 38 2C 54 4D 69 6E 61 73 28 2D 36 2E 35 29 2C 43 61 78 69 61 73 2C 31 2E 33
35 2C 31 35 2E 35 35 2C 33 2E 33 33 0A 42 41 53 4B 45 54 42 41 4C 4C 2C 42 41 53 4B 45 54 42 41
4C 4C 20 2D 20 42 72 61 7A 69 6C 2C 30 38 2E 31 31 2E 32 30 31 37 20 32 32 3A 33 30 2C 38 30 34
â  .  .  .  .  .  .  .  ď  X  .  .  B  A  S  K  E  T  B  A  L  L  ,  B  A  S  K  E  T  B  A  L
L     -     B  a  l  t  i  c     L  e  a  g  u  e  ,  0  9  .  1  1  .  2  0  1  7     2  3  :
3  0  ,  1  0  3  0  ,  X  T  V  a  l  m  i  e  r  a  (  +  9  .  5  )  ,  X  T  P  i  e  n  o
   Z  .  ,  3  .  0  8  ,  1  5  .  2  3  ,  1  .  4  0  .  B  A  S  K  E  T  B  A  L  L  ,  B
A  S  K  E  T  B  A  L  L     -     B  r  a  z  i  l  ,  0  8  .  1  1  .  2  0  1  7     2  2
:  0  0  ,  8  0  4  8  ,  T  M  i  n  a  s  (  -  6  .  5  )  ,  C  a  x  i  a  s  ,  1  .  3
5  ,  1  5  .  5  5  ,  3  .  3  3  .  B  A  S  K  E  T  B  A  L  L  ,  B  A  S  K  E  T  B  A
L  L     -     B  r  a  z  i  l  ,  0  8  .  1  1  .  2  0  1  7     2  2  :  3  0  ,  8  0  4

This blocks are presenting data sent from client, as I can see from the dump.
I don't have idea what to check next.

Offline

#2 2016-11-29 17:18:43

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,232
Website

Re: Growing amount of the memory used on server for PUT calls

Impossible to reproduce the issue here.

Please provide some sample code - NOT IN THE FORUM, but as a downloadable .zip in DropBox (or alike).

Offline

#3 2016-11-30 11:37:06

milos.orlic
Member
From: Serbia
Registered: 2014-03-13
Posts: 4

Re: Growing amount of the memory used on server for PUT calls

I have made as much as possible example to be like the code in a real enivroment.
If you start server, then client, it will start to consume memory until "out of memory" pops out.
Latest night build of mORMot was used.

https://drive.google.com/file/d/0B7EK_9 … sp=sharing

I have left in the zip FastMM_FullDebugMode.dll. I can remove it if it's a problem.

Maybe the custom authentication class is a problem, but, I can't see relation.

Offline

#4 2016-11-30 13:20:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,232
Website

Re: Growing amount of the memory used on server for PUT calls

If I disable authentication in your program, or use TSQLRestServerAuthenticationDefault, there is no memory leak.

So the memory leak is definitively in your custom authentication scheme.
Also I don't understand why you implemented TPxURIParams, whereas I think you have everything you need in TSQLRestServerURIContext.
There is no need either to define TSQLAuthUserId.SetID and so on: just use TSQLRecord.IDValue!

Offline

#5 2016-11-30 14:57:23

milos.orlic
Member
From: Serbia
Registered: 2014-03-13
Posts: 4

Re: Growing amount of the memory used on server for PUT calls

Thanks for fast response. Yes, you are correct about SetID and params, it's some legacy issue at this moment. I have changed example (new link below), custom authentication is excluded and other custom classes except class for service method, everything is on minimum, memory out still happening.

https://drive.google.com/file/d/0B7EK_9 … sp=sharing

Last edited by milos.orlic (2016-11-30 14:57:43)

Offline

#6 2016-11-30 17:10:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,232
Website

Re: Growing amount of the memory used on server for PUT calls

I have found something.
It fact, it was not a memory leak (and FastMM4 did not report it), but the memory was kept after transmission, and only reset when the thread received its next request to process.
As a result, the previous memory buffers was kept up to 32 times (i.e. the thread pool size), up to a memory overflow.

I've ensured http.sys server will release its input/output buffers ASAP.
The behavior can't be seen any more.
See http://synopse.info/fossil/info/c9f5e082c4

Offline

#7 2016-11-30 17:34:28

milos.orlic
Member
From: Serbia
Registered: 2014-03-13
Posts: 4

Re: Growing amount of the memory used on server for PUT calls

Thanks for the help, with this change it's OK now.

Offline

#8 2016-11-30 21:02:43

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,542
Website

Re: Growing amount of the memory used on server for PUT calls

Very good improvement! It help me also. Thanks

Offline

Board footer

Powered by FluxBB