java - Possible memory leak with StringBuilder? -
i using stringbuilder create file object, using see if directory file located inside of exists:
stringbuilder sbfile = new stringbuilder(); sbfile.append("/home/logs/"); file ofile = new file(sbfile.tostring()); if(!ofile.exists()) ofile.mkdir(); sbfile.append("mylogfile.log"); ofile = new file(sbfile.tostring()); but worried reusing same ofile reference on 2 different "versions" of string builder (/home/logs/ vs /home/logs/mylogfile.log) create memory leak. if so, how should write differently?
there no memory leak. instance of file created first time, garbage-collected jvm when no longer used.
the other thing don't need use stringbuilder. file class has constructor takes parent , filename. example this:
file parent = new file("/home/logs/"); if(!parent.exists()) parent.mkdir(); file file = new file(parent, "mylogfile.log"); // todo: file... also, may interested in how garbage collection works in java.
Comments
Post a Comment