Recent

Author Topic: Memory is not freed after response in FPweb embedded server  (Read 2002 times)

alantelles

  • New Member
  • *
  • Posts: 21
Good afternoon (or any time it is).

I'm developing an application that makes use of FPweb embedded server. So I decided to look at the system manager to monitoring memory usage while using the application. And I realized that after the response the memory usage didn't fall back to the level it was before request. And the memory usage increase after every request. Is this the default behavior? Is there a way to cleanup memory after response?
# compulsive coder
FPC 3.2.0, Lazarus 2.0.10

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Memory is not freed after response in FPweb embedded server
« Reply #1 on: July 13, 2020, 07:05:41 am »
Well, if you are using CGI module, then check the Kind property of TFPWebModule. If it is wkPooled, change it to wkOneShot and monitor again.

I'm concerned with this issue quite much.  Hope to know your result.

alantelles

  • New Member
  • *
  • Posts: 21
Re: Memory is not freed after response in FPweb embedded server
« Reply #2 on: July 13, 2020, 04:07:12 pm »
I,m using FPWeb embedded server only without graphical component. Here's a sample

For configuring server

Code: Pascal  [Select][+][-]
  1. constructor TUltraGenServer.Create(APort: word; AnApp: string; Mode:string);
  2. {... lots of code ...}
  3.   HTTPRouter.RegisterRoute('/favicon.ico', @LoadFavIcon);
  4.   HTTPRouter.RegisterRoute('*', @ExecuteAction);
  5.  

This route is registered to '*' because a script will actually control the routing.

For start the server

Code: Pascal  [Select][+][-]
  1. function TUltraGenServer.RunServer:boolean;
  2. begin
  3.   if not FDontServe then
  4.   begin
  5.     WriteLn('Running server at port: ', FPort);
  6.     Application.Title := 'UltraGen server 1.0';
  7.     Application.Port := FPort;
  8.     Application.Threaded := True;
  9.     Application.Initialize;
  10.     Application.Run;
  11.     Result := True;
  12.   end
  13.   else
  14.     Result := False;
  15. end;


Don't know if I can do the test with FCGI module due to the behaviour of FCGI apps.
# compulsive coder
FPC 3.2.0, Lazarus 2.0.10

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Memory is not freed after response in FPweb embedded server
« Reply #3 on: July 16, 2020, 12:00:37 pm »
Since you're writing your own server app using the embedded server, we cannot see how you manage whatever comes in and out of the embedded server. One thing I can assure you, this program:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   classes, sysutils, webutil, fphttpapp, httproute, httpdefs;
  5.  
  6. procedure handle(req: TRequest; resp: TResponse);
  7. var
  8.   s: tstrings;
  9. begin
  10.   s := TStringList.Create;
  11.   with s do
  12.     try
  13.       DumpRequest(req,s,true);
  14.       resp.Content := s.Text;
  15.     finally
  16.       s.Free;
  17.     end;
  18. end;
  19.  
  20. begin
  21.   Application.Port := 9090;
  22.   HTTPRouter.RegisterRoute('*',@handle);
  23.   Application.Run;
  24. end.
  25.  
leaks no memory, called 10000 times the RAM usage stays at 4MB.

alantelles

  • New Member
  • *
  • Posts: 21
Re: Memory is not freed after response in FPweb embedded server
« Reply #4 on: July 19, 2020, 04:20:53 pm »
At first: honoured for being replied by you.

About the question: that's the reply I waited for being in peace about this. I'll review my application to see the points where I'm not freeing memory. In fact, my app uses a lot of dinamically create objects and it's getting hard to track them to free memory in a secure way without cause an access Violation. Since it doesn't do heavy things it's not a problem now but in many requests, it is.
# compulsive coder
FPC 3.2.0, Lazarus 2.0.10

alantelles

  • New Member
  • *
  • Posts: 21
Re: Memory is not freed after response in FPweb embedded server
« Reply #5 on: July 19, 2020, 04:27:27 pm »
Since you're writing your own server app using the embedded server, we cannot see how you manage whatever comes in and out of the embedded server.

