Advertisement

why Slowdown?

Started by April 18, 2002 07:59 PM
7 comments, last by edwinnie 22 years, 7 months ago
why is it that after 0.5min, my game suddenly got slower and very slow, like lagging like that?
Have you checked if you''re leaking any memory? It sounds like a classic memory leak situation.

Advertisement
hmm... like how do i check that?
Hi!

This is a way to check memory leaks (of course, it''s not the better one, but can be useful):

-> Create a text file for outpur at the beginning of the program. We''ll use it as a log file.
-> After each instruction that gets memory (malloc in c, new in Pascal, etc...), write in the log something like this: "Used 2048 bytes for sprite." (or for node, or for structure, or for whatever you want , and put the right amount of bytes -sizeof can help you!-).
-> After each instruction that free memory, write in the log something like this: "Free 2048 bytes for sprite." (the same comments as above).
-> When finishing the execution, close the text file.

Now, it''s time to check the file, and remove the lines goes together (each "used" must have a "free" for the same concept, and of the same size). If after removing it, you still have "used" lines, these will tell you where the memory leak is.

Of course, you can add an "#ifdef FLAG_CHECK_LEAKS" and an "#endif" to surround the code lines that use the log file, and a "#define FLAG_CHECK_LEAKS" at the beginning so, if you have checked it, coment this #define and then, the lines will "desapear" when you compile the program.


theNestruo

Syntax error in 2410
Ok
theNestruoSyntax error in 2410Ok
wow! erm...i must admit i am no expert at this.

but if u can tell me step by step, i would be most appreciated.
like how the program knoes that is a log file?
Well this is how I added alog function to my projects:

  void Log(char Text[], int SetFlag = 0); // Defaults...void Log(int Number, int SetFlag = 0);void LogInit(void){    ofstream File("Log.txt"); // Trunc file    if(!File)    {	MessageBox(NULL, "Could not open Log File.", "Error", MB_ICONSTOP);	return;    }    File.close();}void Log(char Text[], int SetFlag) // Set flag defulted to NULL{    char Flag = ''\n'';    if(SetFlag == 0) Flag = '' '';    ofstream File("Log.txt", ios::app);    if(!File)    {	MessageBox(NULL, "Could not open Log File.", "Error", MB_ICONSTOP);	return;    }    File << Text << Flag;    File.close();    return;}void Log(int Number, int SetFlag){    char Flag = ''\n'';    if(SetFlag == 0) Flag = '' '';	    ofstream File("Log.txt", ios::app);    if(!File)    {  	MessageBox(NULL, "Could not open Log File.", "Error", MB_ICONSTOP);	return;    }    File << Number << Flag;    File.close();    return;}   

At the start of WinMain() or main() just call LogInit() to initalize the file and then I have just overloaded the log function, one to take an int and one to take a string. It is defaulted to go to the next line after each log but if you want it to stay on the same line you just call
Log("some text", NULL); 
instead of
Log("some text"); 
you can also have a int instead of text though so you can go
Log(myVar); 
(in windows NULL is #defined as 0)
I hope this helps...

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Advertisement
i''m assuming your running under some sort of windows os.

There''s logging tools that come with windows that monitor your systems performane, if your running win2k,winNT, or winXP (which you really should be for any code development work because of their stability) press ctrl+shift+esc to bring up windows task manager, click the performance tab so you can see system memory used. If you program spikes this, then your using lots o memory that you probably don''t need to be. If running win9x you should be able to get the memory monitor running (if it''s installed off the win install/oem setup disks) by going to start->program->accesiores->system tools->

Ok, now to fix the problem you need to spend time to look for functions that create memory spaces and but don''t release them(ie, a string manipulation routine, but forgetting to delete the temp buffer). It could also be that you have some sort of recursion accidently built in (functions calling itself), these situations cause huge memory usage. To test for this you need a profiler (there''s one in Microsoft Visual C++ Profesional and enterprise editions, which returns of many times a function is called and tells you how long it was in each. it''s under the build menu, click it and choose function timing). This should give you an idea of functions that don''t return.

To me it definatly sounds like a memory problem, your in the end going to have to go through and double check that your not allocating memory indefinatly. It''s a long process but something you need to do. Also, sense i''m guessing your not a pro, this is to be expected at your lvl, you''ll learn over time what situations can have problems, and how best to code around them. Good luck in your search.

-Scott
-Scott
thx!
Not related, but Elis-Cool, do you use MFC in your sample code?
I was wondering what that MessageBox() thing was..

This topic is closed to new replies.

Advertisement