Custom font leaking memory on device

Hi,
i have been hunting down a memory leak on android device for several days now and finally found the culprit - custom font. 

The symptoms: Corona texture and lua memory indicators do not show any issues - the values are steady, but the Active Apps widget on my device is showing constantly increasing RAM usage. My game starts with ~30MB and dies at ~250MB after a minute or two.

I have a bunch of things going on in my game: explosions (sprites and emmiters), a few GLSL shaders, lighting effects with normal maps, etc. 

So I was meticulously checking all stuff, switching it on and off… Nothing.

Then I switched off all HUD text messages (combo hits, points scored, warnings ) and the problem was gone. I reviewed my text handler and it is all good - I remove display object in transition onComplete(), then nil it.

Now by accident, after some refactoring, I lost the custom font assignment, so the default one was used instead - and guess what, RAM issue disappeared.

I whipped up a quick test project to prove my case. All resources are attached:

tap font to toggle between default and custom

tap remove text object onComplete to toggle well… spawned text object removal after transition completion

tap start to start text object spawn timer (500ms frequency)

tap stop to stop text object spawn timer 

tap clear to trash the spawned objects

Here are results of my test:

startup - device ram usage ~40MB

MEM ~400KB

TEX ~0.1MB

default font

remove text object on transition completion: true

after 100 objects:

device ram usage 24MB

MEM ~555KB

TEX ~0.096MB

after 200 objects:

device ram usage 25.13MB

MEM ~500KB

TEX ~0.096MB


custom font

remove text object on transition completion: true

after 100 objects

device ram usage 91.15MB

MEM ~378KB

TEX ~0.085MB

after 200 objects

device ram usage 138.43MB

MEM ~440KB

TEX ~0.085MB

Devices tested on:

Samsung Galaxy Note 2 - Android 4.4.2

Kata-Tmini4 - Android 6.0

I saw a possibly relevant post on SO: Memory leaks with custom font for set custom font

My questions are:

  1. Can anyone confirm or disprove the issue?
  2. Is this only affecting android?
  3. Any solution available for Corona?

Many thanks for any insights in advance.

P.S.: I am unable to attache the apl due to size restriction, here is an external link: https://nofile.io/f/nFvrxZ6Ohkn/text_ram_issue_apk_live_sync.zip

I reproduced your test using my own harness and SSK2.  There is no leak when using that font.

This zip file contains my test (edTest sub-folder) and a slightly modified version of your test: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/fontLeakCheck.zip

(I only replaced your config.lua, build.settings and got rid of all unneeded files.)

Try running my test on your device(s).

This video shows the before and after of a three minute run (recording sped up 6X for video).  

I started and stopped the test three time, once every minute in the test.

  • Main Memory Usage Before Start and at Start: 0.94 MB
  • Texture Memory Usage Before Start and at Start: 3.81 MB

https://www.youtube.com/watch?v=anQWH5tKRlI&feature=youtu.be

You can re-download the source and try it again.  I (stupidly) did NOT using your font.  :)  Re-shooting video and testing on android now.
 
Same results as last time:

  • Windows:
    • Main Memory Usage Before Start and at Start:  0.94 MB
    • Texture Memory Usage Before Start and at Start:  3.81 MB
  • Android

If I read his post correctly, he cannot detect the leak through lua or texture memory, but rather the memory usage shown in a ‘task manager’ type app.

@roaminggamer: Thanks for such prompt response. I will will look into it i detail tomorrow (it’s nearly 3am here). I just want to stress that I was specifically talking about RAM usage as reported by device , not corona texture or lua memory (via garbage collection). Will post some screenshots tomorrow and run your tests as well. Much appreciated.

My bad! Good luck on this. I’ll look again when I get a chance.

@rominggamer: I ran your tests, turns out your SSK2 setup doesn’t display the problem, modified version of my project does. See test results below.

I see that you spawn text differently, will try to figure out your code to spot the issue. 

This is getting more mysterious…  :blink:

project: modifiedOrig

default font

remove spawn onComplete: true

startup

device RAM indicator - 26.45MB

MEM 540KB

TEX0.061MB

100 iterations

RAM 29.97

MEM 511

TEX 0.056

200 iterations

RAM 31.50MB

MEM 421 KB

TEX 0.094MB


custom font

remove spawn onComplete: true

Startup

Device RAM  26.90MB

MEM 526KB

TEX 0.05MB

100 iterations

Device RAM  108.04MB

MEM 617KB

TEX 0.05MB

200 iterations

Device RAM  165.94MB

MEM 457KB

TEX 0.05MB

Also, noticeable drop in frame rate (didn’t measure, just visual stutter)


project: edTest

Startup

Device RAM  28.87MB

MEM 0.95MB

TEX 3.82MB

1 min

Device RAM  13.22MB

MEM KB

TEX MB

2 min

Device RAM  12.75MB

MEM KB

TEX MB

some more

Device RAM  12.50MB

@roaminggamer: OK, I got it - you were animating scale where I was animation size, so the text stays sharp.

I edited your code just to change that and added spawn count. Here are the results, consistent with my original findings:

