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

How to dispose of array of struc that is reused many times

Jim
In a C# project I'm working on for an iterative design application, I need
to dispose of a large arrray of a struct object and reinitialize the
array between iterations.

That is, the user starts a design, and the program creates an array of
objects defined as follows:

public struct DES_TYPE
{
public string short_string;
public string long_string;
public int entryNo;
public int col;
public int row;
};

The class contains an array of the struct above and two variables that
specify the size of the array:

public class PrincipalClass
{

// create reference to array
public DES_TYPE [,] desType;

// array size
int noCols, noRows;

// class constructor
public PrincipalClass ( int no_cols, int no_rows)
{

// initialize variables
noCols = no_cols;
noRows = no_rows;

// allocate array
desType = new DES_TYPE [ noCols, noRows];

// code to initialize array elements

} // end class
Each time the user starts a new design, the array should be initialized.
It is not known beforehand what its size will be, although a typical
size is on the order of a million objects. I need some
way of freeing the memory. I can't quite see a way of
implementing the IDisposable interface here. The
mechanics of the creating the Dispose ( ) method
aren't in question. What I can't see is how to
dispose of the elements in the struct, that is,
the two strings and three integers *and reclaiming the
memory.*

Anybody see a way around this? For one thing, I don't
mind creating a single instance of the PrincipalClass
when the program is started, and then intializing the array
for each design iteration. But I need a way to free up the
memory used by the array before starting a new design iteration.

What am I overlooking?


Jul 23 '06 #1
8 12933
"Jim" <ji*@nospam.plswrote:
In a C# project I'm working on for an iterative design application, I need
to dispose of a large arrray of a struct object and reinitialize the
array between iterations.
// allocate array
desType = new DES_TYPE [ noCols, noRows];
Each time the user starts a new design, the array should be initialized.
It is not known beforehand what its size will be, although a typical
size is on the order of a million objects. I need some
way of freeing the memory.
You need to pool your arrays, and to get reusable pools, you probably
need to stay away from two-dimensional arrays and do the column & row
multiplication & addition yourself. That way, you can allocate arrays
that are larger than needed / use arrays that are larger than strictly
needed to improve reusability of allocated arrays.

Basically, the solution to your problem is called memory pooling - it's
a well-known technique even outside of GC languages. There should be
lots of examples on Google.
I can't quite see a way of
implementing the IDisposable interface here.
IDisposable is not for memory.

-- Barry

--
http://barrkel.blogspot.com/
Jul 23 '06 #2
On Sun, 23 Jul 2006 02:40:02 GMT, "Jim" <ji*@nospam.plswrote:
>In a C# project I'm working on for an iterative design application, I need
to dispose of a large arrray of a struct object and reinitialize the
array between iterations.

That is, the user starts a design, and the program creates an array of
objects defined as follows:

public struct DES_TYPE
{
public string short_string;
public string long_string;
public int entryNo;
public int col;
public int row;
};

The class contains an array of the struct above and two variables that
specify the size of the array:

public class PrincipalClass
{

// create reference to array
public DES_TYPE [,] desType;

// array size
int noCols, noRows;

// class constructor
public PrincipalClass ( int no_cols, int no_rows)
{

// initialize variables
noCols = no_cols;
noRows = no_rows;

// allocate array
desType = new DES_TYPE [ noCols, noRows];

// code to initialize array elements

} // end class
Each time the user starts a new design, the array should be initialized.
It is not known beforehand what its size will be, although a typical
size is on the order of a million objects. I need some
way of freeing the memory. I can't quite see a way of
implementing the IDisposable interface here. The
mechanics of the creating the Dispose ( ) method
aren't in question. What I can't see is how to
dispose of the elements in the struct, that is,
the two strings and three integers *and reclaiming the
memory.*

Anybody see a way around this? For one thing, I don't
mind creating a single instance of the PrincipalClass
when the program is started, and then intializing the array
for each design iteration. But I need a way to free up the
memory used by the array before starting a new design iteration.

What am I overlooking?
Set all references to the array to null and call System.GC.Collect()
which will force the garbage collector to run. This may take some
time if it has a lot of work to do so your users might notice a pause.

rossum
Jul 24 '06 #3
rossum <ro******@coldmail.comwrote:
On Sun, 23 Jul 2006 02:40:02 GMT, "Jim" <ji*@nospam.plswrote:
Each time the user starts a new design, the array should be initialized.
It is not known beforehand what its size will be, although a typical
size is on the order of a million objects. I need some
way of freeing the memory.

Set all references to the array to null and call System.GC.Collect()
which will force the garbage collector to run. This may take some
time if it has a lot of work to do so your users might notice a pause.
This is using a sledgehammer to pound in a nail, IMHO. The cost of using
large arrays without pooling is that gen 2 collections occur
occasionally. Calling GC.Collect() will force a gen 2 collection every
time you call it.

-- Barry

--
http://barrkel.blogspot.com/
Jul 24 '06 #4
Jim

"Barry Kelly" <ba***********@gmail.comwrote in message
news:6l********************************@4ax.com...
rossum <ro******@coldmail.comwrote:
>On Sun, 23 Jul 2006 02:40:02 GMT, "Jim" <ji*@nospam.plswrote:
>Each time the user starts a new design, the array should be initialized.
It is not known beforehand what its size will be, although a typical
size is on the order of a million objects. I need some
way of freeing the memory.

Set all references to the array to null and call System.GC.Collect()
which will force the garbage collector to run. This may take some
time if it has a lot of work to do so your users might notice a pause.

This is using a sledgehammer to pound in a nail, IMHO. The cost of using
large arrays without pooling is that gen 2 collections occur
occasionally. Calling GC.Collect() will force a gen 2 collection every
time you call it.

-- Barry

--
http://barrkel.blogspot.com/
Barry and rossum:

Thanks to *both of you* for good suggestions. I may be forced to go the
memory pool route, but it does violate a ground rule here: I would
rather the user's design dictate the resources consumed (there are many
other objects besides the array I described earlier), rather than
having the resources dictate one facet of the largest design.
In other words, if I use a technique that cleans up between
iterations, then the user's iteration can trade off maximum
array size for maximum print buffer size which is only
loosely coupled to the array size. The print buffer is
built as the design iteration progresses; each step goes
practically unnoticed by the user. But building a print buffer
all at once... well, it would give him time to go fetch
a fresh cup of coffee.

It does so happen I globbed onto the GarbageCollector
Collect ( ) method after posting my earlier message here,
and found it acceptable up to about 500 k objects.
After that, yes, there is a bit of a balky delay although
shorter than the program's startup initialization which
reads a database. A finicky user may do a dozen
design iterations, and more, depending on how much
of a perfectionist s/he is.

I don't know which way I'm going to proceed just yet.
I'm going to fetch a cup of coffee and run some more
experiments. I'm tempted to offer both features
to the user and call them "Optimize for speed" or
"Optimize for memory usage". This has to be
traded off with how much effort we want to
spend explaining the ramifications to the
user who only knows what he sees on the
screen and doesn't care about the code
behind it all.



Jul 24 '06 #5
"Jim" <ji*@nospam.plswrote:
Thanks to *both of you* for good suggestions. I may be forced to go the
memory pool route, but it does violate a ground rule here: I would
rather the user's design dictate the resources consumed (there are many
other objects besides the array I described earlier), rather than
having the resources dictate one facet of the largest design.
I have my own pooled array generic template, and I use a wrapper
PooledArraySlice<Twhich implements IDisposable. When it's disposed, it
hands the buffer back to the pool. In that way, it's just like other
resources. I'm not sure if this is what you're talking about though, but
I thought I'd just mention it.

