C# - .NET Constructors And Memory Management

May 10, 2011

Coming from more low-level languages like C++, and seeing how transparent .NET memory management is, I've got a concert about a piece a line of code I've written. In C++, every object necessarily (dictated design practices and peculiarities of memory management) needs to have a constructor and a destructor. In .NET, destructors aren't needed as often, and there are different patterns of when they are required and how to use them. My question is this. If I have the following like of code (in VB.NET, but equally applies to C#)

[Code]...

View 4 Replies


ADVERTISEMENT

Performance In Case Of Memory Management

Nov 8, 2011

a) Dim strName as string
For i=0 to 100
strName=GetName()
do something with strname
Next

b) For i=0 to 100
Dim strName as string=GetName()
do something with strname
Next
Out of a and b which will be faster and consume less memory

Public sub GetResourceAttributes()
Dim colreSource as object
.using the colresource
colresource=nothing
end sub
Is sentencing'colresource =nothing'is good programming technique or is it enough to leave nothing done on 'colresource'. Like GC will take care of 'colresource' when there is no reference to the object.

View 2 Replies

VS 2008 Memory Management & Threading?

Sep 28, 2010

I have an application that requires the use of many threads. How do i monitor the memory consumption of my application?And how can i monitor the CPU usage of my application if possible?And is it possible to monitor the memory consumtion and CPU usage of each of these threads?And is it possible to limit or prioritize the memory consuption or cpu usage of the whole application or of each thread?

View 5 Replies

Memory Management - Reducing The Commit Size Of Application

Oct 15, 2009

I have an application which I've developed which queries Active Directory information and stores about 15 properties into a Datatable which is kept in memory. I need to decrease the amount of "Commit" memory it is setting aside. The application stays active in the system tray and allows for quick searching of information along with pictures for users. I have a class, which is able keep memory usage down pretty low. It runs on a timer every 10 seconds and listed below:

[Code]...

View 1 Replies

Memory Management When Insert Massive Data To MSSQL?

Apr 23, 2009

I have 200,000 rows data need to be inserted into MSSQL 2005 express. I have 4G memory. From the beginning, there is 2.0G used. Then, I use microsoft text driver to load text file into datagridview. Memory rise to about 2.8 GB.I start write data row by row from datagrid view. Memory usage keep rising , then OutOfMemory exception is catched.I try to use GC.Collect and for System.Data.Odbc.OdbcConnection , I also try .Dispose(). But it is no use.How can I keeing the memory within about 3.0GB for this usage ? I monitor .vhost.exe. It rise to more than 1G.

View 2 Replies

Constructors Calling Other Constructors In La C#?

Oct 23, 2009

In c# you can have

public class Foo
{
public Foo(string name)
{
//do something

[Code]...

Is there a VB.Net equivalent?

View 1 Replies

.NET And Multiple Constructors?

Nov 30, 2009

I would like to know if you can have multiple constructors in a VB.NET class? Like in java, you can have multiple constructors as long as they have different parameters, is that possible here?

View 1 Replies

VS 2008 Constructors Using With?

Jan 17, 2011

I did not know you could do this. See Article by Dan Mabbutt

'from article - Objects, Parameterized Constructors, Anonymous Types and the With Keyword
'by - Dan Mabbutt
Class someObject

[code].....

View 5 Replies

Controls That Have Constructors With Parameters?

Oct 22, 2009

I tried a search, but no luck. Maybe I was using the wrong words, I don't know.Here's my question...called myControl of type CustomControl. I have a property called SomeProperty that can be set in the Properties window. If I put this control on my form and set the property's value to "a string" normally InitializeComponents would look like this:

<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.myControl= New CustomControl

[code].....

View 5 Replies

DB/Reporting :: Datareader Has No Constructors?

Jul 7, 2008

im converting my classic vb class into vb.net class and im having some problems with my object.Query function

here is my obj.Query Function

Code:
Public Function Query(ByVal sqlStatement As String, Optional ByRef e As String = "") As SqlDataReader
Try

[Code].....

View 5 Replies

Inheriting Classes + Its Constructors?

Nov 2, 2009

i've got a class called prob that inherits system.exception however it seems not to have inherited the constructor

Public Class prob
Inherits System.Exception
public sub stuff

[code]....

throw new exception("test") 'works Throw New prob("test") 'doesn't work is it possible to inherit the constructor into my class prob as well?

View 11 Replies

Overload Constructors In A Class?

Apr 19, 2009

1)When we use Overload constructors in a class

2)and which one is get execut first.

Public Class remoteobj

[Code]...

View 4 Replies

Startup Forms And Constructors?

Mar 5, 2012

