1. A memory controller for a nonvolatile memory, the controller comprising: a counter configured to count a number of valid pages in a block comprising a page to be disabled upon writing, when data is written in the nonvolatile memory; and an adding module configured to set the block as an object of compaction when the number of valid pages counted by the counter is smaller than a threshold number.