How to monitor memory usage for managed/unmanaged code - memory-leaks

I have an application that is built in C# .NET. It uses excel as a presentation layer and unmanged C++ as a processing engine. Is there a tool I can use to check for memory leaks for each component?

AQTime will instrument both managed and unmanaged code. I have used it successfully to find memory leaks in managed/unmanaged project.

Related

External Profiler for Unity 3D apps

Hello is there a way to profile a Process with an external profiler? I use the unity profiler but it dosent show me allocations in other threads then the main thread.
I'm pretty sure that use Unity Profiler is the best way to profile Unity authored apps/games. Unity Profiler shows allocations only on the main thread, because is there where your game code resides.
In some cases we can create our own threads to do some calculations, I imagine is this your case, so I recommend to you use ANTS Performance Profiler and ANTS Memory Profiler, I used both of them to profile ordinary .NET apps and they are really amazing profiles and, yes, they show allocations to all your app's threads.

JavaScript executions fill up PermSpace

I am working on a server side java application, that converts HL7 messages to Java objects using JavaScript. We chose JavaScript for this, because it was easy to deploy in the production system, without down time.
However, we now have the problem that the 30.000 daily executions of the JavaScript's seems to generate a lot of class garbage in the VM's PermSpace. This results in some unattractive stop-the-world garbage collections.
Do any of you know of a way to avoid generating a lot of class objects, when executing JavaScript?
Any other advice is also welcome. 
We are running on an IBM WebSpere 6.1 appserver with java 1.5. The JavaScript engine is SJP-1.0 (Mozilla Rhino).
Regards
Søren

Is there any way to create dynamic types at runtime without having them permanently in the app domain?

My current understanding of dynamically generated types is this:
If you generate a type via CodeDom and load it into an AppDomain, there is no way to unload that type (i.e. Assembly.Unload(...) doesn't exists) without destroying the AppDomain as a whole.
Are there any other ideas on how to create custom types at runtime?
Can the C# 4.0 dynamic keyword be used somehow magically? Could the .NET 4 ExpandoObject be utilised is some lovely way?
Could anonymous types and the dynamic keyword be combined with some technical wizardry?! It feels like we have the tools scattered that might achieve something useful. But I could be wrong.
Once an assembly or type has been loaded into the AppDomain, it's there until the AppDomain is torn down, period, no exceptions.
That's why CodeDom is pure evil when used in any kind of bulk. It's a guaranteed memory leak and performance problem. EVERY compile with CodeDom generates a new assembly. I think you have a few of options:
Run a sandboxed AppDomain for your dynamic types.
Run your primary AppDomain in an environment where recycles and pooling are acceptable. Obviously in a client application, this is not possible, but if you're running in ASP .NET you can add code that monitors the number of Assemblies loaded in your AppDomain and requests a recycle when that number reaches a critical point. Then just have IIS pool your web application and you still have high availability, since you have multiple AppDomains running at once.
Use TypeBuilder and Reflection.Emit. This lets you use one dynamic assembly for all of your dynamically generated types.
If you want to dynamically generate C# style code like you can with CodeDom, you can still use this in conjunction with TypeBuilder, so your dynamic C# code gets compiled to a TypeBuilder in a dynamic assembly, instead of to a new assembly every time. To do this you can use MCS (Mono compiler service). You can pass it C# formatted classes and with a little tweaking, you can have it compile your code to a single dynamic assembly. See Mono Compiler as a Service (MCS)

What are Managed objects and unmanaged objects in C++/CLI?

What are Managed objects and unmanaged object in C++/CLI
Managed objects are a feature of the .NET framework and its implementation of a C++-like language, and have their memory managed for you by the .NET garbage collector. C++ itself has no such concept, and a better (in general) way of managing all resources (not just memory) called RAII.
The concept Managed/Unmanaged is not typically C++. It is Microsoft .Net technology speak.
In normal, plain C++ applications, the application itself is responsible for deleting all the memory it has allocated. This requires the developer to be very careful about when to delete memory. If memory is deleted too soon, the application may crash if it still has a pointer to it. If memory is deleted too late, or not deleted at all, the application has a memory leak.
Environments like Java and .Net solve this problem by using garbage collectors. The developer should not delete memory anymore, the garbage collector will do this for him.
In the 'native' .Net languages (like C#), the whole language works with the garbage collector concept. To make the transition from normal, plain C++ applications to .Net easier, Microsoft added some extensions to its C++ compiler, so that C++ developers could already benefit from the advantages of .Net.
Whenever you use normal, plain C++, Microsoft talks about unmanaged, or native C++. If you use the .Net extensions in C++, Microsoft talks about managed C++. If your application contains both, you have a mixed-mode application.
Managed objects do not exist in C++.
They exist in Microsoft's .NET extensions to C++, and a complete explanation would be a bit long, sorry.

Garbage Collector in Real-Time System

I'm new to C#/Java and plan to prototype it for soft real-time system.
If I wrote C#/Java app just like how I do in C++ in terms of memory management, that is, I explicitly "delete" the objects that I no longer use, then would the app still be affected by garbage collector? If so, how does it affect my app?
Sorry if this sounds like an obvious answer, but being new, I want to be thorough.
Take a look at IBM's Metronome, their garbage collector for hard real-time systems.
Your premise is wrong: you cannot explicitly “delete” objects in either Java or C#, so your application will always be affected by the GC.
You may try to trigger a collection by calling GC.Collect (C#) with an appropriate parameter (e.g. GC.MaxGeneration) but this still doesn’t guarantee that the GC won’t be working at other moments during execution.
By explicitly "delete" if you mean releasing the reference to the object then you are reliant on the garbage collector in C# managed code - see the System.GC class for ways of controlling it.
If you choose to write unmanaged C# code then you will have more control over memory, akin to C++, and will be responsible for deleting your instantiated objects, able to use pointers, etc. For more info see MSDN doc - Unsafe Code and Pointers (C# Programming Guide).
In unmanaged code you will not be at the mercy of the the Garbage Collector and its indeterminate cleanup algorithms.
I don't know if Java has an equivalent unmanaged mode, but this Microsoft info might help provide some direction on C#/.NET to use its available features for your requirement of dealing with the garbage collector.
In Csharp or Java you can't delete object. What you can do is only mark them available for deletion. The memory free up will be done by Garbage Collector.. It might be the case that Garbage Collector may not run during the life time of your application. However it's likely to run. When your system is becoming short of resources it is the most likely time when GC routines are run by the runtime. And when resources are low GC becomes the highest priority thread. So your application do get effected. However you can minimize the effect by calculating the correct load and required resources for your application life time and make sure to buy the right hardware which is good enough for that. But still you can't just bench mark your performance.
Besides just GC the managed application do get a slight overhead over the traditional C++ application due to the extra delegation layer involved. And a slight first time performance panelty since the run time needs to be up and running before your application get started.
Here are some references for developing real-time systems with the .net compact framework:
IEEE - C# and the .NET Framework: Ready for Real Time?
MSDN - Real-Time Behavior of the .NET Compact Framework
They both talk about the memory requirements of using the .net framework.
C# and Java are not for Real-Time development. Soft real-time is attainable however as you note.
For C#, the best you can do is implement the finalize/dispose pattern:
http://msdn.microsoft.com/en-us/library/b1yfkh5e(VS.71).aspx
You can request it to collect, but typically it's much better at determining how to do this.
http://msdn.microsoft.com/en-us/library/system.gc(VS.71).aspx
For Java, there are many options to optimize it:
http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html
Along with third party solutions like IBM Metronome as noted above.
This is a real science within CS itself.

Resources