Memory Consumption

Posted By: Dveyee

Memory Consumption - 12/20/12 10:11

Hi, I have a very simple snippet which dynamically creates an entity as the player approaches and also dynamically removes it once the player has moved far enough from it. My problem now is that when I open up the Statistics Panel (F11) it shows an increasing amount of memory being consumed (ie: nex, mem, geo, shd, all increase and free memory decreases) as I move the player forward. Any idea why more memory is being consumed even though theoretically the amount of memory being used is the same? Albeit, the memory consumption rate is very slow.

Here are some important snippets:
Code:
action sector_life(){
	while(1){
		if(vec_dist(player.x,my.x) > 1000){
			ptr_remove(me);
			break;
		}
		wait(1);
	}

void level_init(){
	while(player==NULL)wait(1);
	var x_count = 512; // first sector is 512 units in length
	
	// create an empty entity in front of the player
	ENTITY* limit_ent = ent_create(NULL, vector(player.x+100, player.y, player.z), NULL);
	
	while(1){
		if(vec_dist(player.x,limit_ent.x) < 100){
			ent_create("sector1.wmb",vector(x_count,0,0),sector_life);
			x_count += 512;
			limit_ent.x += 512;
		}
		wait(1);
	}
}


Many Thanks,
David
Posted By: krial057

Re: Memory Consumption - 12/20/12 11:16

I didn't look in detail. But you have to finish the while loop in level_init once(or change the if condition)... At the moment, a new entity is created every frame. Maybe you want to put the count_x in the if condition?
Posted By: Dveyee

Re: Memory Consumption - 12/20/12 19:47

Yes, a new entity is created once the distance between the invisible entity in front of the player is within 100 quants; however, the new entity is removed once the player has moved over 1000 quants away from it.
Posted By: MasterQ32

Re: Memory Consumption - 12/20/12 22:10

nope
your code creates an entity every frame if the distance to the player is < 100
you don't do any checks how many models you've created
Posted By: Dveyee

Re: Memory Consumption - 12/20/12 22:22

I think I see what you mean. Any tips on limiting the number of models created per frame?
Posted By: MasterQ32

Re: Memory Consumption - 12/20/12 22:24

EDIT: DELETE THIS!
Posted By: Uhrwerk

Re: Memory Consumption - 12/20/12 22:26

Dveyee, you don't need to reduce the number of models create per frame. At most one entity is created per frame.

MasterQ32, have you noticed that limit_ent.x is also increased when an entity is created and this variable is also used in the if comparison. At first I had the same thought like you, but I don't think this will happen here. The code is not elegant, but it should work as Dveyee describes, shouldn't it?

Dveeyee, does the number of entities rapidly increase? You can see it in the debug panel as well. It shouldn't.
Posted By: Dveyee

Re: Memory Consumption - 12/20/12 23:25

Hi Ukrwerk, no it does not rapidly increase. The initial entity count is 1 and as I move my player forward it increases up to 6 where it stabilizes (ent_create = ent_remove); however, the memory consumption still increases slowly.
Posted By: Uhrwerk

Re: Memory Consumption - 12/21/12 00:34

That clearly shows you're not creating too much entities.

Can you please be more specific about "consumption going up"? Which part is exactly going up and by what amount. Could you please provide screenshots when the game is started and when it ran for quite some time?
Posted By: Dveyee

Re: Memory Consumption - 12/21/12 01:37

Give it a try yourself! Press "W" to move forward.

You'll notice that the F11 Statistics Panel shows an increase in memory being used as the player moves forward.

memory.zip
Posted By: MasterQ32

Re: Memory Consumption - 12/21/12 10:46

okay, you found the nexus laugh
the nexus does not work like normal memory
you can allocate memory, but you can never give it back to reallocate it

so the solution for you is the reusing of your segments
just store all entities an array and shift through them
if you "remove" an entity, just set it to invisible and passable
if you use it again set it to the wanted position and show it again
Posted By: Dveyee

Re: Memory Consumption - 12/22/12 08:32

Tricky indeed! Thanks for the tip! laugh Have a Merry Christmas!
Posted By: MasterQ32

Re: Memory Consumption - 12/22/12 10:54

yeah, but the nexus is pretty fast compared against other memory allocation systems
so if you don't exceed the nexus you can get really fast results allocating memory
but only level_load will release it again
© 2024 lite-C Forums