HTML crashes due to (SOLVED) : (PANIC: unprotected error in call to Lua API (not enough memory))

I’m creating alot of objects and then deleting them. Should I use a pooling system instead?

local function what_to_spawn(self)
	self.fruits = {
		{ path = "/assets/objects/bacteria/bacteria_1.goc", preview = "bacteria_1" },
		{ path = "/assets/objects/bacteria/bacteria_2.goc", preview = "bacteria_2" },
		{ path = "/assets/objects/bacteria/bacteria_3.goc", preview = "bacteria_3" },
		{ path = "/assets/objects/bacteria/bacteria_4.goc", preview = "bacteria_4" },
		{ path = "/assets/objects/bacteria/bacteria_5.goc", preview = "bacteria_5" },
		{ path = "/assets/objects/bacteria/bacteria_6.goc", preview = "bacteria_6" },
		{ path = "/assets/objects/bacteria/bacteria_7.goc", preview = "bacteria_7" },
		{ path = "/assets/objects/bacteria/bacteria_8.goc", preview = "bacteria_8" },
		{ path = "/assets/objects/bacteria/bacteria_9.goc", preview = "bacteria_9" }
		
	}

	local random_index = math.random(1, #self.fruits)
	local fruit = self.fruits[random_index]
	print("random_index".. random_index)
	self.current_fruit = fruit  -- save for later spawn

	msg.post("GUI", "send_fruit_preview", { preview = fruit.preview })

	-- Prepare the factory for later creation
	-- factory.unload("factory_go#factory")
	factory.set_prototype("factory_go#factory", fruit.path)
end

function init(self)
	msg.post(".", "acquire_input_focus")
	what_to_spawn(self)
end

local function spawn_random(self)
	if not self.current_fruit then
		return
	end
	local position = go.get_position("mover")
	-- factory.create(component, p, nil, { score = 10 }, 2.0) -- <1>
	
	self.enemy_id = factory.create("factory_go#factory", position + vmath.vector3(0, -20, 0), nil, nil, 1.5)

	-- Immediately pick the next one to preview
	what_to_spawn(self)
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		spawn_random(self)
		sound.play("audio#instance_sound")
	end
end

local has_check_memory = true

function update(self, dt)
	if has_check_memory then
		has_check_memory = false
	timer.delay(0.5, false, function()

		print("Lua memory:", collectgarbage("count"), "KB")
		has_check_memory = true
		end)
	end
end

How many?

HTML crashes due to : (PANIC: unprotected error in call to Lua API (not enough memory))

Increase the heap count:

Defold does pooling in the engine. It is not recommended to add another pooling system on top of it.

1 Like

For the experiment, please stop using this function, try to use separate factory for the each ‘.go’ file. Will there be an error?*

* (this is generally how it should be done, and factory.set_prototype is, in my opinion, intended for completely different purposes)

2 Likes

They add up to like 50-70 sprites with rigidbody with collider

1 Like

That is a quite low number of sprites, so that shouldn’t be a problem.

We rarely see the “panic” type of issues, so it would be good if you could report this issue on github and also attach a small repro case, with repro steps, to help us test the issue.

1 Like

i instantiate 1 per click and then delete when it collides with another gameobject of the same group but after creating 100s of them and deleting on collision(but the total in-game remains at about 50 gameobjects) for a certain amount of time then it crashes at a certain point saying it ran out of memory in console of google chrome

I’ll do some profiling quickly

Can anyone help? I can’t replicate on my pc the memory remains stable (I played for a couple of minutes and nothing went wrong). I tried again google and it failed but heap remained under my heap size set in html settings


This is the error that happened in my browser.

Heap size in html:256mb

Error Log.txt (20.8 KB)


still getting error when I increase heap size to 512

I create a issue

What could be causing this increase in heap?


What version of Defold are you using?

1.10.1. I thought that latest version but i see there has been an update but i don’t know if it’ll fix but I check but i don’t see anything fixes related to html

It sometimes goes up to 1.9 gb heap

As you’re getting a Lua error, also check for Lua related fixes.Or other memory related fxes etc..
Best is to just try 1.10.3 and see if it helps.

1 Like

It was fixed in 1.10.2 - Memory leak in empty HTML5 project over time · Issue #10613 · defold/defold · GitHub

In Defold, it is important to always use the latest version. There is no point in using older versions.

2 Likes

ok problem solved. I just needed to update my version to 1.10.3 Thanks

1 Like