I encountered a piece of code on the weekend, which was about the Java IO stream. The Code appeared in a multi-threaded system, and a piece of code was not closed in time after opening a file and operating the stream, I started to think that too many files will be opened soon or cause memory overflow, but there was still no problem after running for a long time. Then I wrote a small program to test it, finally, it seems to be clear why.
First read the following code
Import java. io. fileInputStream; <br/> public class TTT {<br/> public static void main (String [] args) throws Exception {<br/> for (int I = 0; I <10; I ++) {<br/> final String threadId = "thread _" + I; <br/> Thread thread = new Thread (new Runnable () {<br/> public void run () {<br/> System. out. println (threadId + "started! "); <Br/> try {<br/> FileInputStream FD = new FileInputStream ("/opt/test. log "); <br/> Thread. sleep (60*1000); <br/>} catch (Exception ex) {<br/> ex. printStackTrace (); <br/>}< br/> System. out. println (threadId + "stopped! "); <Br/>}< br/>}); <br/> thread. start (); <br/>}< br/> Thread. sleep (10x60x1000); <br/>}< br/>}
Compile and run this class on linux, and then use the linux Command/usr/sbin/lsof-p <pid> to view the File Information opened by this program.
$/Usr/sbin/lsof-p 'ps-ef | grep java | grep TTT | awk '{print $2} ''| grep" test. log "<br/> java 21562 fkong 3r REG 35471424, 0 0/opt/test. log <br/> java 21562 fkong 4r REG 35471424, 0 0/opt/test. log <br/> java 21562 fkong 5r REG 253,0 0 35471424/opt/test. log <br/> java 21562 fkong 6r REG 253,0 0 35471424/opt/test. log <br/> java 21562 fkong 7r REG 35471424, 0 0/opt/test. log <br/> java 21562 fkong 8r REG 35471424, 0 0/opt/test. log <br/> java 21562 fkong 9r REG 253,0 0 35471424/opt/test. log <br/> java 21562 fkong 10r REG 253,0 0 35471424/opt/test. log <br/> java 21562 fkong 11r REG 253,0 0 35471424/opt/test. log <br/> java 21562 fkong 12r REG 253,0 0 35471424/opt/test. log
You can see that 10 file streams are not closed, regardless of whether they are running in 10 threads or when the results are the same by running the lsof command.
Below I made some changes to this code, that is, after the thread is executed, all threads are set to null, as shown below:
Import java. io. fileInputStream; <br/> import java. util. arrayList; <br/> import java. util. list; <br/> public class TTT {<br/> public static void main (String [] args) throws Exception {<br/> List <Thread> threads = new ArrayList <Thread> (); <br/> for (int I = 0; I <10; I ++) {<br/> final String threadId = "thread _" + I; <br/> Thread thread = new Thread (new Runnable () {<br/> public void run () {<br/> System. ou T. println (threadId + "started! "); <Br/> try {<br/> FileInputStream FD = new FileInputStream ("/opt/test. log "); <br/> Thread. sleep (60*1000); <br/>} catch (Exception ex) {<br/> ex. printStackTrace (); <br/>}< br/> System. out. println (threadId + "stopped! "); <Br/>}< br/>}); <br/> thread. start (); <br/> threads. add (thread); <br/>}< br/> Thread. sleep (2x60*1000); <br/> for (Thread thread: threads) {<br/> thread = null; <br/>}< br/> System. out. println ("Clean up threads! "); <Br/> Thread. sleep (10*60*1000); <br/>}< br/>}
Run lsof again in the process of running 10 threads and after running. The result is still similar, but 10 file streams are not closed.
I made some changes again. After setting all threads to null, I added (or urged JVM) Several gc operations as follows:
Import java. io. fileInputStream; <br/> import java. util. arrayList; <br/> import java. util. list; <br/> public class TTT {<br/> public static void main (String [] args) throws Exception {<br/> List <Thread> threads = new ArrayList <Thread> (); <br/> for (int I = 0; I <10; I ++) {<br/> final String threadId = "thread _" + I; <br/> Thread thread = new Thread (new Runnable () {<br/> public void run () {<br/> System. ou T. println (threadId + "started! "); <Br/> try {<br/> FileInputStream FD = new FileInputStream ("/opt/test. log "); <br/> Thread. sleep (60*1000); <br/>} catch (Exception ex) {<br/> ex. printStackTrace (); <br/>}< br/> System. out. println (threadId + "stopped! "); <Br/>}< br/>}); <br/> thread. start (); <br/> threads. add (thread); <br/>}< br/> Thread. sleep (2x60*1000); <br/> for (Thread thread: threads) {<br/> thread = null; <br/>}< br/> System. out. println ("Clean up threads! "); </P> <p> System. gc (); <br/> System. gc (); <br/> System. gc (); <br/> System. out. println ("Finished GC! "); </P> <p> Thread. sleep (10x60*1000); <br/>}< br/>}
Use lsof again to check that 10 file streams are still open while running, but in "Finished GC !" The result is that all the 10 opened file streams are closed.
Finally, I simply delete the statements that set thread to null, and the running results are consistent with the results of the preceding gc operation.
In the end, IO file streams that have not been closed are not used in the JVM. When Full GC is executed next time, all of them will be recycled, however, it is still difficult for the JVM to do these tasks.