locked
Game freezes every few seconds RRS feed

  • Question

  • Hi,

    Every few seconds my game freezes and then resumes. The time of the freezes varies depending on debug/release or if i am debugging in visual studio.

    Also, when i run in debug mode with debugging sometimes* the program freezes permanently. But if i put a breakpoint in, it breaks, then when i continue it works fine. Its wierd.

    I am using threads for parts of my program but im certain my code is fine.

    Are there any bugs with regard to threads and freezing in xna?

    help please.

     

    Tuesday, December 5, 2006 11:13 AM

Answers

  • I have 3 threads on 'BelowNormal' priority.


    Does any thread wait on any other thread, implicitly or explicitly? Either through a Monitor or similar object, or through lock(), or through some blocking primitive?

    You could be suffering from priority inversion, where some higher-priority thread (like the rendering thread) is blocked waiting for data from some lower-priority thread, and that lower-priority thread is not getting scheduled because it has a lower priority.

    In general, when designing threaded programs, you want to be very careful about designing your data and dependency flow, and use threads to overlap computations that are "pushed out" from the main thread, or use threads to asynchronize inherently synchronous processes (like I/O). A thread that's not the main thread should pretty much always have the structure:

      forever:
        wait for something to do
        do it
        post the result

    And, because the threads are blocking, they should almost always have a priority that's as high as, or higher than, the main thread.

    Tuesday, December 5, 2006 3:13 PM
  • i thought what you thought: The threads are in contention. So i removed all resource sharing but the problem is still here. This did fix the problem of the entire program locking up when i launch, but not the intermitent pauses.

    If the threads don't share any resources, they cannot be providing any value to your program. At some point, you must be passing results from one thread to another (using a shared resource) or invoking methods on a common API (which means the resources used by the API are shared). In the latter case, the common API is effectively the shared resource.

    It is entirely possible that you are invoking an API that is not designed for multithreaded use (most class libraries are not designed that way, except for static methods).

    Your question appears to be, "Is the implementation of threads buggy in the .NET Framework?" The answer is no. There is nothing special about using threads in an XNA Game project. It is the same threads implementation as in any other managed application on Windows. If you introduce additional threads, you have to manage synchronization yourself. By making the claim that your threads don't share any resources, I suspect that you've overlooked something.

    Without seeing any of your code, it will be very hard for anyone to help you identify the problem.

    --Stephen

    Tuesday, December 5, 2006 5:32 PM

All replies

  • Without seeing your code there's really no way we can help you.
    Tuesday, December 5, 2006 1:19 PM
  • "I am using threads for parts of my program but im certain my code is fine."

    How are you certain this is fine? Your problem sounds like the threads are in contention. This would be caused you have your threads accessing the same content at the same time. Threads should be used to do dicreet independent tasks on different pieces of data but, people often use threads to try to process the same data faster or to allow tow processes to use the same data instead of giving each process a copy of the data.

    Show us your thread code and we should be able to tell you if it's causing issues.

    Tuesday, December 5, 2006 2:12 PM
  • the freezing is on a completely random basis. Sometimes it takes 5 seconds to happen, sometimes 1 second. Then it could be 10 seconds before it happens again.

    My threads use no shared resources. They are totally independant. I have 3 threads on 'BelowNormal' priority.

    I am not pausing, resuming or do any fancy thread based stuff.

    I have no memory leaks. Its very graphically simple, everything is initialized at the start and after that it is just draw calls.

    When i run with debugging (release/debug) in visual studio i dont have this problem (or at least it isnt visible). It happens occasionally but i think that is my computer being slow.

    Only when i 'start without debugging' is there a visual problem.

    Tuesday, December 5, 2006 2:19 PM
  • i thought what you thought: The threads are in contention. So i removed all resource sharing but the problem is still here. This did fix the problem of the entire program locking up when i launch, but not the intermitent pauses.
    Tuesday, December 5, 2006 2:25 PM
  • I have 3 threads on 'BelowNormal' priority.


    Does any thread wait on any other thread, implicitly or explicitly? Either through a Monitor or similar object, or through lock(), or through some blocking primitive?

    You could be suffering from priority inversion, where some higher-priority thread (like the rendering thread) is blocked waiting for data from some lower-priority thread, and that lower-priority thread is not getting scheduled because it has a lower priority.

    In general, when designing threaded programs, you want to be very careful about designing your data and dependency flow, and use threads to overlap computations that are "pushed out" from the main thread, or use threads to asynchronize inherently synchronous processes (like I/O). A thread that's not the main thread should pretty much always have the structure:

      forever:
        wait for something to do
        do it
        post the result

    And, because the threads are blocking, they should almost always have a priority that's as high as, or higher than, the main thread.

    Tuesday, December 5, 2006 3:13 PM
  • i thought what you thought: The threads are in contention. So i removed all resource sharing but the problem is still here. This did fix the problem of the entire program locking up when i launch, but not the intermitent pauses.

    If the threads don't share any resources, they cannot be providing any value to your program. At some point, you must be passing results from one thread to another (using a shared resource) or invoking methods on a common API (which means the resources used by the API are shared). In the latter case, the common API is effectively the shared resource.

    It is entirely possible that you are invoking an API that is not designed for multithreaded use (most class libraries are not designed that way, except for static methods).

    Your question appears to be, "Is the implementation of threads buggy in the .NET Framework?" The answer is no. There is nothing special about using threads in an XNA Game project. It is the same threads implementation as in any other managed application on Windows. If you introduce additional threads, you have to manage synchronization yourself. By making the claim that your threads don't share any resources, I suspect that you've overlooked something.

    Without seeing any of your code, it will be very hard for anyone to help you identify the problem.

    --Stephen

    Tuesday, December 5, 2006 5:32 PM
  • Although I agree with the previous posters that this sounds most like a thread contention issue, it is also possible that these pauses could be caused by unusually large amounts of garbage having to be collected.

    If you run your program under the CLR Profiler (available for free download on microsoft.com) you can examine how many generation-2 collections are happening. If it is collecting gen-2 more than every few minutes, you are probably creating too much garbage and will need to optimize your code to reduce this.
    Tuesday, December 5, 2006 6:57 PM
  • I am not sure if this is related or not but thought I would mention it here just in case. I have also noticed a similar behavior, sometimes it happens alot, other times it doesn't happen at all. I do not have ANY threading invloved in my XNA application at all that isn't there by virtue of the framework itself.

    I am running currently an AMD x64 FX2 4000+ processor, in an asus avn-vm board, and I do have some slowish ram at ddr3200. Im my development I tend to run alot of application and thought that this was my problem to many things going on with slow ram, and maybe a full cache. So one of the processes of trying to figure this out started with my not running as many applications, which at first seemed to have a notacible effect. Eventually I started to get used to the difference and still found that same delay just not as often.. Finally I got to turning off running services to see if that was the problem..  Eventually I got to my viris scanner, I currently use the avast product, and noticed that my pauses and or breaks in operation basically went away literally. I am not sure what avast is doing, and it has like 3, or 4 might be 5 services running to monitor things like email, and web, and other services..  When I finally turned them all off the problem went instantly away..

    Just thought that I would present my findings in a similar problem in case it might help. I have since turned back on my scanner and such and just realize that the debug version I run, the fact that my virus scanner seems to scan almost anything that uses memory etc and live with it ..

    Hope the information is at least helpful..


    Tuesday, December 19, 2006 12:50 AM