-- Barry

--
http://barrkel.blogspot.com/
Jul 24 '06 #6
Jim

"Barry Kelly" <ba***********@gmail.comwrote in message
news:6o********************************@4ax.com...
"Jim" <ji*@nospam.plswrote:
>Thanks to *both of you* for good suggestions. I may be forced to go the
memory pool route, but it does violate a ground rule here: I would
rather the user's design dictate the resources consumed (there are many
other objects besides the array I described earlier), rather than
having the resources dictate one facet of the largest design.

I have my own pooled array generic template, and I use a wrapper
PooledArraySlice<Twhich implements IDisposable. When it's disposed, it
hands the buffer back to the pool. In that way, it's just like other
resources. I'm not sure if this is what you're talking about though, but
I thought I'd just mention it.

-- Barry

--
http://barrkel.blogspot.com/
Hmm. Anything you'd care to share?

Jim

Jul 24 '06 #7
"Jim" <ji*@nospam.plswrote:
Barry wrote:
I have my own pooled array generic template, and I use a wrapper
PooledArraySlice<Twhich implements IDisposable. When it's disposed, it
hands the buffer back to the pool.

Hmm. Anything you'd care to share?
I'll post it here, but be aware that it's part of a larger library that
is under current development (it's based around ideas developed for a
past project), and it uses a helper class to create exceptions, not
included. Use it for educational value, no more :) I've also removed its
namespace etc.

-- Barry

--
http://barrkel.blogspot.com/
Jul 25 '06 #8
"Jim" <ji*@nospam.plswrote:
Hmm. Anything you'd care to share?
Oh, and I just added that 'this[int index]' property, but it has a bug -
it uses '_index' rather than 'index'. I normally don't access the array
slice directly, but via other mechanisms. The array pool is a lowly
component in a greater scheme :)

-- Barry

--
http://barrkel.blogspot.com/
Jul 25 '06 #9

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

Similar topics

15
by: Spike | last post by:
Hi I'm using GCC (and NASM) to send over a asm table... BootDrv db 0 BIOS_Mem_Map_Entries dd 0 APM_Status db 0 APM_Major_Version db 0 APM_Minor_Version db 0 APM_Code_Base dd 0 APM_Data_Base...
4
by: Sharon | last post by:
Hi. I put a message box in the form designer, Dispose method. Clicking the form close (X) button, i noticed that the message, appears twice. My code does not call Dispose. What is happening...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
4
by: gladhuman | last post by:
Hi, Let's assume that I have the following structure and the following array: struc My_struct { char var_one; char var_two; char var_three;
13
by: Dave | last post by:
Could someone explain to me when it is appropriate to call the Dispose() method that is contained in most .Net Framework Objects. The MSDN says that Dispose: Releases all resources used by the...
20
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int...
5
by: jason735 | last post by:
Hi, I've got the following problem. I have to sort x*y elements which are in one file. I can use only an array for x elements and floor tmp files which can be read only forward. Thanks for...
2
by: andylotus | last post by:
Hi People I have a WinForm program. The UI contains a ToolStripDropdownButton, which is associate a list of menu items of ToolStripMenuItem, created dynamically. Upon different scenarios of user...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.