Issue #4838: When a module is deallocated, free the memory backing the optional module state data.
diff --git a/Misc/NEWS b/Misc/NEWS
index 69c42d2..fee788f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #4838: When a module is deallocated, free the memory backing the
+  optional module state data.
+
 - Issue #4910: Rename nb_long slot to nb_reserved, and change its
   type to (void *).
 
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 9c705b8..0c6032f 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -315,6 +315,8 @@
 		_PyModule_Clear((PyObject *)m);
 		Py_DECREF(m->md_dict);
 	}
+	if (m->md_state != NULL)
+		PyMem_FREE(m->md_state);
 	Py_TYPE(m)->tp_free((PyObject *)m);
 }