• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

[SOLVED] Lag with ds_grids

Niften

Member
I have an object that loads a ds_grid, and if I leave the game on for more than around 2 minutes I start getting lag spikes.

Any ideas?
 
Are you only doing it once?

Are you writing it back as well as loading?

You said its loading a ds_grid, so I'm assuming this is one you've saved to the hard disk. If you are repeatedly saving it while a game is running, then I think (but am not 100% sure) that would slow down the step time.

Reading and writing files to the hard drive isn't a very quick operation, so doing it often will cause things to chug. But I might be wrong o_O

EDIT:
People will no doubt find it easier to help verify your problem, if you include your code and how / where you're using it.

The above is just a rough guess....
 

NightFrost

Member
You say it loads a ds grid... does it do it every step? Does it create a new ds every time it loads? If so, you have a classic memory leak. Depending what your code does, either make sure it loads only once, or frees the grid before loading a new dataset.
 

Niften

Member
Only one grid is kept in memory at a time. Grids are unloaded/loaded when they need to be. ds_grids are only loaded once. obj_blockchunk draws grids, and I can create multiple blockchunks to have multiple chunks. Whenever a blockchunk is created it creates a new grid. It is not repeatedly saved - it only saves when the grid is unloaded.

Loading script:
Code:
var chunk_x = argument0;
var chunk_y = argument1;

var chunkdata = chunk_data_load(chunk_x,chunk_y);
var chunk_object = instance_create(chunk_x*BLOCK_SIZE*CHUNK_SIZE,chunk_y*BLOCK_SIZE*CHUNK_SIZE,obj_blockchunk);

chunk_object.data = ds_grid_create(CHUNK_SIZE,CHUNK_SIZE);//create the grid for the object
ds_grid_read(chunk_object.data, chunkdata); //load data into the grid
chunk_object.chunk_x = chunk_x;
chunk_object.chunk_y = chunk_y;
I also do ds_grid_destroy whenever the blockchunk object is destroyed, so I don't know what could be causing a memory leak...
 

Simon Gust

Member
Any thoughts?
I suggest taking some time recordings
Code:
var time = current_time;

// functions here

var delay = current_time - time;
show_debug_message("time: " + string(delay) + " ms");
Example
Code:
var time = current_time;

var chunk_x = argument0;
var chunk_y = argument1;

var chunkdata = chunk_data_load(chunk_x,chunk_y);
var chunk_object = instance_create(chunk_x*BLOCK_SIZE*CHUNK_SIZE,chunk_y*BLOCK_SIZE*CHUNK_SIZE,obj_blockchunk);

chunk_object.data = ds_grid_create(CHUNK_SIZE,CHUNK_SIZE);//create the grid for the object
ds_grid_read(chunk_object.data, chunkdata); //load data into the grid
chunk_object.chunk_x = chunk_x;
chunk_object.chunk_y = chunk_y;

var delay = current_time - time;
show_debug_message("time: " + string(delay) + " ms");
Your console should spit out messages that tell how much miliseconds have passed since that function.
 
Have you used the profiler within the debugger to try to identify the cause of the lag spikes?

Also, turn on show_debug_overlay(true) and see if the lag is CPU or GPU based. I think the red bar is code and yellow bar is drawing.
 

Simon Gust

Member
Have you used the profiler within the debugger to try to identify the cause of the lag spikes?

Also, turn on show_debug_overlay(true) and see if the lag is CPU or GPU based. I think the red bar is code and yellow bar is drawing.
Would be cool if the red bar would be cpu and the yellow bar gpu. But they decided that the red bar is just everything in the step / yellow bar in the draw event. So drawing in the step event will make the red bar increase.
 

Niften

Member
I suggest taking some time recordings
Code:
var time = current_time;

// functions here

var delay = current_time - time;
show_debug_message("time: " + string(delay) + " ms");
Example
Code:
var time = current_time;

var chunk_x = argument0;
var chunk_y = argument1;

var chunkdata = chunk_data_load(chunk_x,chunk_y);
var chunk_object = instance_create(chunk_x*BLOCK_SIZE*CHUNK_SIZE,chunk_y*BLOCK_SIZE*CHUNK_SIZE,obj_blockchunk);

chunk_object.data = ds_grid_create(CHUNK_SIZE,CHUNK_SIZE);//create the grid for the object
ds_grid_read(chunk_object.data, chunkdata); //load data into the grid
chunk_object.chunk_x = chunk_x;
chunk_object.chunk_y = chunk_y;

var delay = current_time - time;
show_debug_message("time: " + string(delay) + " ms");
Your console should spit out messages that tell how much miliseconds have passed since that function.
I can't use debug messages because it's being spammed with "Grid 0, index out of bounds writing [0,32] - size is [32,32] - in fact, maybe that has to do with my issue?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I can't use debug messages because it's being spammed with "Grid 0, index out of bounds writing [0,32] - size is [32,32] - in fact, maybe that has to do with my issue?
You might want to fix that then before you deal with other issues... ;)
 
Yes, you'll need to fix that out of bounds error first.

In case it's not obvious, a grid that has a size of [32, 32] means its index for columns and rows go from 0-31.

In the error message you are trying to access index 32, which is actually the 33rd element, so it is out of bounds.
 

Simon Gust

Member
I wish the game would crash instead of filling up the console with messages without notice. It's annoying to wait some like 10 minutes for them to clear.
So I just use arrays instead, but that's personal preference.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
The game WILL crash if you enable gml_release_mode... :)

Personally, I prefer the console output as it means I can still continue to test the project while being aware that there is an issue.
 

Niften

Member
I think I found the problem! The for loop that draws everything was using <= instead of <. Thanks for the help guys, really appreciate it! :)
 
Top