This is a procedure for repelling the "editor memory leak" that gradually enlarges the Unity editor itself behind the scenes and causes it to freeze when a dynamic GUI preview or custom preview is embedded in the editor.

Specific solution

Complete template code for cleaning up the editor window when it is destroyed.

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

public class PreviewEditorWindow : EditorWindow
{
    private Texture2D previewTexture;
    private Material previewMaterial;

    void OnGUI()
    {
        if (previewTexture == null)
        {
            // Dynamically generate texture resources for preview etc.
            previewTexture = new Texture2D(128, 128);
        }
        // ...window drawing...
    }

    // ➔ Release process when window is deactivated or closed
    void OnDisable()
    {
        CleanupResources();
    }

    void OnDestroy()
    {
        CleanupResources();
    }

    private void CleanupResources()
    {
        // Editor resources should be destroyed in-place using DestroyImmediate
        if (previewTexture != null)
        {
            DestroyImmediate(previewTexture);
            previewTexture = null;
        }
        if (previewMaterial != null)
        {
            DestroyImmediate(previewMaterial);
            previewMaterial = null;
        }
        Debug.Log("エディタウィンドウ用の一時メモリ資源を完全に解放しました。");
    }
}
#endif