473,395 Members | 1,870 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.

ArrayList outofmemoryexception

This line throws outofmemoryexception:
for (iYear = 0; iYear <= oYearsList.Count - 1; iYear++)
{
sHtml.Append("<option value='" + oYearsList.Add(iYear)
+ "'>" + oYearsList.Add(iYear) + "</option>");
}

However this work:
for (iYear = 0; iYear <= oYearsList.Count - 1; iYear++)
{
sHtml.Append("<option value='" + iYear + "'>" + iYear
+ "</option>");
}

Is there any problem when you use ArrayList.Add

Jul 20 '07 #1
4 3168
Well, when you call Add on the ArrayList, you are adding another item to
the oYearsList, incrementing the Count. Since you are adding two new items
per line (even if you were adding one, you would have this issue) the Count
property increases by one, and will never be reached by iYear. You end up
in an infinite loop, and then you run out of memory.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<br************@gmail.comwrote in message
news:11**********************@m37g2000prh.googlegr oups.com...
This line throws outofmemoryexception:
for (iYear = 0; iYear <= oYearsList.Count - 1; iYear++)
{
sHtml.Append("<option value='" + oYearsList.Add(iYear)
+ "'>" + oYearsList.Add(iYear) + "</option>");
}

However this work:
for (iYear = 0; iYear <= oYearsList.Count - 1; iYear++)
{
sHtml.Append("<option value='" + iYear + "'>" + iYear
+ "</option>");
}

Is there any problem when you use ArrayList.Add

Jul 20 '07 #2
Well how can I resolve this

Jul 20 '07 #3
Don't call the Add method on the list you are iterating through. What
exactly are you trying to do in the loop? It looks like you are trying to
add an item to the loop with each iteration. But you will never reach the
end of the loop that way.

What is the logic supposed to be here?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<br************@gmail.comwrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
Well how can I resolve this

Jul 20 '07 #4
On Fri, 20 Jul 2007 12:05:22 -0700, <br************@gmail.comwrote:
Well how can I resolve this
Um. Either stop adding stuff to the list you're using as the termination
condition for your loop, or stop using as the termination condition for
your loop the count of the list you keep adding stuff to.

You might get a better answer if you could explain why it is you feel you
need to add new members to your list each iteration through the loop.
It's hard to say given the code you posted, since neither code example
seems all that useful. Maybe you meant to write something like this
instead:

for (iYear = 0; iYear < oYearsList.Count; iYear++)
{
sHtml.Append("<option value='" + oYearsList[iYear].ToString()
+ "'>" + oYearsList[iYear].ToString() + "</option>");
}

Which simply takes each object in the list, converts it to a string, and
includes the value in your output.

There are other aspects of the code that are troubling, such as the fact
that you're appending what looks like XML to something with "Html" in the
name, the fact that you appear to have redundant information in your
output, and the fact that there's no other formatting to the output (like
indentation and line breaks) that would make the output more readable by
humans (obviously not strictly necessary, but it sacrifices one
significant benefit of using text for data output).

But maybe those are all non-issues. You haven't described what you're
actually doing with enough detail for me or anyone else here to say for
sure those are problems.

Finally, just for completeness, here is a version of your code that
changes the behavior the least, while still fixing the infinite loop:

int cyears = oYearsList.Count;

for (iYear = 0; iYear <= cyears - 1; iYear++)
{
sHtml.Append("<option value='" + oYearsList.Add(iYear)
+ "'>" + oYearsList.Add(iYear) + "</option>");
}

All that does is get the count once, rather than retrieving it each time
through the loop. The rest of the code still looks wrong to me, but
hey...if that's what you really really wanted to do, the above is how to
get it to work as you presumably intended it to.

Pete
Jul 20 '07 #5

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

Similar topics

2
by: Peter Aberline | last post by:
Hi all, We have written a C # .NET application and we're encountering memory problems in the form of System.OutOfMemoryException. Our application creates many thousands of objects in a...
4
by: Ryan Seghers | last post by:
I've got a program that has no user interface (like a service but not actually a Windows Service yet) in which I'd like to handle OutOfMemoryExceptions. I'd at least like to log the failure before...
0
by: Per Bergland | last post by:
After many woes, I finally managed to get a stack dump of my System Service (written in C#) that insists on crashing when launched at system boot time (see below on how to get this dump - I...
1
by: Ripul Handa | last post by:
Hi We are running IIS 5.0 cluster with cisco local director. We are running a website on 2 webservers and I have been observing that from past few days we have are getting this error message of...
1
by: SMG - Idealake | last post by:
Hi all, I am getting following error on my error, what could be the reason? Exception of type System.OutOfMemoryException was thrown. Description: An unhandled exception occurred during the...
2
by: Dave | last post by:
We just started getting this error message in our application today (stack trace below). From the OutOfMemoryException, I'm guessing it could be a memory leak. I'm making sure I'm closing all my...
1
by: Ashkan Daie | last post by:
Hi All, When trying to install a performance counter via InstallUtil I get the following exception: Creating performance counter category Enterprise Library Caching. An exception occurred...
13
by: Venkatachalam | last post by:
Hi, In my application I have text(flat) file as input and I have to generate an XML file. The maximum input text file size can be 900MB and gererated xml may result 2+ GB. Based on the first...
8
by: =?Utf-8?B?UGlnZ3k=?= | last post by:
Hi to all, I am getting this System.OutOfMemoryException calling the Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream>,<Obj>) method. The type of <streamis...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...

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.