-1

I'm developing a game on WPF. But as I launch the application, it freezes and the new RAM is not allocated. I 'm using .NET 6 and WPF. I think in the app .NET is running out of heap memory.

My application freezes in 120 MB of RAM when I have 8 GB of it.

Can you tell me how to increase the heap size in the WPF application in .NET 6.

The code is in: https://github.com/DmitriySidyakin/Chess/tree/Chess-0.1.2-with-Computer-Player-development

If you want to compile, you will download the .NET 6 from: https://dotnet.microsoft.com/en-us/download/dotnet/6.0

The program freezes if you select a Computer player in the settings (New Game) and it makes a move.

The accompanying project "GraphAlgorithms" can be downloaded from the repository: https://github.com/DmitriySidyakin/Graph

In "Chess.ComputerPlayer" project, in the file "FiveStepPlayer.cs", in line 150, you can specify the depth of the graph traversal by 0, and it will not be freezed if "int deep" specify 0. But the depth of search in the graph is needed here to improve the quality of AI.

If int deep is set to 5, I have a stack overflow. I also wanted to find out how to programmatically increase the stack or in the project parameters.

The question is, how to programmatically manage the Application Domain in the CLR to increase its stack and heap?

HailToTheVictor
  • 388
  • 2
  • 8
  • 28
  • what the heck did you create??? anyway, if you build for 64 bits, then you have a very large memory management. I don't think you have such a large hardware to exhaust. – Mario Vernari May 03 '23 at 03:28
  • My application freezes in 120 MB of RAM when I have 8 GB of it. – HailToTheVictor May 03 '23 at 03:38
  • 3
    oh, no! 120M is "nothing" in terms of RAM. For sure the problem is somewhere else in your code. I bet on some never ending loop or similar which blocks the main (UI) thread. – Mario Vernari May 03 '23 at 03:41
  • the code is in: https://github.com/DmitriySidyakin/Chess/tree/Chess-0.1.2-with-Computer-Player-development – HailToTheVictor May 03 '23 at 03:48
  • Can you target it to .NET 4.8? If so, what happens? – Jim Foye May 03 '23 at 04:00
  • I do no know. There is no difference. If you want to compile, you will download the .NET 6 from: https://dotnet.microsoft.com/en-us/download/dotnet/6.0 – HailToTheVictor May 03 '23 at 04:18
  • I just cloned the repo and ran the app, but I don't see any freeze. I see the board (well drawn, BTW), and likely everything fine to play a game. Anything else? (NOTE: tried from within the Visual Studio 2022 IDE; RAM 83MB; CPU close to zero) – Mario Vernari May 03 '23 at 04:29
  • Just run the Game with computer player. Then computer player steps, it will be freezed. – HailToTheVictor May 03 '23 at 04:42
  • a popup message shows "ComputerPlayerIsNotAvailableNow" when I select "computer player" for either black or white. If you don't see that message, it could be the cause of freezing. – Mario Vernari May 03 '23 at 04:47
  • You should clone the code from the branch: "Chess-0.1.2-with-Computer-Player-development". It is no in the main branch. – HailToTheVictor May 03 '23 at 04:50
  • Answer is in: https://stackoverflow.com/questions/2556938/how-to-change-stack-size-for-a-net-program/2556970#2556970 – HailToTheVictor May 03 '23 at 06:53
  • 1
    Most of the time if you have an WPF app freezing, it's because you're doing heavy work in the GUI thread, or blocking on IO. Optimize the work, move it to a separate thread, use non blocking IO, etc. – Etienne de Martel May 03 '23 at 14:31

2 Answers2

0

Answer is NO to your original question. .NET manage the memory allocation automatically.

Possibly it's freezing because of some other issues. Try improving the algorithm.

When it freezes click on the Pause button on Visual Studio and see where the execution is. So you can get an idea what it's doing.

May be some events keep firing continuously and making the UI unresponsive.

Visual Studio Diagnostic Tools can be helpful for investigation if there is any memory issue. If memory is not increasing drastically while it's freezing, I think unlikely it's a memory issue that causes the freeze.

https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage?view=vs-2022

https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage-without-debugging2?view=vs-2022

CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • It's not. In "Chess.ComputerPlayer" project, in the file "FiveStepPlayer.cs", in line 150, you can specify the depth of the graph traversal by 0, and it will not be freezed if "int deep" specify 0. But the depth of search in the graph is needed here to improve the quality of AI. – HailToTheVictor May 03 '23 at 05:21
  • @HailToTheVictor I've updated the answer. Sorry I don't have a straight solution. But I think it's more likely to be an algorithm issue rather than a memory issue. – CharithJ May 03 '23 at 06:32
0

Well, I guess I've found it.

In your NewGameSettings.xaml.cs module, you're doing a "forbidden" move: by hidding a modal window, you actually freeze the app. The modal is still there, but you can't see it (hence none can't close it).

Now, I don't know how your app should behave. I only show how to "unlock the freeze". After that, it's up to you tune the code.

    private void StartNewGameButton_Click(object sender, RoutedEventArgs e)
    {
        
        mainWindow.GameSettings.Player1White = PlayerName1.Visibility == Visibility.Visible ? Settings.PlayerType.Player : Settings.PlayerType.Computer;
        mainWindow.GameSettings.Player2Black = PlayerName2.Visibility == Visibility.Visible ? Settings.PlayerType.Player : Settings.PlayerType.Computer;

        mainWindow.GameSettings.Player1WhiteName = PlayerName1.Text;
        mainWindow.GameSettings.Player2BlackName = PlayerName2.Text;

        //this.Visibility = Visibility.Hidden;   //never hide a modal!!!
        this.Close();                            //do this instead!
        mainWindow.EndGame();
        mainWindow.ResetBoard();
        mainWindow.StartGame();
    }
Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
  • It's not. In "Chess.ComputerPlayer" project, in the file "FiveStepPlayer.cs", in line 150, you can specify the depth of the graph traversal by 0, and it will not be freezed if "int deep" specify 0. But the depth of search in the graph is needed here to improve the quality of AI. – HailToTheVictor May 03 '23 at 05:19
  • okay, but doing that the program runs and most of the times halts due a `StackOverflowException`. Every time in a different point. I believe it would be better if you place a logger which traces on file the actual flow of the program. Otherwise it would be very hard. – Mario Vernari May 03 '23 at 05:27