Test result Project edTest modified as follows: - changed transition property from scale to size - added spawn count the values represent RAM usage as indicated by device. +----------+--------------+-------------+ |iterations| systemFont| custom font | |----------|--------------|-------------| | startup | 17.68| 18.52| |----------|--------------|-------------| | 100 | 18.99| 28.96| |----------|--------------|-------------| | 200 | 18.68| 35.88| |----------|--------------|-------------| | 300 | 19.25| 43.48| |----------|--------------|-------------| | 400 | 20.21| 54.43| |----------|--------------|-------------| | 500 | 19.27| 64.82| |----------|--------------|-------------| | 600 | 19.47| 76.48| |----------|--------------|-------------| | 700 | 19.61| 87.47| |----------|--------------|-------------| | 800 | 20.10| 95.47| |----------|--------------|-------------| | 900 | 19.46| 101.92| |----------|--------------|-------------| | 1000 | 20.07| 109.88| |----------|--------------|-------------| |lua mem | 0.9 | 0.95| |----------|--------------|-------------| |tex mem | 5.4 | 3.82| +----------+--------------+-------------+ lua memory and texture memory stats from corona stay consistent

Looks like a bug to me.

If anyone can test on other platforms, it would be great.

Attaching modified mail.lua from edTest project by roaminggamer.

I reproduced your test using my own harness and SSK2.  There is no leak when using that font.

This zip file contains my test (edTest sub-folder) and a slightly modified version of your test: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/fontLeakCheck.zip

(I only replaced your config.lua, build.settings and got rid of all unneeded files.)

Try running my test on your device(s).

This video shows the before and after of a three minute run (recording sped up 6X for video).  

I started and stopped the test three time, once every minute in the test.

  • Main Memory Usage Before Start and at Start: 0.94 MB
  • Texture Memory Usage Before Start and at Start: 3.81 MB

https://www.youtube.com/watch?v=anQWH5tKRlI&feature=youtu.be

You can re-download the source and try it again.  I (stupidly) did NOT using your font.  :)  Re-shooting video and testing on android now.
 
Same results as last time:

  • Windows:
    • Main Memory Usage Before Start and at Start:  0.94 MB
    • Texture Memory Usage Before Start and at Start:  3.81 MB
  • Android

If I read his post correctly, he cannot detect the leak through lua or texture memory, but rather the memory usage shown in a ‘task manager’ type app.

@roaminggamer: Thanks for such prompt response. I will will look into it i detail tomorrow (it’s nearly 3am here). I just want to stress that I was specifically talking about RAM usage as reported by device , not corona texture or lua memory (via garbage collection). Will post some screenshots tomorrow and run your tests as well. Much appreciated.

My bad! Good luck on this. I’ll look again when I get a chance.

@rominggamer: I ran your tests, turns out your SSK2 setup doesn’t display the problem, modified version of my project does. See test results below.

I see that you spawn text differently, will try to figure out your code to spot the issue. 

This is getting more mysterious…  :blink:

project: modifiedOrig

default font

remove spawn onComplete: true

startup

device RAM indicator - 26.45MB

MEM 540KB

TEX0.061MB

100 iterations

RAM 29.97

MEM 511

TEX 0.056

200 iterations

RAM 31.50MB

MEM 421 KB

TEX 0.094MB


custom font

remove spawn onComplete: true

Startup

Device RAM  26.90MB

MEM 526KB

TEX 0.05MB

100 iterations

Device RAM  108.04MB

MEM 617KB

TEX 0.05MB

200 iterations

Device RAM  165.94MB

MEM 457KB

TEX 0.05MB

Also, noticeable drop in frame rate (didn’t measure, just visual stutter)


project: edTest

Startup

Device RAM  28.87MB

MEM 0.95MB

TEX 3.82MB

1 min

Device RAM  13.22MB

MEM KB

TEX MB

2 min

Device RAM  12.75MB

MEM KB

TEX MB

some more

Device RAM  12.50MB

@roaminggamer: OK, I got it - you were animating scale where I was animation size, so the text stays sharp.

I edited your code just to change that and added spawn count. Here are the results, consistent with my original findings:

Test result Project edTest modified as follows: - changed transition property from scale to size - added spawn count the values represent RAM usage as indicated by device. +----------+--------------+-------------+ |iterations| systemFont| custom font | |----------|--------------|-------------| | startup | 17.68| 18.52| |----------|--------------|-------------| | 100 | 18.99| 28.96| |----------|--------------|-------------| | 200 | 18.68| 35.88| |----------|--------------|-------------| | 300 | 19.25| 43.48| |----------|--------------|-------------| | 400 | 20.21| 54.43| |----------|--------------|-------------| | 500 | 19.27| 64.82| |----------|--------------|-------------| | 600 | 19.47| 76.48| |----------|--------------|-------------| | 700 | 19.61| 87.47| |----------|--------------|-------------| | 800 | 20.10| 95.47| |----------|--------------|-------------| | 900 | 19.46| 101.92| |----------|--------------|-------------| | 1000 | 20.07| 109.88| |----------|--------------|-------------| |lua mem | 0.9 | 0.95| |----------|--------------|-------------| |tex mem | 5.4 | 3.82| +----------+--------------+-------------+ lua memory and texture memory stats from corona stay consistent

Looks like a bug to me.

If anyone can test on other platforms, it would be great.

Attaching modified mail.lua from edTest project by roaminggamer.