|
|
In my code I am manipulating/creating/destroying in memory representation of big XML file (as in a few MB of CDATA in them (embeded bitmaps)).
At some stage (and I am in no way under any memory stress as my application is using only about 500MB of memory and I have 1GB free, without counting the virtual memory) I have this out of memory exception happening, in code snippet looking like that:
=====
public static void WriteXml(System.Xml.XmlWriter writer, IEnumerable<string> resources, IResourceProvider provider)<br />
{<br />
writer.WriteStartElement("Resources");<br />
<br />
writer.WriteElementString("ResourceProvider", provider.ID.ToString());<br />
List<string> resourcesWritten = new List<string>();<br />
foreach (string resource in resources)<br />
{<br />
if (resourcesWritten.Contains(resource))<br />
continue;<br />
writer.WriteStartElement("Resource");<br />
byte[] data = provider.LoadData(resource);<br />
writer.WriteElementString("ResourceName", resource);<br />
string b64Data = Convert.ToBase64String(data);<br />
writer.WriteElementString("base64Data", b64Data);<br />
writer.WriteEndElement();<br />
resourcesWritten.Add(resource);<br />
}<br />
writer.WriteEndElement();<br />
} =====
the error stack is:
====
System.Xml.dll!System.Xml.XmlWellFormedWriter.WriteString(string text) + 0x55 bytes <br />
System.Xml.dll!System.Xml.XmlWriter.WriteElementString(string localName, string ns, string value) + 0x29 bytes <br />
System.Xml.dll!System.Xml.XmlWriter.WriteElementString(string localName, string value) + 0xb bytes <br />
> NovaMind.Data3.dll!NovaMind.Data.ResourcesHelper.WriteXml(System.Xml.XmlWriter writer = {System.Xml.XmlWellFormedWriter}, System.Collections.Generic.IEnumerable<string> resources = {NovaMind.Data.NMMapBranch.get_ResourcesInUse}, NovaMind.Data.IResourceProvider provider = {NovaMind.Data.NMDocumentPackage}) Line 27 + 0x13 bytes C# ====
When I look at the internal of the various object involved with the debugger I could see that:
data.Length ~= 7 Mb (~ 7 e+6)
and "Writer" is wrapper around a StringBuilder which MaxCapacity is around 2 GB (2e+9) and which current Length is "only" about 25 Mb (25e+6).
Now, all these numbers are huge I agree, however my memory isn't stressed at all (i still used much less than my physical memory, virtual memory notwithstanding)).
Any idea why I have OutOfMemory exceptions? how to get rid of them?
|
|
|
|
|
Hello,
How are you instanziating the XMLWriter class?
)
If you are using an IO.Stream, you should "Flush()" the XmlWriter and of course "Dispose()" the Stream. (use a using Block for that):
using(MemoryStream ms = new MemoryStream())
{
XmlTextWriter xtw = new XmlTextWriter(ms, System.Text.Encoding.UTF8);
.
.
.
xtw.WriteEndElement();
xtw.Flush();
}
)
If you are using an path, you should "Close()" the XMLWriter.
XmlTextWriter xtw = new XmlTextWriter(yourPath, System.Text.Encoding.UTF8);
.
.
.
xtw.WriteEndElement();
xtw.Close();
Hope it helps!
All the best,
Martin
|
|
|
|
|
I don't think it helps because I haven't finished my write operation yet when the error happen!
I create it like that:
StringBuilder builder = new StringBuilder();<br />
System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(builder);
|
|
|
|
|
Hi,
I am developing a C# windows application(.net 2003).
The functionality of this application is to import all the records from a csv file to a table in a SQL Server 2000.
For this i am opening a sql connection and I am using Sql command. Here i set sqlcommand timeout to 500.
I am using a for loop to insert each record into sql table. After finishing all records i am closing and disposing sql connection.
This is working fine.
For some tables it is necessary to get primary key from another table and insert that primary key along with the current table's data.
For extracting that primary key i wrote funtion. This function opens a new sql connection and sql command having timeout 500.
After fetching data i am disposing that sql connection.
This funtion is called for each record in the csv file.
After inserting approximately 200 records it is giving an Exception Message
"Timeout Expired. The timeout period elapsed prior to obtaining a connection form the pool. This may have occured because all pooled connections were in use and max pool size was reached".
How to overcome this problem. It's urgent
Any one please help me.
Thanks in Advance.
Ramu
|
|
|
|
|
Ramu.M wrote: For some tables it is necessary to get primary key from another table and insert that primary key along with the current table's data.
For extracting that primary key i wrote funtion. This function opens a new sql connection and sql command having timeout 500.
Are you using stored procedures ? If yes then you can get this primary value through an output variable. This will avoid creating connection again.
Ramu.M wrote: After inserting approximately 200 records it is giving an Exception Message
Verify your code to find how many instances of SQLConnection will be open when it reaches 200 ? Try to reduce the number of connection objects open. Make sure that all SQLConnection instances are returned back to pool.
How you are disposing the objects ?
SqlConObject.Open()
SqlConObject.Close()
SqlConObject.Dispose()
Above code will keep the connection in opened state when any error happens on do operations. So it's better to wrap the connection in between using statements. This will dispose object even though any error happens.
using (SqlConObject)
{
SqlConObject.Close()
}
|
|
|
|
|
|
Hy,
Have you tried creating a buffer or such? I mean to create a list with your queries first. When you insert a new query to the list you call the function to get the UID and then fetch the data from the CSV file and then create the query. After you have finished creating the query list just open a connection to the server and loop inside the list to insert the data.
Hope it helps
Do your best to be the best
|
|
|
|
|
|
Hi,
How to remove/delete any particular file from any particular folder/directory?
Thank you in advance
(Riaz)
|
|
|
|
|
System.IO.File.Delete(strFileName)
When you get mad...THINK twice that the only advice
Tamimi - Code
|
|
|
|
|
|
Hi,
I have created one form containing some controls like buttons, textbox, labels and all, and i want to write the Event Handler for some controls in a different class file.I am using C# windows application. How to do that???
Rakesh B.P.
|
|
|
|
|
By default event handler will be added in the same class file. So if you need to write event handler method in your other class, modify event handler reference in InitializeComponent() method to your new class.
EventHandlerClass EventHandler = new EventHandlerClass ();
this.button1.Click += new System.EventHandler(EventHandler.button1_Click);
Why you want to split your event handler code to other class ?
If you are using .NET 2005, then you can make use of partial classes. Class name will be same, but you can keep the events in separate file.
Other way is using delegates.
|
|
|
|
|
Hi,
I have a big class with plenty of methods. But some of the methods inside this is not using now. So all these methods has to be removed which have no refernce. Is there any way to know which are the methods that is not having reference ?
|
|
|
|
|
You can right click on the method and choose 'find all references' ( from memory ) it finds all the places that call a function. Or you can just search the solution for the function name. Dong it for all in one go, not that I know of.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Christian Graus wrote: You can right click on the method and choose 'find all references' ( from memory ) it finds all the places that call a function.
Thanks Christian. This what I am doing now. But it takes lot of time. And I have got lot of function to go. So I thought any way to list all the unreferenced functions. Do you know about Redgate profiler ? will that help on this issue ?
|
|
|
|
|
How do you close a C# application with more than one form? I can close the forms but the application keeps running in the task manager's processes. Can anyone help me out ?
Girish Soni
|
|
|
|
|
sonigirish wrote: I can close the forms but the application keeps running in the task manager's processes. Can anyone help me out?
You have to close the main form(where main() function resides). Once you close the main form, it will be removed from process list.
|
|
|
|
|
Thanks for reply I am having two forms when i click button on form1 then form1 will close and form2 will open. I want to close application by clicking on button on form2.
Girish Soni
|
|
|
|
|
sonigirish wrote: i click button on form1 then form1 will close and form2 will open. I want to close application by clicking on button on form2.
In which form your main() method resides ? How you are closing form1 when form2 is open ? Are you just hiding form1 when form2 shows ?
|
|
|
|
|
My main method is in program.cs and my first form is form1 and on button click event code is
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2() ;
frm.Show() ;
}
I want to close application on form2' button click ie button2-Click event
Girish Soni
|
|
|
|
|
sonigirish wrote: I want to close application on form2' button click ie button2-Click event
Write a method in your first form which closes that form. Using delegates, call this method when form2 is closed. This will close your entire APP.
Alternatively, you can pass form1 object to form2 constructor, and dispose this object when form2 closed.
|
|
|
|
|
hi
u can use Application.Exit() where evr u plan to terminate the application. for eg. if after showing form1 u want to close it, then on lets say btnExit_click event, u can use Application.Exit() to close the entire application and this would also remove the application thread from the task manager. This method basically terminates the application domain which the application is running in.Hope this would help.
Cheers
|
|
|
|
|
If your app is running with all forms closed, you must have a thread running. What if you debug, close all and hit the break menu item to see where the code is ?
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|