첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ Container.cs

namespace TestProject
{
    /// <summary>
    /// 컨테이너
    /// </summary>
    public class Container
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 데이터
        /// </summary>
        public string Data;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Container(data)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="data">데이터</param>
        public Container(string data)
        {
            Data = data;
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 전화 컨테이너
        /// </summary>
        private Container phoneContainer;

        /// <summary>
        /// 노트북 컨테이너
        /// </summary>
        private Container notebookContainer;

        /// <summary>
        /// 컨테이너 1
        /// </summary>
        private Container container1;

        /// <summary>
        /// 컨테이너 2
        /// </summary>
        private WeakReference container2;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.phoneContainer    = new Container("Phone"   );
            this.notebookContainer = new Container("NoteBook");
             
            this.container1 = this.phoneContainer;
            this.container2 = new WeakReference(this.notebookContainer);
 
            this.containerValueLabel1.Text = this.container1        == null ? "null" :  this.container1.Data;
            this.containerValueLabel2.Text = this.container2.Target == null ? "null" : (this.container2.Target as Container).Data;

            this.collectButton.Click += collectButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region GC 수집 버튼 클릭시 처리하기 - collectButton_Click(sender, e)

        /// <summary>
        /// GC 수집 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void collectButton_Click(object sender, EventArgs e)
        {
            this.phoneContainer    = null;
            this.notebookContainer = null;
 
            GC.Collect(0, GCCollectionMode.Forced);

            GC.WaitForFullGCComplete();
 
            this.containerValueLabel1.Text = this.container1        == null ? "null" :  this.container1.Data;
            this.containerValueLabel2.Text = this.container2.Target == null ? "null" : (this.container2.Target as Container).Data;
        }

        #endregion
    }
}

 

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요