I was able to narrow down a problem to a certain point. I was hoping for an explanation or some light shed upon it. I went to Project/Project1/Application/Startup Forms. I found that when I included a "New" constructor in the form I wanted as startup, the form became unavailable for choosing as a startup form. I do not quite understand this because I noticed from work in another document that the form I want (call it StartupForm) actually has a basic "New" construction with no arguments, so that is why I do not understand quite what is happening here.

[Code]...

View 1 Replies

Type Byte Has No Constructors?

Jan 5, 2012

Why I am I getting the "type byte has no constructors" error here?

Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim image As Byte() = New Byte(stream.Length

View 3 Replies

VS 2008 Difference Between The 2 Constructors?

Mar 27, 2010

In one of the VB classes I have for a moving object there are 2 Constructors in the code as below:What is the difference between these 2 constructors?what does the first & the second constructor do?

[Code]...

View 4 Replies

VS 2008 How To Write Constructors

Nov 19, 2009

Public Class Car
Private Dim CarName as String
Private Dim CarWeight as String
Private Dim CarColor as String

[code]....

1.) In the book I am reading it says that when the default constructor(i.e. the one with no parameters) is executed all the class level variables get set to default values based on their type automatically by the compiler. For example, all String variables would be set to empty string("") and all Boolean variables would be set to False, etc. If you look at all my constructors you will see that non of them get values passed to them for all my class level variables. So my question is for the class level variables that don't get anything passed to them(ex:CarModel), do they take the same default values as they would if the parameter-less constructor were called?

2.) Following from my question #1, is it good practice to make sure that all the constructors that take parameters make sure they set every class level variable. Should I have written the above code as follows?

Public Class Car
Private Dim CarName as String
Private Dim CarWeight as String
Private Dim CarColor as String

[code]....

View 3 Replies

Generic Class's Constructors With Param

Dec 30, 2009

i've got a Class like this public Class Cart(Of Item) Public Sub New(ByVal a As Integer, ByVal ParamArray items As Item())but i do not see how to create an instance of it: Dim block_names As New Cart(Of String, 5I)i get something like "type expected" o.O

View 2 Replies

Handling Events Fired In Constructors?

Nov 8, 2009

Work = New ExampleWork()Here the Work is a withevents variable and I've used the handles clause for handling various events fired by the ExampleWork object. However the event handler will not get assigned till the constructor of the ExampleWork returns. Now how can I handle any events fired from the constructor? I can move the constructor logic out to a separate method and call it after the constructor has returned and thus handle all the fired events including events fired from constructor. However it doesn't look good

View 2 Replies

WebService Development - Constructors With Parameters

Mar 3, 2010

I'm fairly new to WebService development and have just set up my own webservice (ASP.Net 3.5, Visual Studio 2008 .asmx file). I cannot find a way of setting up my webservice to take parameters on the constructor. If I create a constructor that takes parameters, it is not then shown when I hook up to the webservice from my application (it only shows a parameterless constructor).

View 2 Replies

Winforms - Custom Constructors For Forms In .net?

Nov 1, 2010

I would like to open a new form from some other form, and pass some selected object from a control on that form to the new form. The sensible way to do this, I thought, was as a parameter to the forms constructor. Now I know that the visual studio GUI creates partial classes for my forms, that hold the properties that I can drag onto there in the designer. I assume it also holds a default constructor. Since it might do all sorts of stuff that is needed to initialise the form, I figured I should call it from my custom constructor ala

public sub new(byval my_parameter as Foo)
Me.new()
Me.my_parameter = my_parameter

[code].....

That clearly wasn't it, because it can't find a default constructor. The thing is, visual studio goes trough great lengths to prevent me from seeing the generated constructor, so I know how to access it. This leads me to believe that I am actually doing it wrong, and should have set out on some different path, as the path you are forced in to usually is the sensible thing to do, which I usualy find out way too late.

View 3 Replies

.net - Entity Framework Parameterized/Overloaded Constructors?

Mar 17, 2011

In my previous application using Linq2SQL I was able to overload constructors with parameters by doing this:

Namespace CoreDb
Partial Public Class Accomplishment
Public Sub New(ByVal accomplishmentTypeID As Object, ByVal description As String, ByVal title As String, ByVal applicableDate As DateTime, ByVal lastUpdatedBy As String)

[code]....

Basically using a partial class off the object, sharing the namespace of the .dbml file and calling the default constructor and then doing additional stuff. So then in my code I could do something like:

Dim accomplishment As New Accomplishment(id, description, title, applicableDate, lastUpdatedBy)

This seems to no longer work in Entity Framework as there is no default constructor to call.Does this no longer work? And if so what is a good alternative to implementing something like this?

View 2 Replies

Properly Use Nullable With Numeric Types In Constructors?

Jan 9, 2011

