Can C# GC move memory objectsHow do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?Deep cloning objectsHow do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Why not inherit from List<T>?
How can an advanced civilization forget how to manufacture its technology?
How did the hit man miss?
Wrapper in return method for test class
Would letting a multiclass character rebuild their character to be single-classed be game-breaking?
If a specific mass of air is polluted, will the pollution stick with it?
What's the fastest way to get Hard To Borrow (HTB) stocks?
How can I deal with a player trying to insert real-world mythology into my homebrew setting?
Correlation of independent random processes
How to achieve this rough borders and stippled illustration look?
Why are Hobbits so fond of mushrooms?
Correct use of ergeben?
Bronze Age Underwater Civilization
During copyediting, journal disagrees about spelling of paper's main topic
Why did my rum cake turn black?
Was adding milk to tea started to reduce employee tea break time?
Is Prophet from Facebook any different from a linear regression?
Reverse the word order in string, without reversing the words
Can I use "candidate" as a verb?
Supporting developers who insist on using their pet language
Is Trump personally blocking people on Twitter?
<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?
Stuck Apple Mail - how to reset?
Why is dry soil hydrophobic? Bad gardener paradox
Is a Lisp program in both prog-mode and lisp-mode?
Can C# GC move memory objects
How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?Deep cloning objectsHow do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Why not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Let's suppose this very basic C# code:
var tab = new int[10];
I have read that non fixed variables can be moved in memory by garbage collector.
My question is: Is it possible that "tab" address change during my program execution ?
I just want to understand.
In fact, no matter if tab value change.
c#
add a comment |
Let's suppose this very basic C# code:
var tab = new int[10];
I have read that non fixed variables can be moved in memory by garbage collector.
My question is: Is it possible that "tab" address change during my program execution ?
I just want to understand.
In fact, no matter if tab value change.
c#
Follow up question: why do you care? Just interested?
– Blorgbeard
Jul 4 at 16:36
yes, just interested
– Bob5421
Jul 4 at 16:37
I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.
– David Pilkington
Jul 5 at 4:42
add a comment |
Let's suppose this very basic C# code:
var tab = new int[10];
I have read that non fixed variables can be moved in memory by garbage collector.
My question is: Is it possible that "tab" address change during my program execution ?
I just want to understand.
In fact, no matter if tab value change.
c#
Let's suppose this very basic C# code:
var tab = new int[10];
I have read that non fixed variables can be moved in memory by garbage collector.
My question is: Is it possible that "tab" address change during my program execution ?
I just want to understand.
In fact, no matter if tab value change.
c#
c#
edited Jul 4 at 18:37
casperOne
66.5k14 gold badges161 silver badges226 bronze badges
66.5k14 gold badges161 silver badges226 bronze badges
asked Jul 4 at 8:06
Bob5421Bob5421
1,5913 gold badges24 silver badges63 bronze badges
1,5913 gold badges24 silver badges63 bronze badges
Follow up question: why do you care? Just interested?
– Blorgbeard
Jul 4 at 16:36
yes, just interested
– Bob5421
Jul 4 at 16:37
I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.
– David Pilkington
Jul 5 at 4:42
add a comment |
Follow up question: why do you care? Just interested?
– Blorgbeard
Jul 4 at 16:36
yes, just interested
– Bob5421
Jul 4 at 16:37
I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.
– David Pilkington
Jul 5 at 4:42
Follow up question: why do you care? Just interested?
– Blorgbeard
Jul 4 at 16:36
Follow up question: why do you care? Just interested?
– Blorgbeard
Jul 4 at 16:36
yes, just interested
– Bob5421
Jul 4 at 16:37
yes, just interested
– Bob5421
Jul 4 at 16:37
I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.
– David Pilkington
Jul 5 at 4:42
I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.
– David Pilkington
Jul 5 at 4:42
add a comment |
4 Answers
4
active
oldest
votes
Yes it will.
But you can use the fixed
keyword to stop the GC from moving it if you so desire.
add a comment |
Yes. The memory address of tab
can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4
.
The point is, in c#
you don't need to bother about memory addresses as it's a managed language. All references to tab
variable will be changed accordingly, and your program will survive garbage collection seamlessly.
add a comment |
It is certain that the array object could be moved in memory.
But note that you cannot obtain the pointer of a managed reference object, or you use a fixed
block, it cannot be moved by GC within it.
If the array object is moved, the reference from variable tab
to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.
add a comment |
Considering you are using var
it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.
On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.
If you want to be sure to fix location of that variable you can use "fixed".
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56883363%2fcan-c-sharp-gc-move-memory-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes it will.
But you can use the fixed
keyword to stop the GC from moving it if you so desire.
add a comment |
Yes it will.
But you can use the fixed
keyword to stop the GC from moving it if you so desire.
add a comment |
Yes it will.
But you can use the fixed
keyword to stop the GC from moving it if you so desire.
Yes it will.
But you can use the fixed
keyword to stop the GC from moving it if you so desire.
answered Jul 4 at 8:13
David PilkingtonDavid Pilkington
11.5k2 gold badges27 silver badges56 bronze badges
11.5k2 gold badges27 silver badges56 bronze badges
add a comment |
add a comment |
Yes. The memory address of tab
can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4
.
The point is, in c#
you don't need to bother about memory addresses as it's a managed language. All references to tab
variable will be changed accordingly, and your program will survive garbage collection seamlessly.
add a comment |
Yes. The memory address of tab
can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4
.
The point is, in c#
you don't need to bother about memory addresses as it's a managed language. All references to tab
variable will be changed accordingly, and your program will survive garbage collection seamlessly.
add a comment |
Yes. The memory address of tab
can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4
.
The point is, in c#
you don't need to bother about memory addresses as it's a managed language. All references to tab
variable will be changed accordingly, and your program will survive garbage collection seamlessly.
Yes. The memory address of tab
can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4
.
The point is, in c#
you don't need to bother about memory addresses as it's a managed language. All references to tab
variable will be changed accordingly, and your program will survive garbage collection seamlessly.
edited Jul 4 at 8:24
answered Jul 4 at 8:10
RenatRenat
2,1441 gold badge11 silver badges22 bronze badges
2,1441 gold badge11 silver badges22 bronze badges
add a comment |
add a comment |
It is certain that the array object could be moved in memory.
But note that you cannot obtain the pointer of a managed reference object, or you use a fixed
block, it cannot be moved by GC within it.
If the array object is moved, the reference from variable tab
to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.
add a comment |
It is certain that the array object could be moved in memory.
But note that you cannot obtain the pointer of a managed reference object, or you use a fixed
block, it cannot be moved by GC within it.
If the array object is moved, the reference from variable tab
to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.
add a comment |
It is certain that the array object could be moved in memory.
But note that you cannot obtain the pointer of a managed reference object, or you use a fixed
block, it cannot be moved by GC within it.
If the array object is moved, the reference from variable tab
to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.
It is certain that the array object could be moved in memory.
But note that you cannot obtain the pointer of a managed reference object, or you use a fixed
block, it cannot be moved by GC within it.
If the array object is moved, the reference from variable tab
to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.
edited Jul 4 at 8:22
answered Jul 4 at 8:13
AlseinAlsein
8281 silver badge16 bronze badges
8281 silver badge16 bronze badges
add a comment |
add a comment |
Considering you are using var
it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.
On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.
If you want to be sure to fix location of that variable you can use "fixed".
add a comment |
Considering you are using var
it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.
On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.
If you want to be sure to fix location of that variable you can use "fixed".
add a comment |
Considering you are using var
it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.
On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.
If you want to be sure to fix location of that variable you can use "fixed".
Considering you are using var
it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.
On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.
If you want to be sure to fix location of that variable you can use "fixed".
answered Jul 4 at 8:14
edokanedokan
3,24316 silver badges32 bronze badges
3,24316 silver badges32 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56883363%2fcan-c-sharp-gc-move-memory-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Follow up question: why do you care? Just interested?
– Blorgbeard
Jul 4 at 16:36
yes, just interested
– Bob5421
Jul 4 at 16:37
I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.
– David Pilkington
Jul 5 at 4:42