CreateTimer memory increase

BlitzMax Forums/BlitzMax Programming/CreateTimer memory increase

Hezkore(Posted 2010) [#1]
I don't know if I'm doing anything wrong or what's going on, but it seems that CreateTimer slowly increases my applications memory usage!
It starts out with 6200K and after a minute or so it's up to 7350K.

There's another thing I've noticed, making a small application that does.. well nothing, makes it eat 50% CPU. :o
Graphics(320, 240, 0)
While Not KeyDown(KEY_ESCAPE)
WEnd

Throwing in a delay there seems to fix it, but is that really the best way to do it?

Last edited 2010


shinkiro1(Posted 2010) [#2]
50% CPU: Have you also created a timer in this app?
It seems your app is running nonstop (if you have a dual core)

The small memory increase is normal due to the gc waiting some time before doing a cleanup. You can try manually calling
GCCollect()

allthough it shouldn't be needed.

Last edited 2010


Czar Flavius(Posted 2010) [#3]
Because the program doesn't do nothing! It checks if the escape key has been pressed, and if it hasn't, it checks if the escape key has been pressed, and if it hasn't, it checks if the escape key has been pressed........... millions of times a second.

A Delay of 1 will tell the OS that there isn't any useful work to do at the moment, and the OS will prioritise other tasks for a while. It's a good idea to Delay 1 in your main game loop.


Hezkore(Posted 2010) [#4]
*DOUBLE POST - SORRY!*

Last edited 2010


Hezkore(Posted 2010) [#5]
Yeah the program does nothing but even a delay of 20 milliseconds only lowers the CPU usage to about 15%, BUT... check this out!

This will give you a high CPU usage
SuperStrict
SetGraphicsDriver(D3D7Max2DDriver())

Local g1:TGraphics = CreateGraphics(320, 240, 0, 60, GRAPHICS_BACKBUFFER)
SetGraphics(g1)

EnablePolledInput()
Repeat
	Cls()
	DrawText("Graphics 1", 32, 32)
	Flip()
Until KeyHit(KEY_ESCAPE) Or AppTerminate()


While this uses MUCH less CPU!
SuperStrict
SetGraphicsDriver(D3D7Max2DDriver())

Graphics(320, 240, 0, 60)

EnablePolledInput()
Repeat
	Cls()
	DrawText("Graphics 1", 32, 32)
	Flip()
Until KeyHit(KEY_ESCAPE) Or AppTerminate()

Bug?

Last edited 2010

Last edited 2010


Hezkore(Posted 2010) [#6]
No idea how I double posted that.. sorry :S


Czar Flavius(Posted 2010) [#7]
I don't know why the two are different, but again of course you get high cpu usage. It's a matter of quantity and quality. If you are doing a simple action in a loop, the CPU will just do it kabazilions of times a second. You don't need to be finding the nth prime number to tie it up. Put a Delay 1 in there to give the OS a chance to take over.


Hezkore(Posted 2010) [#8]
No dude, something is seriously wrong!
We'll use your beloved Delay function and I'll show you that one way eats way more CPU than the other.

20% Usage (Using delay):
SuperStrict
SetGraphicsDriver(D3D7Max2DDriver())

Local g1:TGraphics = CreateGraphics(320, 240, 0, 60, GRAPHICS_BACKBUFFER)
SetGraphics(g1)

EnablePolledInput()
Repeat
	Cls()
	DrawText("Graphics 1", 32, 32)
	Flip()
	Delay(5)
Until KeyHit(KEY_ESCAPE) Or AppTerminate()


0% CPU usage (No delay):
SuperStrict
SetGraphicsDriver(D3D7Max2DDriver())

Graphics(320, 240, 0)

EnablePolledInput()
Repeat
	Cls()
	DrawText("Graphics 1", 32, 32)
	Flip()
Until KeyHit(KEY_ESCAPE) Or AppTerminate()


By your logic, the example that eats 0% CPU should eat way more CPU usage than the 20% example.

Last edited 2010


Czar Flavius(Posted 2010) [#9]
It seems to be the Flip command that is causing the difference. Using Flip(0) for the first example removes the CPU usage. Using Flip(0 or 1) for the second example increases CPU usage.



Last edited 2010


Hezkore(Posted 2010) [#10]
Hmm interesting!
I know the flip parameter changed it's function some time ago, maybe it's not changed if you use CreateGraphics() instead of Graphics()


Zeke(Posted 2010) [#11]
look mod/brl.mod/graphics.mod/graphics.bmx-> function Flip() and Graphics():

if using "softsync" there is internal delay. so if you have fast computer delay is bigger and so on.

and 'softsync' is used only with Graphics() command, depth=0 and hertz>0. (windowed mode, Graphics 800,600 or Graphics 800,600,0,30). -> Less CPU.

using CreateGraphics() and Flip(), your app is synced with monitor refreshrate. -> more CPU.