2016-10-24 1 views
-2

저는 주로 Python과 C#으로 작업합니다.
가비지 수집기를 수동으로 사용하지 않아야합니다. 자동으로 작업을 처리하므로 혼란하지 않는 것이 가장 좋습니다.왜 가비지 수집이 작동하는지 이해해야합니까?

사실인가요?
그렇다면 GB가 어떻게 작동하고 사용하는 방법을 이해해야하는 이유는 무엇입니까?

+2

[파이썬 메모리 누수 (http://stackoverflow.com/questions/1435415/python-memory-leaks) –

+1

수동으로 사용하는 방법을 알고 그것은 두 가지 다른 것들입니다. 내가했던 모든 독서는 그것을 건드리지 않는다고 말하지만 (수동으로 사용하지 마십시오), 그것이 작동하는 방법을 알아야만 자동 튜닝 자동 가비지 콜렉션 매직이 모든 잠재력을 발휘합니다. – Quantic

+0

누가 GC가 작동하는지 이해해야한다고 말했습니까? 그것이 당신의 공부 분야가 아니라면 그것이 어떻게 작동하는지 알 필요는 없지만 성능에 나쁜 영향을 미칠 수있는 것이 무엇인지를 알아야합니다. –

답변

1

사실 당신이 말한 것은 사실입니다. .NET에서 COM interop을 사용하여 작업하는 동안 COM 리소스를 수동으로 릴리스하고 가비지 수집을 일찍 수행해야하는 경우가있었습니다. COM 개체 내에서 순수 가상 함수 호출로 인한 응용 프로그램 충돌을 방지합니다. 이것은 가비지 수집에 대해 알아야 할 부분을 생각할 수있는 한 가지 예입니다.

0

버그를 만들지 않으려면 참조 카운팅 및 가비지 수집에 대해 충분히 알아야합니다. 규칙은 구현 및 다른 구현 (예 : pypy, ironpython)으로 다양하며 jpython은 매우 다릅니다. cpython에서 객체의 참조 카운트가 0이되면 객체의 __del__이 호출되므로 닫히는 파일과 같은 것에 의존 할 수 있습니다. 기본 기술로 객체를 정리해야한다고 생각할 때 __del__이 호출되는 다른 구현에서는 그렇지 않습니다. 예를 들어 How does garbage collection and scoping work in C#?을 참조하십시오.

def foo(fileobj): 
    """Append foo to file. I double pinky-promise that I don't keep a 
    reference to the file.""" 
    fileobj.write('foo\n') 

# in cpython, reference counting closes the file right away so we know 
# that "foo\n" has been flushed to the file when call returns. 
foo(open('myfile', 'a')) 

# In other python implemenations, file may remain open and unflushed 
# for a long time. We can use a context manager to get that thing closed. 
with open('myfile', 'a') as myfile: 
    foo(myfile) 

I (심장) cpython 참고 카운트지만 마지 못해 문맥 관리자에게 몇 년 동안 옮겼습니다.

cpython에서 정기적 인 참조 횟수가 모호한 경우 가비지 수집이 시작됩니다. 다음은 원형 참조가있는 클래스 집합으로 weakref으로 수정합니다. BTW, 다른 구현에 다르게 작동합니다. cpython 가비지 수집기를 주기적으로 호출합니다. gc.collect을 직접 호출 할 필요는 없지만 문제를 피하는 방법을 알아야합니다. 실행하면

import gc 
import weakref 

class Child: 

    def __init__(self, parent, index): 
     self.parent = parent # <creates circular ref 
     self.index = index 

class Parent: 

    def __init__(self, nodecount): 
     self.nodes = list(Child(self, i) for i in range(nodecount)) 

class ChildWeakref: 

    def __init__(self, parent, index): 
     self.parent = weakref.ref(parent) 
     self.index = index 

class ParentWeakref: 

    def __init__(self, nodecount): 
     self.nodes = list(ChildWeakref(self, i) for i in range(nodecount)) 

gc.collect() # clean up any pending 

print("With circular references") 
print('before allocation', len(gc.get_objects())) 
tree = Parent(100) 
del tree 
print('before collection', len(gc.get_objects())) 
gc.collect() 
print('after collection', len(gc.get_objects())) 

print() 

print("Without circular references") 
print('before allocation', len(gc.get_objects())) 
tree = ParentWeakref(100) 
del tree 
print('before collection', len(gc.get_objects())) 
gc.collect() 
print('after collection', len(gc.get_objects())) 

, 내가 얻을 :

With circular references 
before allocation 5486 
before collection 5689 
after collection 5486 

Without circular references 
before allocation 5486 
before collection 5486 
after collection 5486 
관련 문제