473,587 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Managed memory vs unmanaged memory

My understanding is that when I re-compile a existing MFC application with
the /clr switch, that the code generated is managed(with some exceptions)
but that the data isn't, i.e. not garbage collected. I also noticed that
when replaced some of the existing MFC dialogs with managed winforms that
everything is still running in the same app domain.( No context change) So
my question is this, what are the performance differences in using managed
data vs. unmanaged data? Is there a difference in the way value types are
handled vs. reference types?

Thanks

Bill


Nov 17 '05 #1
4 5716
William F. Kinsley wrote:
My understanding is that when I re-compile a existing MFC application
with the /clr switch, that the code generated is managed(with some
exceptions) but that the data isn't, i.e. not garbage collected.
Correct.
I
also noticed that when replaced some of the existing MFC dialogs with
managed winforms that everything is still running in the same app
domain.( No context change) So my question is this, what are the
performance differences in using managed data vs. unmanaged data? Is
there a difference in the way value types are handled vs. reference
types?


Managed data may move at any time. This means that managed data needs to be
pinned before being accessed by native code. Pinning, of course, incurs
additional overhead that unmanaged memory does not see. When managed data
is accessed by managed code, no pinning is necessary since the CLR can track
(and adjust) all references to the managed memory if it should move.

Value types on the managed heap are really no different, but value types as
local variables are quite different - they're basically the same as native
types as local variables. They are allocated directly on the stack,
embedded directly into other value types, etc.

There is a context change of sorts when moving from managed to unmanaged
code (and vice-versa) because of coordination with the GC. For example, the
GC can freeze a thread running managed code at nearly any point in it's
execution. If a thread is running native code, however, the GC will not
freeze that thread until it returns to managed code.

-cd
Nov 17 '05 #2
Thank for your help, however I have some follow up questions,
1) Ok, I understand that managed pointers copied to an unmanaged pointer
that it must be pinned, but since I am working in MC++ and I'll have both
managed and on managed pointers, how can I tell the differences? For
example, in this code sample below:
a) Both integers are created on the stack (there is only one stack,
right? there is not a managed stack and unmanaged stack?
b) fFArray and dlg are on the managed heap? These do not need to be
pined, right?
c) pObj is a pointer on the stack and it points to an managed object and
needs to be pinned, right?
System::Int32 sysInt32;
int nativeInt;
System::Object __pin *pObj;

#pragma push_macro("new ")
#undef new
float fFArray2 __gc[] = new float __gc[5];
MFCTestApp::GCA boutDlg *dlg;
dlg = __gc new MFCTestApp::GCA boutDlg ();
pObj = dlg;
#pragma pop_macro("new" )

dlg->ShowDialog() ;
pObj = 0;
delete dlg;
dlg = 0;
2) An in your last paragraph, are you refering to boxing?
Again, thank you for your help,
Bill

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:OQ******** ******@TK2MSFTN GP15.phx.gbl...
William F. Kinsley wrote:
My understanding is that when I re-compile a existing MFC application
with the /clr switch, that the code generated is managed(with some
exceptions) but that the data isn't, i.e. not garbage collected.
Correct.
I
also noticed that when replaced some of the existing MFC dialogs with
managed winforms that everything is still running in the same app
domain.( No context change) So my question is this, what are the
performance differences in using managed data vs. unmanaged data? Is
there a difference in the way value types are handled vs. reference
types?


Managed data may move at any time. This means that managed data needs to

be pinned before being accessed by native code. Pinning, of course, incurs
additional overhead that unmanaged memory does not see. When managed data
is accessed by managed code, no pinning is necessary since the CLR can track (and adjust) all references to the managed memory if it should move.

Value types on the managed heap are really no different, but value types as local variables are quite different - they're basically the same as native
types as local variables. They are allocated directly on the stack,
embedded directly into other value types, etc.

There is a context change of sorts when moving from managed to unmanaged
code (and vice-versa) because of coordination with the GC. For example, the GC can freeze a thread running managed code at nearly any point in it's
execution. If a thread is running native code, however, the GC will not
freeze that thread until it returns to managed code.

-cd

Nov 17 '05 #3
William F. Kinsley wrote:
Thank for your help, however I have some follow up questions,
1) Ok, I understand that managed pointers copied to an unmanaged
pointer that it must be pinned, but since I am working in MC++ and
I'll have both managed and on managed pointers, how can I tell the
differences? For example, in this code sample below:
In Managed Extensions for C++ it's very hard to tell sometimes. A simple
T* might be a native pointer, managed reference, or interior pointer and you
need to examine the context where it's declared to figure out which. In
VC++ 2005 the new C++/CLI bindings separate these concepts entirely: T* is
a native pointer, T& is a native reference, T^ is a managed "handle", T% is
a tracking reference, interior_ptr<T> is an interior pointer, pin_ptr<T> is
a pinned pointer.
a) Both integers are created on the stack (there is only one stack,
right? there is not a managed stack and unmanaged stack?
Yes, both on stack. Yes, only one stack, but the organization of the
managed portion of the stack may be different from that of the native stack
(in regard to parameter passing, local variable allocations, stack frame
setup, etc).
b) fFArray and dlg are on the managed heap? These do not need to be
pined, right?
Yes, both on managed heap. These need to be pinned iff they are passed to
native code. Items accessed from managed code never need to be pinned.
c) pObj is a pointer on the stack and it points to an managed
object and needs to be pinned, right?
pObj is a value type (pointer) and lives on the stack. It needs to be
pinned if it's passed to native code. It's really no different from dlg in
terms of needing to be pinned or not.

