|
Hi am writing a chat server and I was stress testing my server and found some errors.
Say I have an arraylist.
ArrayList sockets = new ArrayList(listOfSockets);
Now I call Socket.Select to take out the sockets that don't have data on them.
Socket.Select(sockets, null, null, 100000000);
This is where the error happens, if I pass in a ArrayList that has 65 sockets in it.
It causes a IndexOutOfRangeException to happen. I have tried using other ways of doing it and it causes different errors but it always happens when it reaches 65 sockets.
I am dumbfounded please help.
------------------
I'm naked under my clothes...
|
|
|
|
|
That's badass how your name scrolls!!! Can you teach me your hax0rz skillzE?@!??
/\ |_ E X E GG
|
|
|
|
|
Hello everyone,
I'm working on imaging software in C#, but there's problem with memory.
For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app.
I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change.
I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory.
I'm afraid that times of malloc() and free() are gone, but...
|
|
|
|
|
ltinka wrote:
I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change.
You are right that GC doesn't do anything. GC is very lazy. Because of performance it basically collects memory when there isn't enough free memory, or when you tell it to do so (calling GC.Collect() ) Here[^] is a lot information about GC.
Task manager shows memory allocated for a process, and this is actually bigger than memory used. IIRC that's what execution engine (I think) does - allocating is expansive operation, so it tries to minimalize allocation (by allocation bigger blocks of memory at once).
ltinka wrote:
When I force thumbnail image to dispose (calling Image.Dispose()), nothing change.
You should always do that. Exactly for very reason how GC works.
David
|
|
|
|
|
Firstly, make sure you don't have any references to your images: if there are still references to the images in memory, the garbage collector will not collect the in-memory images.
Finally, as a way to determine if your images are being cleaned up, try calling
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
And the garbage collection will kick into action and reclaim unused memory. If your memory is still high, you've got some references to the images still hanging around.
Also, when you're done with an image, call Dispose() on it. It'll free up some resources immediately before the GC ever gets around to reclaiming the memory.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Conversation With a Muslim
Judah Himango
|
|
|
|
|
dhn is partially correct. The GC is pretty lazy, but not as bad as he makes it sound. The GC Scheduler makes it's own determinations on when to run the GC process. MANY factors cause the Scheduler to either slow down the schedule or speed it up.
It doesn't run only when your running out of memory. For instance, if you suddenly dispose of a large number of objects, then the GC will kick in early to clean up and compact memory. If you only disposed a few large objects, the GC won't collect them until it's normal schedule.
Basically, looking at the Task Manager is not a good indicator of how much memory your application is actually using. What your seeing is appoxmiately how much unmanaged memory is allocated to your processes managed memory heaps, whether your using it or not. In other words, this is NOT a representation of how much unmanaged memory your application is actually using. The .NET Framework Memory Manager is also responsible for returning managed memory back to the unmanaged pool, if and when needed, or if requested by the system.
If you want to check to see how much memory check into using the Performance Monitor. For instance, according to the Task Manage, a little analog clock application that I wrote is currently occupying 13MB of Memory. The Performance Monitor says that it's more like 178KB.
Don't worry about it. The GC will collect on schedule and when it needs to. Any managed memory that needs to be released back to the system for other processes will be freed. It's just a matter of time and necessity.
Oh! BTW: Forcing the GC to collect is not a good idea unless you know exactly what your doing and why. Forcing collections will mess with the Scheduler's optimizer and can actually hurt your applications performance.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote:
dhn is partially correct. The GC is pretty lazy, but not as bad as he makes it sound.
Everything I say sounds bad these days cuz I feel like that. That is, bad. Yeah I was quite incorrect. Thanks. OTOH I belive he follows that link I gave him... so he can correct my mystifications
David
|
|
|
|
|
Make sure you dispose of absolultey every thing the moment you have writen it to the screen.
If you write a bitmap to screen... dispose of it.
if you draw a line dispose of the pen and/or brush and the graphics object as soon as you have drawn it.
Dont think because its on screen you should not dispose of it until you no longer need it on screen... draw it then dispose of every graphics element(Bitmaps, clones, pens, brushes, graphics objects) it used.
I wrote a simple app that used cloned portions of a bitmap as brushes and painted them to the screen... i didnt get many brushstokes done before i hit an "Out of memmory" error when depending soley on the GC.
|
|
|
|
|
Well, because you relied on the GC to call the .Dispose() methods for you, you we're using unmanaged system resources faster than the GC could free them. The GC doesn't track unmanaged resources used (short of memory anyway) so it can't tell that the system is running low on stuff like Handles. It's very unlikely you actually ran out of memory, rather it is more likely that you blew through tens of thousands of handles.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
ltinka wrote:
When I force thumbnail image to dispose (calling Image.Dispose()), nothing change.
You need to make sure you do this, or your memory usage will grow
ltinka wrote:
I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory.
If the GC has not kicked in before then, it will do so in time to stop this.
ltinka wrote:
I'm afraid that times of malloc() and free() are gone, but...
No, if you have large objects like media files or images in your app, you need to handle your own memory. malloc and free are long gone though, unless you came to C# from C and not C++.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thanks a lot. My app is in development for nearly 18 months and there are huge amount of code to repair by Dispose() calls ( ).
|
|
|
|
|
Im trying to develop an application that will login to an Exchange Server (using 2000 at my job) and poll to see if any new messages are in a particular inbox i.e. my own.
Any suggestions on how to do this in C#? I'm relatively familiar with C# and if any clarifications are needed on what I'm trying to do, please let me know.
Josh Kierpiec
|
|
|
|
|
Hello,
I'm writing my own print preview window using a PrintPreviewControl . I can't find where to get the total number of pages (so I can provide page navigation.) PrintPreviewDialog does it, but I can't find a property.
I've thought on incrementing the StartPage property until it throws an exception (will it? the docs say nothing) but there should be a better way.
Any ideas? Thanks!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I've solved my problem, so I'll share with you
In the contructor I set the StartPage property to a huge number (say, 1 billion). Then I install the StartPageChanged event handler. After the print preview is generated, I get that event, with StartPage set to the last. I save the page number, and then restore it to the first page.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Does anyone know what needs to be done to programmatically check or uncheck a checkbox in Excel using C# (or even VB.Net)? I already have the code to open an excel document, change cells, update links, save, and close, but the checkboxes are killin me.
I have tried using the Worksheet.CheckBoxes(object Index) method to try and retrieve a CheckBox object, but it doesn't work. It returns an obscure COM error. I tried it by using both the checkbox name and a number for the Index parameter. When attempting to generate a macro in excel to change the value of the checkbox, no code is generated when performing actions to the object. I don't want to have the checkbox link to a cell, and change the cell to control the checkbox -- that is lame. But if no one can find a solution, I may have to do it that way.
-----------------------
I pity the foo.
-Mr. T
|
|
|
|
|
I just cant' get how to draw strings with "proper" letter spacing,
especially when they're bold. Some letters in specific combinations
when being drawn next to each other appear slured (without even a pixel gap).
The font I'm using is the default MS Sans Serif 8,25pt
Here's a link to a sample screenshot:
>> link
The left one is drawn using the Graphics.DrawString method and the right one is
a screenshot from a ListView. Why is that difference?
How to achieve this?
|
|
|
|
|
iliyang wrote:
How to achieve this?
May be you must change the StringFormat class ?
|
|
|
|
|
Well, turns out that it is a global issue and there's no way
(at least that I know) that can fix this. Thе string that I
drew was something like "bla bla the_messed_part" (1) and the string
in the listview was "the_messed_part" (2). And when I tried to
draw the string (2) or let the listview draw the string (1)
both work the same way. So I guess the solution is to choose
a different font like Tahoma, for example, which looks like MS Sans Serif.
Of course, this is not a very good solution, 'cause this way I'm
fixing the font and if the user has a totally different system font
set, it'll be pretty ugly...
|
|
|
|
|
I have successfully written a C++ DLL and can pass integer types from C++ to C# using the DllImport method. However I can not figure out how to pass a string (byte array or any other type) from my C++ dll to C#.
The C++ DLL side compiles without error and creates a DLL file. The C# side produces compiler errors no matter how I try to pass the string (have tried everything I can think of). Since Interoperation is a
"virtue" in Visual Studio I am sure that there is a simple solution but it eludes me. Here are the relevant code sections:
In C++ DLL header (.h)
extern "C" __declspec(dllexport) int addfive(int x);
extern "C" __declspec(dllexport) char* putFolderList();
In C++ DLL source (.cpp)
int addfive(int i) { return i+5 ;}
char* putFolderList(){ return "my string" ;}
In C#:
using System.Runtime.InteropServices;
[DllImport("DLLBuildTest.dll")]
static extern int addfive(int x); // VALID
static extern char* putFolderList(); // NOT Valid
errors (2) and warnings (1) from C# compilation:
#1 - warning CS0626: Method, operator, or accessor 'DLLCaller.Form1.putFolderList()' is marked external and has no attributes on it. Consider adding a DllImport attribute to specify the external implementation
@2 - error CS0214: Pointers may only be used in an unsafe context
#3 - error CS0029: Cannot implicitly convert type 'char*' to 'string'
My question is: What is a valid method for passing a string from a C++ DLL to a C# program?
Paul Largent
|
|
|
|
|
Doesn't
static extern string putFolderList();
help?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
I've created a TreeView which lists items from active directory. With the help of another Code Project user, I've created a context menu which is specific to the type of object right-clicked-on by the user.
The problem is that when I select the menu option, the 'sender' holds a reference to the MenuItem, not the TreeNode clicked-on in the first place.
How can I get a reference to the original TreeNode that the user right-clicked on?
I've thought of one way but it seems a bit of a botch: to create a TaggedMenuItem class inherited from MenuItem which has a 'Tag' like Controls have and to use the MouseUp event and populate the Tag of EVERY TaggedMenuItem on the context menu with a reference to the TreeNode as I create the context menu. Then, I can get back to it from the menu item click event handler. Like I said, this sounds a bit crap.
Is there a better way?
|
|
|
|
|
You can subscribe to the MouseDown event and figure out which node was right-clicked on with the help of the mouse coordinates.
Point point = new Point(e.X, e.Y);
TreeNode node = feedTreeView.GetNodeAt(point);
You can then store node in a class level member variable, which you can then access from anywhere within the class.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thanks for your suggestion. I'm new to OO and I think I was just over-complicating things!
Regards
SH
|
|
|
|
|
How does a HashTable stores data ? it stores inside itself or a HashTable stores only the adress of the object ?
Thanks
|
|
|
|
|
If you store reference types, it stores only references (adresses). If you store value types, I belive it stores values (copies).
David
"Stay Kul And Happy" I.A.
|
|
|
|