Memory Leak with network.request

Hi there,

I’m going through my app now it’s finished just doing some tidying up and have noticed there seems to be a memory leak occurring somewhere around my network request code… this is how I’m making my network request. Happy to post the full code if that would help.

requestId = network.request( url, “POST”, downloadListener,  params);

The request downloads some audio content into the app and saves it to the documents directory. This can sometimes be quite a bit of audio, so a lot of data is potentially being transferred.

Does anyone know what I need to do to free up any memory that is being assigned to this process. Once the download has completed, is the memory associated with this meant to be freed up automatically or are there a set of steps I need to go through to manually free up the memory?

Any tips / advice would be much appreciated.

Many thanks,

Ian

@Ian,

Does the memory jump up, fall back and stay stable around a specific amount of used memory, or does it grow forever and never come back down?

Also, can you show us the code you’re using to measure memory usage?

Hi,

Thanks for the response.

It was going up very slowly but steadily. I tried all sorts, but in the end what seems to have worked is setting some of the external classes I was requiring to nil, so where on every single network request I was setting a local variable as follows:

local crypto = require(“crypto”);

…afterwards I’m now setting it to crypto = nil;

There are several of these on every upload / download.

This in theory shouldn’t make any difference though as it’s a local variable, which I thought got wiped from memory automatically…?

Weird.

Whatever, it seems to be working now anyway. For reference, the code I was using to measure the memory usage is as follows:

function monitorMem()    collectgarbage("collect");     print( "MemUsage: " .. collectgarbage("count") );     local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000;     print( "TexMem:   " .. textMem ); end

Thanks,

Ian

Once you require a module, it’s loaded. It will not be reloaded when you require it again.  Also there is little logical reason to require a library that you’re going to use over and over multiple times. Require it at the top of the module once.

Setting crypto to nil doesn’t free up any memory.  The module is loaded and it’s reference is stored in a system level table. Your “local” version is just holding a pointer to the library.

What would be helpful is to see your downloadListener() function.

Rob

Hi Rob,

Thanks - I understand what you’re saying and I’m aware it shouldn’t have made any difference. But it all seems sorted now. All I can assume is that maybe whilst I was trying a few things I moved or reordered some code that has made a difference.

Thanks for the response and I’ll bear in mind that requiring only needs to occur once at the top rather than within a function.

Cheers.

Ian

@Ian,

Does the memory jump up, fall back and stay stable around a specific amount of used memory, or does it grow forever and never come back down?

Also, can you show us the code you’re using to measure memory usage?

Hi,

Thanks for the response.

It was going up very slowly but steadily. I tried all sorts, but in the end what seems to have worked is setting some of the external classes I was requiring to nil, so where on every single network request I was setting a local variable as follows:

local crypto = require(“crypto”);

…afterwards I’m now setting it to crypto = nil;

There are several of these on every upload / download.

This in theory shouldn’t make any difference though as it’s a local variable, which I thought got wiped from memory automatically…?

Weird.

Whatever, it seems to be working now anyway. For reference, the code I was using to measure the memory usage is as follows:

function monitorMem()    collectgarbage("collect");     print( "MemUsage: " .. collectgarbage("count") );     local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000;     print( "TexMem:   " .. textMem ); end

Thanks,

Ian

Once you require a module, it’s loaded. It will not be reloaded when you require it again.  Also there is little logical reason to require a library that you’re going to use over and over multiple times. Require it at the top of the module once.

Setting crypto to nil doesn’t free up any memory.  The module is loaded and it’s reference is stored in a system level table. Your “local” version is just holding a pointer to the library.

What would be helpful is to see your downloadListener() function.

Rob

Hi Rob,

Thanks - I understand what you’re saying and I’m aware it shouldn’t have made any difference. But it all seems sorted now. All I can assume is that maybe whilst I was trying a few things I moved or reordered some code that has made a difference.

Thanks for the response and I’ll bear in mind that requiring only needs to occur once at the top rather than within a function.

Cheers.

Ian