[snipped code]
2) An in your last paragraph, are you refering to boxing?


No. Boxing is the process of placing a value type on the managed heap.
This is relevant to the second-to-last paragraph that I wrote. Value types
live on the stack or directly embedded in other objects. Boxed value types
live on the managed heap and are referenced like any reference type.

In the last paragraph I'm referring to context-switch work that goes on
whenever a thread enters or leaves native code. Every time managed code
calls native code, the compiler has to generate code more or less equivalent
to:

prepare_to_exit _clr()
marshall_params _to_native_stac k()
call_native_fun ction()
marshall_return _to_managed_sta ck()
reenter_clr()

It doesn't really generate calls for all those things, but they all occur.
It's not exactly a context switch, but it's a lot more than simply calling a
function.

-cd
Nov 17 '05 #4
Thank you, I think I now understand this a little better now.

Bill
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:Oo******** ******@TK2MSFTN GP09.phx.gbl...
William F. Kinsley wrote:
Thank for your help, however I have some follow up questions,
1) Ok, I understand that managed pointers copied to an unmanaged
pointer that it must be pinned, but since I am working in MC++ and
I'll have both managed and on managed pointers, how can I tell the
differences? For example, in this code sample below:
In Managed Extensions for C++ it's very hard to tell sometimes. A simple
T* might be a native pointer, managed reference, or interior pointer and

you need to examine the context where it's declared to figure out which. In
VC++ 2005 the new C++/CLI bindings separate these concepts entirely: T* is a native pointer, T& is a native reference, T^ is a managed "handle", T% is a tracking reference, interior_ptr<T> is an interior pointer, pin_ptr<T> is a pinned pointer.
a) Both integers are created on the stack (there is only one stack,
right? there is not a managed stack and unmanaged stack?
Yes, both on stack. Yes, only one stack, but the organization of the
managed portion of the stack may be different from that of the native

stack (in regard to parameter passing, local variable allocations, stack frame
setup, etc).
b) fFArray and dlg are on the managed heap? These do not need to be
pined, right?
Yes, both on managed heap. These need to be pinned iff they are passed to
native code. Items accessed from managed code never need to be pinned.
c) pObj is a pointer on the stack and it points to an managed
object and needs to be pinned, right?


pObj is a value type (pointer) and lives on the stack. It needs to be
pinned if it's passed to native code. It's really no different from dlg

in terms of needing to be pinned or not.

[snipped code]
2) An in your last paragraph, are you refering to boxing?
No. Boxing is the process of placing a value type on the managed heap.
This is relevant to the second-to-last paragraph that I wrote. Value

types live on the stack or directly embedded in other objects. Boxed value types live on the managed heap and are referenced like any reference type.

In the last paragraph I'm referring to context-switch work that goes on
whenever a thread enters or leaves native code. Every time managed code
calls native code, the compiler has to generate code more or less equivalent to:

prepare_to_exit _clr()
marshall_params _to_native_stac k()
call_native_fun ction()
marshall_return _to_managed_sta ck()
reenter_clr()

It doesn't really generate calls for all those things, but they all occur.
It's not exactly a context switch, but it's a lot more than simply calling a function.

-cd

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2286
by: Weston Fryatt | last post by:
(Sorry for spamming multiple groups, But I need a solution to this problem) I think this should be a simple question on Memory Allocation in a managed DLL and passing a memory pointer over to an unmanaged DLL. I have a "Unmanaged" Client DLL that I'm creating a Managed "wrapper" to be used in VB.Net and/or C#.. In the my Client DLL (unmanaged), There are several functions where I need to allocate a "client side" memory buffer to...
3
3497
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust ------------------------------------------------------------------------
2
2716
by: Sandy | last post by:
I am confused about Unmanaged Code, How .Net Framework treate that code, What is the use of that. Thanks in advance Sandeep Chitode
6
5889
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example will prove useful. structure MyStruct { // this is a complicated struct declaration in the sense
12
12522
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley retreiving the data. Presently all of the c++ code is still in .NET 1.1, so we're using a _nogc bridge class wrapped in a _gc c++ class in order to facilitate this interop. But we've converted everything not c++ to .NET 2.0 and would love to
25
2999
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class MyClass:IDisposable
3
2208
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm trying to specify the requirement from unmanaged DLL component that will be used by a managed application written in C#. The unmanaged DLL is implementing some kind of algorithm for defect detection that will generate results. Each defect result should be a structure containing the data of the found defect. The number of defect is known only at the end of the algorithm at the unmanaged side. The managed application should get the...
8
8982
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx http://www.codeproject.com/managedcpp/garbage_collection.asp - sources
20
2287
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Sorry to bring up a topic that is just flogging a dead horse.... but... On the topic of memory management.... I am doing some file parcing that has to be done as quick as posible, but what I have found hasnt been encouraging... I found that the *quickest* way is to use a struct to retrieve fixed length packets out of the file, but structs get placed on the stack! In one of the books I read, the stack is only about 1mb, so I would...
0
7924
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.