HandleURL Memory leak

Posting here before opening a bug request in case I missed something.

I have several web apps running 24/7 used only with their handleurl events as API. I have a memory leak with HandleURL in a Web project in Xojo 2023r4.

To test:

  • Create a new web application
  • In the HandleURL event, place the following code:
'use static to have the entire code in one page without any dependencies to properties
static NumRequests as integer
static initialObjectCount as integer
Static initialMemoryUsed as integer

'initialize
if initialObjectCount = 0 then initialObjectCount = Runtime.ObjectCount
if initialMemoryUsed = 0 then initialMemoryUsed = Runtime.MemoryUsed

'increment number of requests
NumRequests = NumRequests + 1

'build the page
var thePage as string
thePage = "<html><head><title>test</title></head><body>"
thePage = thePage + "Num requests : " +  NumRequests.ToString + "<br>"
thePage = thePage + "Object count : " +  Runtime.ObjectCount.ToString + "<br>"
thePage = thePage + "Memory usage : " +  Runtime.MemoryUsed.ToString + "<br>"
thePage = thePage + "<br>"
thePage = thePage + "Object count variation : " + format(Runtime.ObjectCount - initialObjectCount,"-0") + "<br>"
thePage = thePage + "Memory usage variation : " +  format(Runtime.MemoryUsed - initialMemoryUsed,"-0") + "<br>"
thePage = thePage + "</body></html>"

response.Status = 200
response.Write thePage
return true

Here are my statistics, running in debug mode on MacBook Pro M2 MacOS 14.1.2:

after 1 request:
Object count : 6803
Memory usage : 9443216

Num requests : 500
Object count : 14183
Memory usage : 22877824

Num requests : 1000
Object count : 21682
Memory usage : 22817472

In comparison, 2023r2.1 also had leaks, but a lot less:

Num requests : 1
Object count : 6568
Memory usage : 9153968

Num requests : 500
Object count : 7442
Memory usage : 14587216

Num requests : 1000
Object count : 8442
Memory usage : 15803920

For comparison, I made a small desktop project using ServerSocket and this code in the tcpsocket connected event:

'use static to have the entire code in one page without any dependencies to properties
static NumRequests as integer
static initialObjectCount as integer
Static initialMemoryUsed as integer

'initialize
if initialObjectCount = 0 then initialObjectCount = Runtime.ObjectCount
if initialMemoryUsed = 0 then initialMemoryUsed = Runtime.MemoryUsed

'increment number of requests
NumRequests = NumRequests + 1


'build the page
var pagelines() as string
pagelines.add "<html><head><title>test</title></head><body>"
pagelines.add "Num requests : " +  NumRequests.ToString
pagelines.add "Object count : " +  Runtime.ObjectCount.ToString
pagelines.add "Memory usage : " +  Runtime.MemoryUsed.ToString
pagelines.add "<br>"
pagelines.add "Object count variation : " + format(Runtime.ObjectCount - initialObjectCount,"-0")
pagelines.add "Memory usage variation : " +  format(Runtime.MemoryUsed - initialMemoryUsed,"-0")
pagelines.add "</body></html>"

var page as string = join(pagelines,"<br>")

'build the header
var headerlines() as string
headerlines.Add "HTTP/1.1 200 OK"
headerlines.Add "Content-Type: text/html"
headerlines.Add "Date: Sun, 07 Jan 2024 19:36:53 GMT"
headerlines.Add "X-Content-Type-Options: nosniff"
headerlines.Add "X-Frame-Options: SAMEORIGIN"
headerlines.Add "Content-Length: " + page.Length.ToString

var header as string = join(headerlines,&u0D+&u0A)

var fullpage as string = header + &u0D+&u0A + &u0D+&u0A + page

'write all
me.Write fullpage
me.Flush

And here are the stats for this:

Num requests : 1
Object count : 98
Memory usage : 8199920

Num requests : 500
Object count : 54
Memory usage : 11687712

Num requests : 1000
Object count : 54
Memory usage : 11676992

Let me know if you have any insight.

… just in case people would say the memory usage stabilizes between 500 and 1000, here are the results after 3000 queries:

Num requests : 3000
Object count : 51682
Memory usage : 26752336

So it keeps growing to infinity and beyond :slight_smile:

Just a side note, this should be enough:

// Static values are created AND initialized just once
Static initialObjectCount As Integer = Runtime.ObjectCount
Static initialMemoryUsed As Integer = Runtime.MemoryUsed

:+1: thanks. I’ll improve the code in the project I’ll include in the bug report.

2 Likes

If it isn’t being cleaned automatically after a few mins of inactivity, then there is definitely something wrong.

Have you created the Issue already?

Thanks Bruno!

Hello Ricardo. No I haven’t created an issue yet. It definitely does not get cleaned up. I have instances of my main api that grow up to several hundreds of megabytes after a few weeks of running and I need to restart them.

Here’s the data of my api app:

At Startup (after a few queries received(

  • ObjectCount = 7362
  • MemoryUsed = 8318528

After about 20 hours:

  • ObjectCount = 332980
  • MemoryUsed = 81367584

Might be related to this issue
https://tracker.xojo.com/xojoinc/xojo/-/issues/74998

I have to restart the webApp every 3-4 days or else it uses all memory (8GB) available on the server.

It has to be something different, in your case it was happening when loading a normal WebPage. In this case it seems to affect the HandleURL event.

It would be really nice to catch this one and fix it as part of 2024r1.

1 Like

Could be. In my case, in only use the HandleURL event. (Ricardo beat me to it :slight_smile:

Should I go ahead and open a bug? (I like to ask before opening tickets)

2 Likes

Yes, please. Thank you!

Issue created # [75302] (https://tracker.xojo.com/xojoinc/xojo/-/issues/75302)

2 Likes

The path of a temporary file was being kept in memory for 2 hours for doing some auto-cleanup. In this case, we were already cleaning up the file as soon as it wasn’t needed, so the path won’t be kept for that long anymore.

This will be fixed in 2024r1. Thanks a lot for reporting it!

7 Likes

That was awesome fast !!! Thank you so much. So this means that the memory usage should have reached a plateau at some point. Let me check this for a few days, I’ll get back with my findings.

I am converting a big webservices project to web2.
Any estimate for release date of 2024r1?

Yep. Before April.

2 Likes