473,395 Members | 1,694 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,395 software developers and data experts.

Garbage Collection with Weak References

I'm loading a boatload of data into a DataSet. The memory usage grows and
grows for the app while loading that data. Calling GC.Collect() reduces the
consumption slightly. When I minimize the app though, the usage goes to
about 500k, and then grows when maximizing the app and working with DataSet.
The DataSet still appears to have all data when mem footprint was many many
megs at the end of loading it.

Two questions:
1) I'm guessing DataSet employs weak references to all data, and that weak
referenced data is GC'ed when the app minimizes. Can anyone confirm/deny
that?
2) If that is the case, is there a "GC.AgressivelyCollect()" type call I
can make while I'm loading data so the footprint stays in a somewhat sane
range?

Thanks in advance!

Derrick
Nov 15 '05 #1
7 2444
"Derrick" <de*********@excite.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
1) I'm guessing DataSet employs weak references to all data, and that weak referenced data is GC'ed when the app minimizes. Can anyone confirm/deny
that?


This is not the case. If the dataset had weak references that were
collected, how would the dataset re-aquire the data?
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com


Nov 15 '05 #2
Unless you are running out of memory, I would advise that you let the GC do
it's job uninterrupted and without intervention. The application is going
to use as much memory as it needs, but the GC will automatically start
collecting more aggressively if system memory comes under pressure.

Oh, one other thing: Manually calling GC.Collect() might have some
unintended consquences for you: depending upon whats going on with your app
at the time that you call it, that might actually cause memory to be
released less efficiently. The GC operates on the principle that longer
lived objects are collected less frequently than shortlived objects, so each
time an object survives a GC (because there is a reference to it), it is
promoted to a higher generation. There are 3 generations: 0, 1, and 2. The
GC won't try to collect an object in generation 1 unless not enough memory
was released with a gen 0 collection. Gen 2 collections are even more rare,
and are very expensive, as the GC must freeze all threads and examine every
object in memory. By calling GC.Collect() manually, you might be
artificially promoting some of your objects to Gen 1 or even Gen 2, and
thereby increasing the amount of time that the memory will be held (and
increasing the expense of destroying them)

Rico Mariani (performance optimizer guy for MS) has an interesting blog
entry on the topic:
http://weblogs.asp.net/ricom/archive.../02/40780.aspx
"Derrick" <de*********@excite.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
I'm loading a boatload of data into a DataSet. The memory usage grows and
grows for the app while loading that data. Calling GC.Collect() reduces the consumption slightly. When I minimize the app though, the usage goes to
about 500k, and then grows when maximizing the app and working with DataSet. The DataSet still appears to have all data when mem footprint was many many megs at the end of loading it.

Two questions:
1) I'm guessing DataSet employs weak references to all data, and that weak referenced data is GC'ed when the app minimizes. Can anyone confirm/deny
that?
2) If that is the case, is there a "GC.AgressivelyCollect()" type call I
can make while I'm loading data so the footprint stays in a somewhat sane
range?

Thanks in advance!

Derrick

Nov 15 '05 #3
What's going on if it's not GarbageCollection then? Can you explain the
memory usage behavior I'm observing? It does not make sense to me..

Looking at app (release build, simple windows forms app) via task manager,
App startup, mem usage 10 megs
I load a 20 meg xml file into a dataset, mem usage about 60 megs.
Minimize the app, mem usage 500k
Maximize the app, mem usage 2 megs.
Navigate data via forms app, all data still available in the DataSet, mem
usage goes up slowly
Minimize the app, mem usage back down to 500k
Maximize the app, mem usage 2 megs.

In a previous post I provide sample code to reproduce this, let me know if
you'd like me to post again..

Thanks -

Derrick

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"Derrick" <de*********@excite.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
1) I'm guessing DataSet employs weak references to all data, and that

weak
referenced data is GC'ed when the app minimizes. Can anyone confirm/deny that?


This is not the case. If the dataset had weak references that were
collected, how would the dataset re-aquire the data?
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com

Nov 15 '05 #4
Thanks for the info, I had been calling GC.Collect with 0 after reading a
little today, still didn't help.

I still don't understand why the mem usage stat in windows task manager
drops to almost nothing when the app is minimized, and then grows relatively
slowly upon maximizing and navigating data. Is that expected?

