473,409 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Question regarding memory management

Hi all,

I am having slight confusion regarding memory management in .net.

Say suppose i have two application one is in C# and other is in
MFC(VC++).

Both of this application are using lots of memory.

Suppose i run first C# application which has occupied all memory and
performed necessary operation and run stay in idle state as it is.
Means after performing operation i have disposed all object but still
garbage collector is not called(as we can not force garbage collector
to be execute).
And now i want to run MFC application which require lots of memory. so
will garabage collector be called at a time of running mfc application
to get memory for mfc applicaton.

Can some one explain me memroy management between managed and unmanaged
application.

any help will be appreciated.

thanks in advance.

Nov 21 '05 #1
1 2758

<tr**************@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi all,

I am having slight confusion regarding memory management in .net.

Say suppose i have two application one is in C# and other is in
MFC(VC++).

Both of this application are using lots of memory.

Suppose i run first C# application which has occupied all memory and
performed necessary operation and run stay in idle state as it is.
Means after performing operation i have disposed all object but still
garbage collector is not called(as we can not force garbage collector
to be execute).
And now i want to run MFC application which require lots of memory. so
will garabage collector be called at a time of running mfc application
to get memory for mfc applicaton.

Can some one explain me memroy management between managed and unmanaged
application.

any help will be appreciated.

thanks in advance.


Part of your confusion stems from the fact that you think the GC is a memory
manager, while it's not, let me explain (in short).
First, let's forget about ".NET, it's a marketing term, so let's talk about
the active component that differentiates managed and unmanaged applications,
it's a runtime component called the CLR (on MS platforms).
Managed and unmanaged applications are running inside processes, managed by
the OS, the CLR is private to the process, the OS doesn't know about it at
all. So, to the OS there is only the "process".
Process memory management is a task performed by the OS. An active process
occupies a part of physical memory (RAM) , called the Process Working Set
(WS), and as all active processes must share the available RAM memory, the
OS will have to adapt the WS of individual processes to the available RAM.
Say you start a new process P2 when a running process P1 occupies all
available RAM (the OS won't let this happen though), the OS will trim P1's
WS by writing (data) pages from RAM to the paging file, until there is
sufficient space for the second process (P2) to start running.
As long as P1 remains idle, P2 can see it's WS grow until all pages of P1
are removed from RAM. Whenever P1 returns from idle state the OS will
reverse the operation and start trimming P2's WS. Note that when both P1 and
P2 are active, they will (most likely) incur a high performance penalty
because of the paging activity as a result of the WS trimming.

Now what's the role of the CLR, or more precisely, the GC in this whole
story. First, the GC is not a memory manager, it's a collector, it's role is
to free the space occupied by dead objects from the GC heap (a private per
process heap) and re-arrange the live object's locations within this heap
such that they are adjacent at the beginning of the GC heap (in an ideal
world). The GC heap is nothing more than a private process heap (there are
many more heaps in a process), The CLR's "memory allocator" reserves the GC
heap space from the Process Virtual Address Space by calling the Win32 heap
functions - see here we have the link between OS and application.
The CLR allocates(reserves) an initial heap consisting of 2 segments of 16MB
(workstation version), the first segment being used to store the
generational heaps (gen0-2) the second to store large (>85Kb) objects.
The CLR's "memory allocator" reserves a new segment whenever one of the
initial segments become full (with live objects), additional segments can be
returned to the OS when they get empty, the initial segments are never
returned to the OS (actually they are at CLR shutdown time).
It's clear that the GC heap segments become part of the process WS, and when
the OS trims the WS it will remove pages from the heap. Now, before this
happens, the CLR will call the GC in order to collect dead objects, and as
such create as much as free pages as possible.
But, how does the CLR knows that the OS needs more free memory? Well, on XP
or higher, the CLR registers for Memory Resource Notifications
(CreateMemoryResourceNotification ), whenever a certain memory threshold is
reached the OS notifies the CLR which starts a GC run (which frees pages
from it's heap) and possibly trims it's own WS by returning excess segments
to the OS.
On lower OS's the CLR queries the memory counters at regular intervals and
will initiate GC and possibly return excess segments when a certain memory
threshold has been reached.

Hope this more or less answers you questions, for more details on OS Memory
management, you'll have to search the Platform SDK docs, for more details
about the GC I would suggest http://blogs.msdn.com/maoni/default.aspx as a
must read.

Willy.




Nov 21 '05 #2

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

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
18
by: Andre Laplume via AccessMonster.com | last post by:
I have inherited a bunch of dbs which are are shared among a small group in my dept. We typically use the dbs to write queries to extract data, usually dumping it into Excel. Most dbs originated...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.