c# - Garbage Collector won't free memory of some objects -
i have these methods:
public void getimage(jobject o) { var imagefile = o["file"].tostring(); if (!file.exists(imagefile)) { sendmessage("file not exist"); return; } using (var image = image.fromfile(imagefile)) { var serialized = getimageasstring(image); var ob = new jobject { { command, (int) command.getimage }, { "content", serialized } }; send(ob); ob = null; serialized = null; } } private static string getimageasstring(image image) { using (var stream = new memorystream()) { image.save(stream, system.drawing.imaging.imageformat.jpeg); return convert.tobase64string(stream.toarray()); } } private static void send(jobject o) { var package = encodecontent(o); _stream.write(package, 0, package.length); _stream.flush(); cursor.current = cursors.waitcursor; } private static byte[] encodecontent(jobject o) { return encoding.utf8.getbytes(string.concat(getmessagesize(o), o, (char) 3)); }
after execution of getimage
612kb jpeg
file, memory consumption of application goes 10mb up, , after several minutes 10mb not disposed.
- how can objects generated
getimage
eligiblegc
? - how can make sure objects generated
getimage
not allocated long after method execution?
the gc determines when free memory - when feels pressure free memory, so. if has no such pressure, wont.
since jpeg large object, gc not put in gen1 collector, gets cleaned more other, longer lived generations. therefore it's less have pressure collect.
to check if not collecting because doesn't feel it, or if it's leak, try doing following set of calls, , see happens memory. if doesn't drop, may have leak (though guess it's gc not collecting). (disclaimer - not recommend leave code in production code. let gc thing).
gc.collect(); gc.waitforpendingfinalizers(); gc.collect();
Comments
Post a Comment