Garbage Collector and Local Data

O

O.B.

I'm a little unfamiliar with how the garbage collector works in C#.
First my example:

[StructLayout(LayoutKind.Explicit)]
public unsafe struct TestClass {
[FieldOffset(0)]
fixed byte buffer[1400];
[FieldOffset(0)]
TestA testA;
[FieldOffset(0)]
TestB testB;
}

public class MyTest {
static public TestClass convert(byte[] data) {
TestClass temp;
Marshal.Copy(data, 0, (IntPtr)(&test), sizeof(TestClass));
return temp;
}
}

It is my understanding that "temp" was created on the stack within
"convert" and may be susceptible to getting snatched by the garbage
collector, correct? Or am I just being paranoid?
 
J

Jon Skeet [C# MVP]

O.B. said:
I'm a little unfamiliar with how the garbage collector works in C#.
First my example:

[StructLayout(LayoutKind.Explicit)]
public unsafe struct TestClass {
[FieldOffset(0)]
fixed byte buffer[1400];
[FieldOffset(0)]
TestA testA;
[FieldOffset(0)]
TestB testB;
}

public class MyTest {
static public TestClass convert(byte[] data) {
TestClass temp;
Marshal.Copy(data, 0, (IntPtr)(&test), sizeof(TestClass));
return temp;
}
}

It is my understanding that "temp" was created on the stack within
"convert" and may be susceptible to getting snatched by the garbage
collector, correct? Or am I just being paranoid?

You're being paranoid: for a start, the garbage collector is only
concerned with the managed heap.

temp is created on the stack, and returned as a value by convert.
 
O

O.B.

Jon said:
O.B. said:
I'm a little unfamiliar with how the garbage collector works in C#.
First my example:

[StructLayout(LayoutKind.Explicit)]
public unsafe struct TestClass {
[FieldOffset(0)]
fixed byte buffer[1400];
[FieldOffset(0)]
TestA testA;
[FieldOffset(0)]
TestB testB;
}

public class MyTest {
static public TestClass convert(byte[] data) {
TestClass temp;
Marshal.Copy(data, 0, (IntPtr)(&test), sizeof(TestClass));
return temp;
}
}

It is my understanding that "temp" was created on the stack within
"convert" and may be susceptible to getting snatched by the garbage
collector, correct? Or am I just being paranoid?

You're being paranoid: for a start, the garbage collector is only
concerned with the managed heap.

temp is created on the stack, and returned as a value by convert.

Sure enough; I think I need to get up and take a break. I should've
known the answer to that one. Thank you.
 
W

Willy Denoyette [MVP]

| Jon Skeet [C# MVP] wrote:
| >> I'm a little unfamiliar with how the garbage collector works in C#.
| >> First my example:
| >>
| >> [StructLayout(LayoutKind.Explicit)]
| >> public unsafe struct TestClass {
| >> [FieldOffset(0)]
| >> fixed byte buffer[1400];
| >> [FieldOffset(0)]
| >> TestA testA;
| >> [FieldOffset(0)]
| >> TestB testB;
| >> }
| >>
| >> public class MyTest {
| >> static public TestClass convert(byte[] data) {
| >> TestClass temp;
| >> Marshal.Copy(data, 0, (IntPtr)(&test), sizeof(TestClass));
| >> return temp;
| >> }
| >> }
| >>
| >> It is my understanding that "temp" was created on the stack within
| >> "convert" and may be susceptible to getting snatched by the garbage
| >> collector, correct? Or am I just being paranoid?
| >
| > You're being paranoid: for a start, the garbage collector is only
| > concerned with the managed heap.
| >
| > temp is created on the stack, and returned as a value by convert.
| >
|
| Sure enough; I think I need to get up and take a break. I should've
| known the answer to that one. Thank you.

Note that it's quite expensive to do this. In "convert" you are actualy
copying a byte[] on the GC heap (data) to a byte[] on the stack
(temp.buffer), on return this byte[] is copied back from the callee's stack
frame to the callers stack frame. This means that you are finaly copying two
times the same data. What are you trying to achieve?

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top