I'm not running out of memory on my machine, but am worried about clients
running on older machines with much less memory available. The dataset I'm
prototyping with is one of our smaller sets, xml file about 20m. They can
get up to 300m....

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:uZ*************@tk2msftngp13.phx.gbl...
Unless you are running out of memory, I would advise that you let the GC do it's job uninterrupted and without intervention. The application is going
to use as much memory as it needs, but the GC will automatically start
collecting more aggressively if system memory comes under pressure.

Oh, one other thing: Manually calling GC.Collect() might have some
unintended consquences for you: depending upon whats going on with your app at the time that you call it, that might actually cause memory to be
released less efficiently. The GC operates on the principle that longer
lived objects are collected less frequently than shortlived objects, so each time an object survives a GC (because there is a reference to it), it is
promoted to a higher generation. There are 3 generations: 0, 1, and 2. The GC won't try to collect an object in generation 1 unless not enough memory
was released with a gen 0 collection. Gen 2 collections are even more rare, and are very expensive, as the GC must freeze all threads and examine every object in memory. By calling GC.Collect() manually, you might be
artificially promoting some of your objects to Gen 1 or even Gen 2, and
thereby increasing the amount of time that the memory will be held (and
increasing the expense of destroying them)

Rico Mariani (performance optimizer guy for MS) has an interesting blog
entry on the topic:
http://weblogs.asp.net/ricom/archive.../02/40780.aspx
"Derrick" <de*********@excite.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
I'm loading a boatload of data into a DataSet. The memory usage grows and grows for the app while loading that data. Calling GC.Collect() reduces

the
consumption slightly. When I minimize the app though, the usage goes to
about 500k, and then grows when maximizing the app and working with

DataSet.
The DataSet still appears to have all data when mem footprint was many

many
megs at the end of loading it.

Two questions:
1) I'm guessing DataSet employs weak references to all data, and that

weak
referenced data is GC'ed when the app minimizes. Can anyone confirm/deny that?
2) If that is the case, is there a "GC.AgressivelyCollect()" type call I can make while I'm loading data so the footprint stays in a somewhat sane range?

Thanks in advance!

Derrick


Nov 15 '05 #5


"Derrick" <de*********@excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Thanks for the info, I had been calling GC.Collect with 0 after reading a
little today, still didn't help.

I still don't understand why the mem usage stat in windows task manager
drops to almost nothing when the app is minimized, and then grows
relatively
slowly upon maximizing and navigating data. Is that expected?

I'm not running out of memory on my machine, but am worried about clients
running on older machines with much less memory available. The dataset
I'm
prototyping with is one of our smaller sets, xml file about 20m. They can
get up to 300m....



The OS will trim the workingset of all windows programs when minimized, this
has nothing to do with .NET.
When memory becomes scarce, the OS will also trim the WS of all processes.
From the applications perspective, there is no need to call GC.Collect, and
reducing the WS of an active process is also a bad idea, as it often results
in a page-out sequence followed by a page-in, so in short a lot of
unnecessary IO.

Now back to your xml file, if you really intend to process such large xml
files in a timely manner (that is without paging), you will have to change
your design, or you will need a lot of memory (>1GB), GC.Collect nor
reducing the WS will help you out.

Willy.
Nov 15 '05 #6
Well, it could be that the initial parse of the XML is costing you temporary
memory. As Willy noted, the O/S will reclaim memory on a minimize.

Are these XML files just serialized datasets, or are they XML files that you
have created, and are "manually" reading into datasets? If it's the latter,
you might try using some different technology to read the XML files. If you
load it up as a DOM object, (at least, this is the way the COM XML parsers
work, someone correct me if .Net is different), the whole document must be
loaded into memory and parsed, so large XML files can be very expensive.
You might try an alternative technology, like SAX, which loads the XML
progressively, and fires events as new elements are read.
"Derrick" <de*********@excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Thanks for the info, I had been calling GC.Collect with 0 after reading a
little today, still didn't help.

I still don't understand why the mem usage stat in windows task manager
drops to almost nothing when the app is minimized, and then grows relatively slowly upon maximizing and navigating data. Is that expected?

