Adding a texture to a Sprite2d uses up 171mb of RAM

Godot Version

4.2.1.stable

Question

As stated in the title, when I add a 3840 × 3840 texture (56.25mib) to a Sprite2d, the game uses up 171.3mb more, than without the texture.

Without texture in Sprite2d (Game is the second one in the task list):
1000062244

With texture in Sprite2d (Game is the second one in the task list):
1000062246

As seen in the screenshots above, the memory went from 360.9mb to 532.2mb which is an increase of 171.3mb. The Godot editor itself had a 125.3mb increase.

I don’t really understand why this happens, deleting a Sprite2d with an even larger texture in it showed little to no change in the memory usage.

Is there anyway to reduce the memory usage?

Could be coincidence, but 171.3MB = 175,411,200 bytes.

3840x3840x8 (coz 8 bits per byte) = 117,964,800 + 56.25mb * 1024 = 175,564,800

Given potential small rounding in both 171.3 and 56.25 numbers, I would say the above two 175xxx numbers could be actually identical if we knew precise amounts instead of megabytes.

Also I know nothing about how memory works with Godot, but I figured modern colors are often 32 bit with 8 of those bits used for alpha, so hence multiplying total # of pixels by 8?

Just thinking off the top of my head here and could be entirely off-base :smiley:

PS (edit): one way to test this theory is to save your texture with alpha channel removed and see if memory usage drops by 171.3 - 56.25 = 115.05 mb. Assuming, of course, that Godot recognizes this lack of channel and optimizes for it.

PPS: @mrcdk may know, he seems far more knowledgeable about Godot.

1 Like

I’m not sure, the task manager memory monitor may be showing an approximation of the used memory and not the memory the process is using currently. I can’t remember. Here’s more info about it Memory Information in Task Manager – Pavel Yosifovich

Either way, to know how many memory a Godot app is using it’s better to use the Monitor tab in the Debugger dock and check for Memory > Static and Memory > Static Max or use the Performance singleton and check the different monitors Performance — Godot Engine (stable) documentation in English

2 Likes

Thanks for the reply! I tried monitoring the game’s usage using the method you mentioned:

Without the sprite in the game, the static was constant around 57MiB and static max was constant at 303.8MiB.

With the sprite in the game, the static steadily climbed up, from around 56MiB to 65MiB when I stopped the game, the static max was same as without the sprite, at 303.8MiB.

Any idea why the static with the sprite kept increasing?

Sorry, I’m not sure, I don’t know what your game is doing.

Whoops :sweat_smile: it turned out I had some errors in the code, I fixed it and now the game runs constantly at around 57MiB for the static monitor, but the game still lags whenever I add the sprite in though…

1 Like

Thanks to everyone who tried to help! My solution at last was to reduce the image size, as I had quite a lot of empty, unused space within it. Now the game is running a lot smoother.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.