How can I find out what is creating garbage? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
This is a really general learning-based question, not a technical problem.
I'm making a game in Unity. The game involves a lot of pretty complex, high-object-count processes, and as such, I generate a lot of garbage. As everyone who makes games that use managed code knows, garbage collection spikes are a HUGE buzzkill. I am also trying to target mobile devices, so this pain is amplified.
My goal is simple: track down the procedures that generate the most garbage and pool and reuse the objects involved in order to reduce the load on the garbage collector. I have tackled all of the obvious classes, but my GC issues persist. If anyone can offer some greater wisdom here, please bring it on.
What I have been unable to track down in my research is a good way to measure GC load and track down problem spots in the code itself. I can get metrics from Unity and from diagnostics classes about the size of the memory pool (though I'm no expert on what any of those numbers really mean) and I can display the total number of GC collections that have happened since the start, but my knowledge of debugging this issue kind of ends there.
I know there MUST be better ways to solve this problem. Can anyone point me at the right tools or framework classes (or libraries?) that can help me out here? I've seen some mention of a debugger program (sgen?) that's part of Mono, but I can't find a download or let alone a guide for how to hook it up to a Unity game.
Any help would be greatly appreciated. Thanks.

For C# and Unity3D specifically, the profiler built into Unity3D is the best tool you'll have available to you. You should be able to get very far with the Unity Profiler. Specifically the CPU Profiler:
I have highlighted the most important column - GC Alloc - Which, on a frame-by-frame basis, shows memory that has been allocated for garbage collection. The aim of the game here is to make that column as consistently close to zero as possible.
I'd suggest spending a few hours in there, playing your game, pausing it and digging into areas of your code where GC Alloc is showing numbers.
Finally, I strongly suggest you check out this video tutorial by one of the developers of Unity on memory debugging and using the Unity Editor profiler properly.

Caveat: it's all down to details: general principles are nice but you're right - real data is the right thing. Unfortunately it's hard to get for Unity: I've seen reports that DotTrace works for some aspects of Unity games but that most of Unity looks like a black box to it.
A useful rule of thumb is to look for new statements. It's a rule of thumb, not a 100% scientific principle (structs are created with new but they go on the stack... mostly...) but it's a good leading indicator of possible causes of fragmentation. If you "new" something up and let if just disappear ( go out of scope, be dereferenced, etc) it will end up in the garbage collector. Moreover if you new up a batch of items and dereference some of them you'll also fragment the heap which makes the collection spikes worse as the collector tries to defragment the memory.
This is why most recommendations in this space will be to use pools, as you're already doing.
Here are some general purpose links that might be useful in tackling memory management in Unity:
http://www.gamasutra.com/blogs/WendelinReich/20131109/203841/C_Memory_Management_for_Unity_Developers_part_1_of_3.php
http://andrewfray.wordpress.com/2013/02/04/reducing-memory-usage-in-unity-c-and-netmono/

Related

Garbage Collector I need a guide [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 16 days ago.
Improve this question
I am really confused about GC. My head is a mess when I am trying to explain this to somebody.
Do you guys have a guide or set of articles or a good book that explains GC in .NET?
I did program a little bit in C a long time ago, not much in C++ but mostly in C# all I can explain is using keyword for opening files and Dispose which I don't really know how to answer about its internals.
I googled and youtube'd about GC, all I get is empty terminology for interview to memorize which I don't want to memorize. I am looking firstly for a history of it, how it works in C, C++. I don't want random keywords managed heap, 3 generations, mutex, weak references, like fine I wanna know about it too but I need an order, reading about GC feels like a puzzle for me, I open like 10 tabs then I forgot where it started and its really frustrating.
I really need a dummy guide, from C to IDisposable, even if it takes 500 pages I am gonna go through it.
I know it might be asked a lot of times, it might be a dumb question, but I am never gonna learn if I don't ask for help. I googled myself, I get mostly interview responses which aren't followed by code and its like reading a med school book, I am never gonna memorize anything if it doesn't make sense.
A very high level summary -
Garbage collection is one of the key differentiators between managed languages like C# and unmanaged languages like C and C++.
Managed languages take care of allocating and deallocating memory for your data objects. Garbage collection is just the automatic freeing of memory when you don't need it anymore. C and C++ don't do this for you - you have to do it yourself, or else you will eventually run out of memory. Obviously folks have come up with strategies over the years for dealing with this (reference counters, etc.), but there's really no substitute for the automatic garbage collector of a managed language.
The truth is in C# you rarely have to worry about garbage collection. There are a handful of scenarios where you can accidentally step into pitfalls that prevent it from happening on some objects - we call these memory leaks - but that's a bigger topic.
The Wikipedia article on garbage collection is pretty decent if you want to try to get into the nitty gritty. Otherwise if you're just getting into C# or explaining C# to someone new to it I honestly wouldn't think about it in the beginning. That's sort of the point - it exists so that you don't have to think about it.
Also using and Dispose are actually not really related to garbage collection (maybe indirectly). In fact using and Dispose are closer to resource management strategies in unmanaged languages. That is to say they represent manual resource deallocation. But Dispose isn't supposed to be used for memory deallocation either except in rare circumstances. Rather it's supposed to be for cleaning up any other resources that might be in use, such as open files.
This is a list of books you might like
https://www.amazon.in/Memory-Management-Implementation-Programming-Development/dp/1556223471
Memory Management: Algorithms and Implementation in C/C++ (Windows Programming/Development)
https://www.amazon.com/Garbage-Collection-Algorithms-Automatic-Management/dp/0471941484
Garbage Collection: Algorithms for Automatic Dynamic Memory Management
You can find official documentation on GC in C# there
https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/
The information should be of some help to you, but I am not familiar with it much, and will learn more about it.

Tool to monitor threads in C# application [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am currenly analyzing my code and application for having resource leakage. How do I monitor a C# process for currently running threads?
There's already a tool present, Parallel Stacks. To open it, Click on
Debug -> Windows -> Parallel Stacks
You can have a quick view on how to work with Parallel Stacks at MSDN.
This is the approach I use when I need to locate a leak:
Open a memory profiler.
I use perfmon.
This article has some material about setting perfmon and #fmunkert also explains it rather well.
Locate an area in the code that you suspect that it is likely that the leak is in that area. This part is mostly depending on you having good guesses about the part of the code that is responsible for the issue.
Push the Leak to the extreme: Use labels and "goto" for isolating an area / function and repeat the suspicious code many times (a loop will work to. I find goto more convenient for this matter).
In the loop I have used a breakpoint that halted every 50 hits for examining the delta in the memory usage. Of course you can change the value to feet a noticeable leak change in your application.
If you have located the area that causes the leak, the memory usage should rapidly spike.
If the Memory usage does not spike, repeat stages 1-4 with another area of code that you suspect being the root cause. If it does, continue to 6.
In the area you have found to be the cause, use same technique (goto + labels) to zoom in and isolate smaller parts of the area until you find the source of the leak (Please don't downvote me for the recursive step... :0) ).
Please note that the down sides of this method are:
If you are allocating an object in the loop, it's disposal should be also contained in the loop.
If you have more than one source of leak, It makes it harder to spot (yet still possible)
Good luck...
If you have visual studio 2013 You can download the Microsoft Concurrency Visualizer for Visual Studio:
https://msdn.microsoft.com/en-us/library/dd537632.aspx
It gives great insight in the application and threads currently running.
Synchronization, Sleeping, Blocking etc etc.
Next to that you can also download the extension (found on the same page)
In my opinion a great tool (and best of all free)

Winforms / WPF Private bytes Memory Leak? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm using Visual Studio 2008 to work on a Winform / WPF project.
It uses multiple projects and classes to build it into a working product.
My problem is, we have noticed that there is a 4-8k per second leak in the memory usage. granted it is a small leak, but it is non-stop continuous 4-8k. Our application runs over night and even for a few days at time. When those few days comes alone, this thing has eaten up more memory than the computer can handle (usually 2-3 gigs) and a force restart on the pc is the only solution. This leak occurs even while nothing is happening except network communications with our host.
After further analysis on the project through ANTS Memory Profiler, we have discovered that the Private bytes data is continuously growing. Is there any way to tell where this private data is being created from? I haven't had much luck tracking this down with ANTS. Steps would help greatly!
Image of the private bytes increasing (~45 minutes):
Image of the Time line growth (~45 minutes):
Thanks in advance!
If the private bytes keep increasing, it means you have a memory leak. Try DebugDiag, it is from MS and free, also a very good tool to tracking memory leak on Windows.
Use this tool is simple, first you create a rule to monitor your process with DebugDiag collection, it will create memory dump according to your rule, you can create the memory dump manually. Then you can use DebugDiag Analysis to analysis the dump, please set the right symbol path before analysis.
This MSDN article Identify And Prevent Memory Leaks In Managed Code might help too. This article points our how to find out if the memory leak is a native one or managed one. If it is a purely .NET manage leak, you can also use CLR profiler to debug the problem.

Steps to open source a small project [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I've been working for a couple of years on a small project, almost by myself, with the eventual help of some colleagues. The project is getting out of my hands, because the size of the code is growing (around 20K lines now) and the initial expectations I had for it have outgrown my own ability and time. So now I want to open source it, with the hope to attract some contributors. My motivations for going open source are these:
The project is rather academic (a library of algorithms for scientific computing), and I don't really have any economic interest in it.
The project is getting too big for me to handle it by myself, and the number of features I've planned are enough to keep a small team motivated (I think).
It needs a lot of testing, not just unit testing, but testing in the real world to see if the API is easy to use, the performance is as expected, etc.
I'm sure it has a lot of bugs, but I can only find a few, since its me alone testing it.
It needs proper documentation, because the API is getting a bit complex.
Other than that, I think that the project could benefit from a comunity in terms of deciding which features are most needed, and creating a set of guidelines for the future development.
I'm using Git, so my first thought was to publish it on Github and/or Codeplex. Besides that, what would be the steps to help to slowly grow a community of users and perhaps developers around it? Do I need a domain of my own, or should I stick to Github/Codeplex? How do I set up a platform for collaboration between developers potentially geographically separated? Should I set up a mailing list? And most important, how do I attract people to use it and collaborate with it?
The project is a .NET library for optimization and machine learning, written in C#.
There is only one piece of advice I can give here; use Github. It is common, (pretty much) everyone knows about it, it is easy to use, and the community who you are trying to attract is already on it. It has a ton of tools which you may not have even thought about, but may come in handy. It it pretty much the perfect solution for what you're looking to do, so don't overthink it.
As for attracting people to use it and contribute, if it is something that is useful and good, people will find it. I have found a ton of obscure projects with a simple google. If someone googles for something related to your project (and it is appropriate named and such) they will likely find it. There isn't really much you can do to force a demand though, just let it happen. As for contributors, people who are using it will likely contribute they're additions back. Just be sure to stay actively involved in managing it (monitoring pull requests, etc). If no one is accepting requests or managing versions, contributors will likely start to give up on your project.

Is there any console "graphics" library for .Net? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
My basic goal here is writing a .NET remake of Kingdom of Kroz. For those not familiar with the game:
http://www.indiefaqs.com/index.php/Kingdom_of_Kroz
http://www.youtube.com/watch?v=cHwlNAFXpIw
Originally it was supposed to be a quick distraction project to give me a break from all the generic enterprise WCF/WF/LINQ2SQL/etc work projects occupying most of my time lately. While the result of my effort is playable, it looks like absolute arse (even for a console-based game) because of the way I'm redrawing everything in each frame.
I'm aware of some alternate approaches but in the brief tests I've done they still don't offer significant performance or aesthetic benefits. I don't want to resort to a library which 'emulates' a console if I can help it. I'd prefer to work with the proper Win32 console API under the hood, but not to work with it directly if I can help it. Keeping in mind that it's a distinctly niche use case, what would be the 'generally' accepted best approach for this? Are there any particularly optimal console 'drawing' techniques one should be aware of? I don't mind swimming in a sea of PInvoke and marshalling as long as it still ends up with a fast, responsive and efficient console UI.
You could try Curses-Sharp http://sourceforge.net/projects/curses-sharp/ or libtcod https://github.com/chamons/libtcod-net
curses-sharp is a "A full featured, object-oriented, multi-platform C# wrapper for the curses terminal control library. "
and libtcod is "...a free, fast, portable and uncomplicated API for roguelike developpers providing an advanced true color console, input, and lots of other utilities frequently used in roguelikes."
http://msdn.microsoft.com/en-us/library/ms682073(v=VS.85).aspx
Some/many of these functions you might need to use P/Invoke for, but they should do what you need. You can write at arbitrary locations in the buffer and get key-based, non-buffered input. Usually you start with GetStdHandle() to get handles to the console input and output "streams" and then you call one of the appropriate functions from the above list to do something, like WriteConsoleOutputCharacter(), or PeekConsoleInput().
I once wrote a library to create an in-process windowing system using the Windows console and plain Win32 on C. Fun project, but I don't know where it is now.
Consider checking out the pdcurses or ncurses library. I am using it in Visual C++ and while you are specifying C#, it may be of use to you. It is made for console gaming and I am using it in several projects. It will eliminate the need to utilize a callback refreshing the screen as you will be able to move the character (represented as an ASCII character) without refreshing the screen.

Categories