Archived

This topic is now archived and is closed to further replies.

ArdeII

DirectX 9 mystical memory leak?

Recommended Posts

I have this very very simpe DirectX9 application which basically does nothing but opens window and cleares it (no 3D stuff is rendered). My problem is when the program is started it immediatelly starts to allocate memory and do not free it. When started the memory usage is 13,000k in Win task manager and it grows linearilly. I have noticed that DX tutorials does this same thing, but the samples which uses the GraphicsSample framework doesnt. I have also noticed that the memory is allocated within the render method (see code below) I use C# and SharpDevelop and Win2000. Please someone help me because I want to know what I''m doing wrong (or am I?). Have anyone suffered from same kind of problems? Thanks in advance. CODE ------ using System; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace Test { public class MemTest : Form { static void Main() { MemTest app = new MemTest(); app.Show(); while (app.Created) { app.Render(); Application.DoEvents(); } } private Device device; public MemTest() { InitializeGraphics(); } protected bool InitializeGraphics() { PresentParameters pres = new PresentParameters(); pres.Windowed = true; pres.SwapEffect = SwapEffect.Discard; device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pres); device.RenderState.Lighting = true; return true; } protected void Render() { device.Clear(ClearFlags.Target, Color.Bisque, 1.0F, 0); device.BeginScene(); device.EndScene(); device.Present(); } } }

Share this post


Link to post
Share on other sites
I never used C# before, but if I was going to debug this I''d put a breakpoint on all 3 lines which have "new" in them... Run the program, it should only hit those 3 lines once while its initializing. If it keeps hitting them over and over, something is wrong. Try it.

Share this post


Link to post
Share on other sites
I was writing this
-----------------
Thanks for the advice,
the method where the news are is called only once.
If I comment the Render method out it still allocates memory that is in the application loop the application does absolutely nothing but loops and calls Application.DoEvents();
------------------

when I discovered that the method Application.DoEvents(); is to be blamed for memory allocation.

Why is that? Does anyone know?

-----

Share this post


Link to post
Share on other sites
The issue probably isn''t with DX or you, it''s probably the managed framework, but it''s probably not a problem. See, the garbage collector determines when the best time to collect excess memory is, depending on a variety of factors. This means that the memory usage as shown in task manager is not accurate. For a more accurate picture of how much memory your app is actually using, minimize it. This usually causes the garbage collector to run and thus the correct memory usage is shown.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
Thanks dude it works, but
it still bothers me that the memory usage is raising continuously. Doesnt this mean that if someone uses my application for long time period he runs out of memory?

-----

Share this post


Link to post
Share on other sites
No, it won''t. The garbage collector determines best time to run collection, which usually means when CPU usage is low, available memory is low, or a variety of other factors. It is generally very good at determining when to collect. For more information, check here:

Microsoft Information
More Microsoft Information




Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
Conclusion: I believe now

That Garbage Collection thing is so amazing thing that it is hard to believe that it works. I read this article and it convinced me to believe that it really does work!

http://www.doitin.net/memusage.htm

Note: When I was programming with C++ it was really annoying when you were reqiured to deallocate every memory allocation and work with those stinky pointers. Now with C# I wish that I could control the memory even a little and I really miss those real pointers. Funny isnt it

Share this post


Link to post
Share on other sites
C# does have pointers, you can access them by using the unsafe keyword. However, this is probably not recommended since you''re bypassing one of the main tenents of the framework. I''m also not sure how that would bode in some instances for the cross-language compatibility. Thanks for the article, a good insight.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
Yes I know that C# has pointers but they are so impracticable. Maybe I have understood wrong but if I want to use pointers like in C++ it is not possible because the memory is constantly being modified.

Example:

Node[] n;

public unsafe void ChangeNodesAddress(Node otherNode[])
{
fixed(Node* temp=&otherNode[12])
{
n=temp;
}
}

You cannot do this!!!

Or if you can do this(how?) you have no guarantees that the address points to the right place after a while.

Am I right or wrong?
I have read some pointer articles but I havent found any usefull yet, so some tips would be appreciated

Share this post


Link to post
Share on other sites
Honestly, I don''t know.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The unsafe word tells the runtime not to move those pointers around because they are being used.

Share this post


Link to post
Share on other sites
That's BS. If task manager says the app is using 30megs - it is. That memory is allocated for that process and other processes cannot use it without paging that memory to disk.

I just got boned by this too. I was running some test with GraphEdit and I got the "start prayin' suckka, Windows is resizing your page file..." message. deveenv was using 257megs, and then my GraphEdit test went to allocate a number of large buffers. My computer started thrashing as loads of memory was paged out to disk and the swap file was made larger to accommodate it. After the test finished, devenv was still using 257megs. Minimizing it reduced the footprint to 2,584k, and restoring it brought it to 10,204k and now every time I click on something I have to wait for it to load again (like the macro recorder which pisses me off to the nth degree) and the memory consumption shoots back up (after starting the macro recorder and player it's back to 32meg - then my computer beat pud for a second while it collected and its back to 9megs).

devenv spends 95% of its time idle, and it did not release memory for another app to use it when I ran out of physical and virtual memory.

I'm rather impressed my machine has not locked-up.


[edited by - Magmai Kai Holmlor on July 10, 2003 6:58:39 PM]

Share this post


Link to post
Share on other sites
The first .NET appliation to use particular things will have those things loaded and compiled into its processes memory space. This will inflate the size of your .NET applications in memory, but should stay well under 20 megs (besides whatever you allocate). You will see that if you run two instances of the program, once is substaintially smaller.

Do not worry about your small test application, it doesn''t allocate enough memory to bring about a garbage collection. I would recommend calling System.GC.Collect() ever few hundred frames or so if you are having problems with the garbage collector hanging your program for split seconds and effecting hte framerate. However if you keep the number of object allocations to a minium (note: allocate objects of pretty much any size you like without fear) you shouldn''t have much of a problem with the collector effecting your framerate. My engine chugs along doing nearly 100 low level collections every second or so. Its pretty increadable.

-SniperBoB-

Share this post


Link to post
Share on other sites