Click here to Skip to main content
15,914,014 members
Home / Discussions / C#
   

C#

 
Questionmemory allocated on heap in method Pin
devvvy29-Jan-09 2:14
devvvy29-Jan-09 2:14 
AnswerRe: memory allocated on heap in method Pin
Rob Philpott29-Jan-09 2:17
Rob Philpott29-Jan-09 2:17 
QuestionRe: memory allocated on heap in method Pin
Eddy Vluggen29-Jan-09 2:22
professionalEddy Vluggen29-Jan-09 2:22 
AnswerRe: memory allocated on heap in method Pin
devvvy29-Jan-09 2:27
devvvy29-Jan-09 2:27 
AnswerRe: memory allocated on heap in method Pin
Eddy Vluggen29-Jan-09 2:33
professionalEddy Vluggen29-Jan-09 2:33 
GeneralRe: memory allocated on heap in method Pin
S. Senthil Kumar29-Jan-09 3:04
S. Senthil Kumar29-Jan-09 3:04 
AnswerRe: memory allocated on heap in method Pin
Guffa29-Jan-09 5:21
Guffa29-Jan-09 5:21 
AnswerRe: memory allocated on heap in method [modified] Pin
Luc Pattyn29-Jan-09 5:30
sitebuilderLuc Pattyn29-Jan-09 5:30 
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.

Smile | :)
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

GeneralRe: memory allocated on heap in method Pin
N a v a n e e t h29-Jan-09 5:51
N a v a n e e t h29-Jan-09 5:51 
AnswerRe: memory allocated on heap in method Pin
Luc Pattyn29-Jan-09 8:54
sitebuilderLuc Pattyn29-Jan-09 8:54 
GeneralRe: memory allocated on heap in method Pin
N a v a n e e t h29-Jan-09 15:44
N a v a n e e t h29-Jan-09 15:44 
AnswerRe: memory allocated on heap in method Pin
Luc Pattyn29-Jan-09 15:52
sitebuilderLuc Pattyn29-Jan-09 15:52 
Questionhow to vertically scan an image in c# Pin
Swati Khanna29-Jan-09 1:58
Swati Khanna29-Jan-09 1:58 
AnswerRe: how to vertically scan an image in c# Pin
musefan29-Jan-09 1:59
musefan29-Jan-09 1:59 
GeneralRe: how to vertically scan an image in c# Pin
Swati Khanna29-Jan-09 2:02
Swati Khanna29-Jan-09 2:02 
GeneralRe: how to vertically scan an image in c# Pin
musefan29-Jan-09 2:03
musefan29-Jan-09 2:03 
GeneralRe: how to vertically scan an image in c# Pin
Swati Khanna29-Jan-09 2:08
Swati Khanna29-Jan-09 2:08 
GeneralRe: how to vertically scan an image in c# Pin
musefan29-Jan-09 2:13
musefan29-Jan-09 2:13 
GeneralRe: how to vertically scan an image in c# Pin
Eddy Vluggen29-Jan-09 2:21
professionalEddy Vluggen29-Jan-09 2:21 
GeneralRe: how to vertically scan an image in c# Pin
musefan29-Jan-09 2:30
musefan29-Jan-09 2:30 
GeneralRe: how to vertically scan an image in c# Pin
Swati Khanna29-Jan-09 2:35
Swati Khanna29-Jan-09 2:35 
Questionstacking of 2d images Pin
Nettai29-Jan-09 1:15
Nettai29-Jan-09 1:15 
AnswerRe: stacking of 2d images Pin
musefan29-Jan-09 1:33
musefan29-Jan-09 1:33 
GeneralRe: stacking of 2d images Pin
Nettai29-Jan-09 1:49
Nettai29-Jan-09 1:49 
GeneralRe: stacking of 2d images Pin
musefan29-Jan-09 1:56
musefan29-Jan-09 1:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.