문제

I am a bit confused about the fact that in C# only the reference types get garbage collected. That means GC picks only the reference types for memory de-allocation. So what happens with the value types as they also occupy memory on stack ?

도움이 되었습니까?

해결책

For a start, whether they're on the stack or part of the heap depends on what context they're part of - if they're within a reference type, they'll be on the heap anyway. (You should consider how much you really care about the stack/heap divide anyway - as Eric Lippert has written, it's largely an implementation detail.)

However, basically value type memory is reclaimed when the context is reclaimed - so when the stack is popped by you returning from a method, that "reclaims" the whole stack frame. Likewise if the value type value is actually part of an object, then the memory is reclaimed when that object is garbage collected.

The short answer is that you don't have to worry about it :) (This assumes you don't have anything other than the memory to worry about, of course - if you've got structs with references to native handles that need releasing, that's a somewhat different scenario.)

다른 팁

I am a bit confused about the fact that in C# only the reference types get garbage collected.

This is not a fact. Or, rather, the truth or falsity of this statement depends on what you mean by "get garbage collected". The garbage collector certainly looks at value types when collecting; those value types might be alive and holding on to a reference type:

struct S { public string str; }
...
S s = default(S); // local variable of value type
s.str = M(); 

when the garbage collector runs it certainly looks at s, because it needs to determine that s.str is still alive.

My suggestion: clarify precisely what you mean by the verb "gets garbage collected".

GC picks only the reference types for memory de-allocation.

Again, this is not a fact. Suppose you have an instance of

class C { int x; }

the memory for the integer will be on the garbage-collected heap, and therefore reclaimed by the garbage collector when the instance of C becomes unrooted.

Why do you believe the falsehood that only the memory of reference types is deallocated by the garbage collector? The correct statement is that memory that was allocated by the garbage collector is deallocated by the garbage collector, which I think makes perfect sense. The GC allocated it so it is responsible for cleaning it up.

So what happens with the value types as they also occupy memory on stack ?

Nothing at all happens to them. Nothing needs to happen to them. The stack is a million bytes. The size of the stack is determined when the thread starts up; it starts at a million bytes and it stays a million bytes throughout the entire execution of the thread. Memory on the stack is neither created nor destroyed; only its contents are changed.

There are too many verbs used in this question, like destroyed, reclaimed, deallocated, removed. That doesn't correspond well with what actually happens. A local variable simply ceases to be, Norwegian parrot style.

A method has a single point of entry, first thing that happens is that the CPU stack pointer is adjusted. Creating a "stack frame", storage space for the local variables. The CLR guarantees that this space is initialized to 0, not otherwise a feature you use strongly in C# because of the definite assignment rule.

A method has a single point of exit, even if you method code is peppered with multiple return statements. At that point, the stack pointer is simply restored to its original value. In effect it "forgets" that the local variables where ever there. Their values are not 'scrubbed' in any way, the bytes are still there. But they won't last long, the next call in your program are going to overwrite them again. The CLR zero-initialization rule ensures that you can never observe those old values, that would be insecure.

Very, very fast, takes no more than a single processor cycle. A visible side-effect of this behavior in the C# language is that value types cannot have a finalizer. Ensuring no extra work has to be done.

A value type on the stack is removed from the stack when it goes out of scope.

첫째, 이것은 매우 실현 가능하지만 올바른 디자인을 통해 사고 할 때 상당한 노력이없고 그것을 구현할 수 없을 것입니다. 여기에 옵션이 있습니다.

기본적으로 봐야 할 것은 증강 주장입니다. SharePoint가 인증 된 사용자를 알고있는 주장 세트를 보완하는 방법이 필요하다고 말하는 것입니다. SharePoint를 사용하면 항목을 클레임에 대해 보안 할 수 있습니다.

여러 가지 방법으로 클레임을 보강 할 수 있지만 가장 일반적인 두 가지는 다음과 같습니다.

ADFS - ADFS가 응용 프로그램의 사용자 정의 ID 공급자로 사용되면 ADFS 클레임 규칙 및 속성 저장소를 사용하여 SQL 데이터베이스를 쿼리하고 새 클레임을 할당 할 수 있습니다. 사용자에게. 그런 다음이 새로운 클레임을 사용하여 SharePoint를 보호해야합니다. 이 경로를 따르는 경우 Steve Peschka가 주제에 게시 한 모든 것을 기본적으로 읽으십시오.

http://blogs.technet.com/b/speschka/archive/2010/07/30/charcive-sharepoint-2010-and-adfs-v2-end-to-ind.aspx

ADFS에서 SQL 속성 저장소 사용에 대한 자세한 내용을 찾으려면 다음을 살펴보십시오.

http://blogs.technet.com/b/vinitt/archive/2013/04/15/how-to-use-to-custom-sql- 속성 - 저장 - 생성 - 청구 및 - authorize-user.aspx

사용자 지정 클레임 공급자 - SharePoint 용 사용자 지정 클레임 공급자를 작성하고 웹 응용 프로그램에 등록 할 수 있으므로 모든 코드를 작성하여 반환 된 값을 처리해야하지만 나는 그것이 문제가 아니라고 가정합니다. 이 옵션은 실제로 SharePoint를 ADF로 보안 할 때 사용자 경험을 더 잘 제어 할 수 있으므로 모든 유효성 검사 또는 검색 측면을 클레임 피커 (Pister Picker)로 통합 할 수 없습니다. 사실 사람들 피커가 유효한 속성 값으로 존재하는지 여부가 유효한 것으로 나타나는 것보다 더 나빠질 것으로 보입니다. 이는 SharePoint에서 항목을 보안 할 때, 하이브리드를 고려해야 할 수있는 항목을 보안 할 때 매우 열려 있습니다. 클레임 증강에 대한 ADFS를 사용하는 방식이지만 사용자 정의 클레임 공급자가 있으며 SQL 데이터베이스에서 이름을 검색하고 해결할 수 있습니다.

자세한 정보와 사용자 정의 클레임 제공 업체의 출발점은 여기에서 찾을 수 있습니다.

"Nofollow"> http://msdn.microsoft. COM / ko-US / Library / GG615945 % 28v= 사무실 .14 % 29.aspx

Steve Peschka는이 주제에 많은 블로그 게시물을 작성했지만 디자인을 구축 할 때 모든 것을 통해 봐라는 것이 좋습니다.

이것은 올바른 방향으로 시작하기 시작하고 의심의 여지가 없으며 필요한 모든 답변을 얻기 전에 더 많은 질문을 할 것입니다.

도움이되기를 바랍니다.

value types would get deallocated when the stack frame is removed after it has been executed i would assume

Would also like to add that stack is at a thread level, and heap is at application domain level.

So, when a thread ends it would reclam the stack memory used by that specific thread.

Every value type instance in .NET will be part of something else, which could be a larger enclosing value type instance, a heap object, or a stack frame. Whenever any of those things come into being, any structures within them will also come into being; those structures will then continue to exist as long as the thing containing them does. When the thing that contains the structure ceases to exist, the structure will as well. There is no way to destroy a structure without destroying the container, and there is no way to destroy something that contains one or more structures without destroying the structures contained therein.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top