Code: Pascal  [Select][+][-]
  1.  AInter := TInterpreter.Create(ATree);
  2.  
  3.     AInter.Interpret;
  4.     Output := AInter.PLive;
  5.     //AInter.FreeInstances;
  6.     AInter.Free;
  7.     AParser.Free;
  8.     ALexer.Free;
  9.     AResponse.Content := Output;
  10.     WriteLn(#13+'['+FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now)+'] ' +
  11.       ARequest.Method + ': '+
  12.       ARequest.URI+' -- '+ IntToStr(AResponse.Code)+
  13.       ' ' + AResponse.CodeText +
  14.       ', ' + IntToStr(AResponse.ContentLength) + ' B', #13);
  15.  
  16.  

The main section, who creates and free the elements of response is this. I thought that after the response it would clear itself. By the way, I really got to review my interpreter code.
# compulsive coder
FPC 3.2.0, Lazarus 2.0.10

alantelles

  • New Member
  • *
  • Posts: 21
Re: Memory is not freed after response in FPweb embedded server
« Reply #6 on: July 19, 2020, 11:00:06 pm »
In time: @leledumbo, I visited your GitHub and saw the template engineering and parsers seem to be a subject of your interest. Would you like to contribute at UltraGen project? I'm developing a brand new programming language specifically designed to create web pages. It can use plain and script text mixed like PHP but with modern approaches and strongly typed. Maybe you can find it interesting. Here is the link

https://github.com/alantelles/ultragen/tree/refactor

Greetings
# compulsive coder
FPC 3.2.0, Lazarus 2.0.10

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Memory is not freed after response in FPweb embedded server
« Reply #7 on: July 20, 2020, 08:49:30 am »
Code: Pascal  [Select][+][-]
  1.  AInter := TInterpreter.Create(ATree);
  2.  
  3.     AInter.Interpret;
  4.     Output := AInter.PLive;
  5.     //AInter.FreeInstances;
  6.     AInter.Free;
  7.     AParser.Free;
  8.     ALexer.Free;
  9.     AResponse.Content := Output;
  10.     WriteLn(#13+'['+FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now)+'] ' +
  11.       ARequest.Method + ': '+
  12.       ARequest.URI+' -- '+ IntToStr(AResponse.Code)+
  13.       ' ' + AResponse.CodeText +
  14.       ', ' + IntToStr(AResponse.ContentLength) + ' B', #13);
  15.  
The main section, who creates and free the elements of response is this. I thought that after the response it would clear itself. By the way, I really got to review my interpreter code.
I don't see ATree being Free-d, and if it eventually is, no guarantee it will free its children recursively as well. I'd say compile with -glh and you will get a proper stacktrace. AResponse, if it comes as HTTPRouter route's parameter, will be handled outside your registered handler, so no need to free it manually (in fact, don't).
In time: @leledumbo, I visited your GitHub and saw the template engineering and parsers seem to be a subject of your interest. Would you like to contribute at UltraGen project? I'm developing a brand new programming language specifically designed to create web pages. It can use plain and script text mixed like PHP but with modern approaches and strongly typed. Maybe you can find it interesting. Here is the link

https://github.com/alantelles/ultragen/tree/refactor

Greetings
I consider that one kind of shitty, as it was made with classic single person programming in mind. Nowadays we made frontend totally separate from backend, which is glued by HTTP contract.
I'm sorry I don't have time for such a project. Outside my main job, I only make things that I consider useful for me and only then I share it with the world.

alantelles

  • New Member
  • *
  • Posts: 21
Re: Memory is not freed after response in FPweb embedded server
« Reply #8 on: July 20, 2020, 02:22:37 pm »
Thanks for reply. It's a pity I can't have you at the project but your answers contributed to me somehow. I'll review the destructors to clean all objects. About the project, anytime you change your mind a bit feel invited.
# compulsive coder
FPC 3.2.0, Lazarus 2.0.10

 

TinyPortal © 2005-2018