I'm not running out of memory on my machine, but am worried about clients
running on older machines with much less memory available. The dataset I'm prototyping with is one of our smaller sets, xml file about 20m. They can
get up to 300m....

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:uZ*************@tk2msftngp13.phx.gbl...
Unless you are running out of memory, I would advise that you let the GC do
it's job uninterrupted and without intervention. The application is going
to use as much memory as it needs, but the GC will automatically start
collecting more aggressively if system memory comes under pressure.

Oh, one other thing: Manually calling GC.Collect() might have some
unintended consquences for you: depending upon whats going on with your

app
at the time that you call it, that might actually cause memory to be
released less efficiently. The GC operates on the principle that longer
lived objects are collected less frequently than shortlived objects, so

each
time an object survives a GC (because there is a reference to it), it is
promoted to a higher generation. There are 3 generations: 0, 1, and 2.

The
GC won't try to collect an object in generation 1 unless not enough memory was released with a gen 0 collection. Gen 2 collections are even more

rare,
and are very expensive, as the GC must freeze all threads and examine

every
object in memory. By calling GC.Collect() manually, you might be
artificially promoting some of your objects to Gen 1 or even Gen 2, and
thereby increasing the amount of time that the memory will be held (and
increasing the expense of destroying them)

Rico Mariani (performance optimizer guy for MS) has an interesting blog
entry on the topic:
http://weblogs.asp.net/ricom/archive.../02/40780.aspx
"Derrick" <de*********@excite.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
I'm loading a boatload of data into a DataSet. The memory usage grows and grows for the app while loading that data. Calling GC.Collect() reduces the
consumption slightly. When I minimize the app though, the usage goes
to about 500k, and then grows when maximizing the app and working with

DataSet.
The DataSet still appears to have all data when mem footprint was many

many
megs at the end of loading it.

Two questions:
1) I'm guessing DataSet employs weak references to all data, and that

weak
referenced data is GC'ed when the app minimizes. Can anyone

confirm/deny that?
2) If that is the case, is there a "GC.AgressivelyCollect()" type
call I can make while I'm loading data so the footprint stays in a somewhat sane range?

Thanks in advance!

Derrick



Nov 15 '05 #7
What you are observing is not the amount of managed heap in use by your
application. The amount of memory reported by Task Manager has more to do
with your working set than anything else, which is a very coarse
measurement. When a windowed application is minimized, the operating system
will collapse the working set - that's the behavior you're seeing.

If you really want to understand what's going on with your memory
allocations, use a tool like the CLR profiler available at:
www.gotdotnet.com

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Derrick" <de*********@excite.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
What's going on if it's not GarbageCollection then? Can you explain the
memory usage behavior I'm observing? It does not make sense to me..

Looking at app (release build, simple windows forms app) via task manager,
App startup, mem usage 10 megs
I load a 20 meg xml file into a dataset, mem usage about 60 megs.
Minimize the app, mem usage 500k
Maximize the app, mem usage 2 megs.
Navigate data via forms app, all data still available in the DataSet, mem
usage goes up slowly
Minimize the app, mem usage back down to 500k
Maximize the app, mem usage 2 megs.

In a previous post I provide sample code to reproduce this, let me know if
you'd like me to post again..

Thanks -

Derrick

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"Derrick" <de*********@excite.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
1) I'm guessing DataSet employs weak references to all data, and that

weak
referenced data is GC'ed when the app minimizes. Can anyone confirm/deny that?


This is not the case. If the dataset had weak references that were
collected, how would the dataset re-aquire the data?
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com


Nov 15 '05 #8

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

Similar topics

28
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all...
8
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found...
1
by: Nadav | last post by:
Hi, Lets take in mind the following scenario: Object A creates Object B and Object C, Object B and C are being initialized by object A so they reference each other ( Object B has a reference to...
18
by: Larry Herbinaux | last post by:
I'm having issues with garbage collection with my long-standing service process. If you could review and point me in the right direction it would be of great help. If there are any helpful...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
19
by: Born | last post by:
Here are my challenges to you: 1) Suppose object A has to do cleanup before it dies. Also suppose there are multiple references to A. How can I determine when and who to call Dispose (if you use...
4
by: coco343 | last post by:
Hello, I'm using a datagridview with an inner dropdownlist for a column. The datasource for the gridview and the dropdrownlist is a object, with inner collection for the dropdrownlist. When i...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.