I have a class that implements a range of numbers....call it NumericRange(Of T). Internally, NumericRange stores T as a Nullable, T?. I have another class that wraps this class as NumericRange(Of UInt16). Call this class MyNumRange (I'm being simplistic here). So In MyNumRange, I have a few constructors defined:

[Code]....

View 2 Replies

Protected Constructors And MustInherit / Abstract Class

Jul 5, 2009

What is the difference between a class with protected constructors and a class marked as MustInherit? (I'm programming in VB.Net but it probably equally applies to c#). The reason I ask is because I have an abstract class that I want to convert the constructors to shared/static methods. (To add some constraints). I can't do this because it's not possible to create an instance in the shared function.

[Code]...

View 3 Replies

Loading An Image From A Jpeg That Exists In Memory (but Not On Disk) Using Direct Memory Access

Nov 5, 2009

In VB 2005, I am calling a C++ DLL function that returns the address in memory and size in bytes of a jpeg image. How can I load that jpeg image directly from memory into a picture box in my VB form? I cannot afford to save it to disk first.

View 10 Replies

Attempted To Read Or Write Protected Memory / Often An Indication That Other Memory Is Corrupt

May 22, 2007

Im getting a problem with one of my programs I have made in visual basic.NET where it gives me a memory error when i debug the program in the IDE. the exact error is this: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt". My program basically has a few forms with text fields on that get their text values from an access database on the same hard drive. Im wondering if im doing something wrong because my program just seems to eat up memory whenever it does anything. For example I have one form that has a combobox on it and when you change the drop down list selection it retreives a few strings from the database and puts them into the relevant text boxes, if you keep changing the selection then the memory usage (in task manager) just keeps going up and up. occasionally I get the error mentioned above when debugging but in my built version of the program it throws an exception everytime the memory usage gets past 49K.

Also I noticed when debugging in the "immediate window" frame I get the following message often:A first chance exception of type 'System. Invalid OperationException' occurred in System.Data.dll...Do I need to somehow be "releasing" the memory that is used to gather data once it becomes redundant?

View 12 Replies

Attempted To Read Or Write Protected Memory. This Is Often An Indication That Other Memory Is Corrupt

Nov 24, 2011

I am using the following code

This error occurs :

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Public Class FormRegEnumValue

[CODE]......................

View 2 Replies

Attempted To Read Or Write Protected Memory. This Is Often An Indication That Other Memory Is Corrupt?

Feb 1, 2009

QuoteSystem.AccessViolationException was unhandled Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Source="System.Windows.Forms"

Im designing a web browser and i continue to receive this error after going to about 3 websites it crashes with that error.

View 6 Replies

Necessary To Detach Event Handlers To Ensure That Memory Is Cleaned Up And That There Are Not Memory Leaks?

Jan 20, 2010

I have a BackgroundWorker object that I instantiated to perform a DB process on a background thread ansynchronously. I have event handlers for DoWork and RunWorkerCompleted. I can tell that the BackgroundWorker is disposing of itself because I added a message box into the Disposed event handler.Is it necessary to detach the event handlers to ensure that the memory is cleaned up and that there are not memory leaks?

View 1 Replies

Thread And Memory Leaks - Memory Usage Jumps About 1000k And It Never Goes Back Down

Apr 17, 2011

I've noticed that every time I start a new thread in my program, the memory usage jumps about 1000k and it never goes back down. I have ensured that the thread is no longer running, and only one instance of my worker thread is ever running at a time. I am using ThreadPool.QueueUserWorkItem and ASyncOperation to get stuff done. The program starts using a Sub Main in a module that uses STAThread; I read that using STAThread instead of MTAThread makes it impossible for the Garbage Collector to get in and consider objects for garbage collection. However, when I change the STAThread to an MTAThread, the WebBrowser control in my frmMain cannot be instantiated.

I get this exception:

Code: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll

Additional information: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. I do threading with the WebBrowser control, so will using MTAThread fix my problem? If so, how should I partition my code so that the WebBrowser control won't be affected by the above exception? I call the thread repeatedly with a timer (although only one instance ever runs at a time), and my program's memory usage starts at about 5000k and can jump to 300,000k+ depending on many times the thread needs to be called.

View 6 Replies

Create A Method SetValues - Call Multiple Constructors?

Dec 29, 2011

I'm a c# developer and have not enought experience in VB.NET.the scenario:

Namespace Presenters
Public Class BaseFooPresenter
assuming the public default parameterless constructor[code]....

I can create a method SetValues( ... parameters ... ) and call it from the two constructors but does someone knwos a workaround to avoid this error?, why the compiler do not validate the possibility to call the base constructor before the overloaded constructor?how to justify logically the fact that it's not possible to call the base class constructor and another class level constructor from one class level constructor at the same time?

View 3 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved