Generally, you should call Dispose
and drop all references to the PictureBox. Some time later the GC will collect this PictureBox and free the memory. But it is no way to know, when exactly GC will run. So, the memory usage may increase for some time before GC will run - it is normal. In most cases you should not manually force GC to run, but in some specific cases you can do this with GC.Collect();
(but it strongly not recommended)
Update
You can't manually "collect/free" only specified object. Only force GC (and get all the possible problems, mentioned by the link above).
The better solution may be to rework your code to avoid recreation of ImageBoxes. You can try
- show/hide the single ImageBox and just replacing the image inside it
- cache the ImageBox object and add/remove it from the form with
Controls.Add/Controls.Remove
.
You will still get some memory traffic due to loading/disposing images, but it may be better then recreation of a whole ImageBox
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…