manage memory of Mat