|
The object is up for garbage collection as soon as it's not used any more.
Actually, setting the reference to null is totally pointless in this scenario. The garbage collector knows that the object is not used any more after the call to DoSomeWork, so the object may even be collected before you set the reference to null.
It's not the scope of the variable that decides garbage collection, only usage. The garbage collector only cares about where the objects are used, it's just the compiler that cares about the scope of variables.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi,
I disagree with what all others said on the subject.
Here are some facts:
1. if o.DoSomeWork(); stores a reference to o somewhere (using this inside the DoSomeWork method), say in a static List< SomeObj>, then o will not be collectable as long as the list lives, no mather how often you do o=null; or return;
Assuming DoSomeWork does not cause a reference to be stored:
2. return; does not cause the gc to run.
3. when SomeMethod() returns, o is collectable , which means it could/should be collected by the gc if there were a reason for the gc to run. Normally the only reason for gc to run is an explicit need for memory, as in SomeThingElse els=new SomeThingElse(); anywhere in your process. Without a need for memory, no gc run, no collection at that point.
4. if o were the only reference to your SomeObj, then setting it null, removes the last reference to the object, and hence makes it collectable (again, not collected). Having the method return also removes the last reference, hence nulling a reference does not make sense just before a return. It does make sense when a lengthy operation sits in between o=null; and return; though.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:56 AM
|
|
|
|
|
Luc Pattyn wrote: if o were the only reference to your SomeObj, then setting it null, removes the last reference to the object, and hence makes it collectable
Well, My understanding was setting null will not make any difference for garbage collection. I believe GC is brilliant enough to know when the object is no more used.
I agree to all other what you said
|
|
|
|
|
N a v a n e e t h wrote: setting null will not make any difference for garbage collection
yes it does. The GC looks for references "everywhere" while performing its "mark pass" (assuming a simple "mark and sweep" type GC). Every time you remove a last reference somewhere (e.g. by writing null over it), you make the referred object collectible (i.e. it no longer gets marked as "in use", so the sweep pass will destroy it). The GC is *not* analyzing your code to predict whether an object will still be used in the future, it only determines "reachability", i.e. the theoretical possibility to access it again, based on the data structures only.
In the meantime I have developed a little app that demonstrates exactly how the GC works. When I find
the time, I will write a short article on the subject.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Luc Pattyn wrote: The GC is *not* analyzing your code to predict whether an object will still be used in the future, it only determines "reachability", i.e. the theoretical possibility to access it again
So do you usually set null when you are done with the object in your projects? I have seen very less projects doing this. You may interest in this[^]. In that post, author says,
Setting things to null
So, should you set your reference types to null when you're done with them? Of course not. Once a variable falls out of scope, it's popped of the stack and the reference is removed. If you can't wait for the scope to exit, you likely need to refactor your code.
I have seen similar explantion in CLR via C#[^] book by Jeffrey Richter.
Luc Pattyn wrote: In the meantime I have developed a little app that demonstrates exactly how the GC works. When I find
the time, I will write a short article on the subject.
WOW! that is going to be true fun. When we can expect that? Looking forward to read the article.
|
|
|
|
|
Hi,
N a v a n e e t h wrote: So do you usually set null ...
No, I don't because my code typically does not satisfy the last condition in my earlier point 4.[^]
But I would when circumstances were such that it would make sense.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
how do i scan an image column wise in c# . i need help with the code . i wanted to read the next pixel of the same column .please help im a beginner in image processing
Thanks in advance
|
|
|
|
|
well if your image is a bitmap you can create a 2D array of pixels then loop through columns and rows
actually just use:
Bitmap b = new Bitmap(filepath);
for(int i = 0; i < b.Width; i++)
{
for(int j = 0; j < b.Height; j++)
{
Color c = b.GetPixel(i, j);
}
}
|
|
|
|
|
do u mean i can use getpixel and setpixel method to solve this....i was intending to use unsafe code so that it could be faster....
|
|
|
|
|
what do you want to do with the pixels?
|
|
|
|
|
i am trying to segment characters from a string of characters in a license plate image....i want to scan my image column wise so that i can extract every character ....so i need to find how many black pixels are present in a column....
|
|
|
|
|
Good luck lol :P
You do have to go through the pixels though, so I would stay with my GetPixel() suggestion. I dont know of another alternative to getting the pixels sorry (unless you read the file yourself, but then you would have to store pixels as an array, and loop through them yourself)
Just out of interest - what algorithms will you be using to detect the letters?
|
|
|
|
|
There's some articles on CP that deal with OCR. Would still be a challenge, I guess
I are troll
|
|
|
|
|
ahhh.. V.Interesting.
Might take a look at doing something like that if i ever get the free time lol
The office MODI method looks like the simplist approach, cant say that be good for speed thou :P
|
|
|
|
|
thanku all ........if any 1 can still help i'll be highly obliged..........
|
|
|
|
|
i am having set of 2d images....and i want to stack those images to visualize 3d image..i didn't get any idea.....i was searching in google,but i couldn't get exact details.....if anyone help me by giving any article or idea related to this......
|
|
|
|
|
What?!? what is the 3D image meant to look like? what do the 2D images represent?
|
|
|
|
|
i am doing project in computed tomography.....i have 2d images of full hand....i have to stack the 2d images to visualize the full hand
|
|
|
|
|
hmmm... i think you may be in the wrong place for this kind of question.
How much do you know about 3D graphics. If not alot you will need to start from the basics of 3D graphics.
If you can render 3D models(so to speak) then you could create a 3D array of pixels based on your 2D images. I assume your 2D images are RAW files? or something similar, i.e. two colors, 1 color for object, another color for space
|
|
|
|
|
Like a volume texture?
You may have to ray trace it to get your final (2d) view..
Although there are probably better ways.
|
|
|
|
|
How you do it depends on how your images are stored.
Is each image in a separate file? Are they bitmaps, jpegs or some other format.
I once did something similar where the images (horizontal slices through a head) were stored in a set of files.
Each file contained a 2D image as a sequence of integers where an integer corresponded to a pixel. I.e. the file was essentially a 2D array of pixels.
To get the 3D image I had to get the corresponding pixels from each file and monkey around with them. In other words I treated the set of files as a 3D array so I could take slices in horizontal or vertical directions.
Hope this helps but without knowing exactly how the images are stored I can't be more specific.
Regards
David R
|
|
|
|
|
Hi,
I want to know which process had called my application built using C# .NET.
Example:
Their is an application named "Example1.exe" build using C# .NET. When you open the file manully by double clicking or by any other way then, "Explorer.exe" is the parent process of my application. In this case I want to find that "Explorer.exe" is my parent process. Please help me with the code which has to be placed inside the Main() method of my application (Example1.exe) to find this.
Normaly when it is double clicked this is not important but in case if my application is called by any other application then i had to work accordingly.
ashok
|
|
|
|
|
|
Hi all,
i am new in C# environment.
i try to create a desktop application.
i have 2 classes (form1 & createXML)
the form1 located a textBox that called txtPhoneNumber.
now, when the user will write his phone number the application will send him an SMS.
the problem is that the txtPhoneNumber.Text doesn't send the user number to the createXML class.
the XML is look like:
strBuild = strBuild.Append("<to>"); //Destinations
strBuild = strBuild.Append(inputForm.txtPhoneNumber.Text); //input from the form1 Class
strBuild = strBuild.Append("");
but in the debug i see that there is no number. and there is no any hint for error.
any idea?
please help!!!
|
|
|
|
|
i didn't ignore the HTML tags........
fixed XML builder:
strBuild = strBuild.Append("<TO>");
strBuild = strBuild.Append(inputForm.txtPhoneNumber.Text);
strBuild = strBuild.Append("</TO>");
|
|
|
|