In the project often need to use the MD5 of the calculation file, for some purposes, MD5 calculation algorithm, usually in the network query, the general algorithm is to read the entire file stream of bytes, and then calculate the file md5, this way when the file is large, and there is a lot of concurrency, it can lead to memory explosion. So the following code provides several ways. And by calculating the MD5 and size of a 323M file, some information about the GC is given.
Code
/** Copyright (C) 2016. All rights Reserved. */ Packageme.nabil.mixed;Importorg.apache.commons.codec.digest.DigestUtils;Importorg.apache.commons.io.FileUtils;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException;/*** MD5 Encryption class*/ Public classMd5util {Private Static FinalLogger Logger = Loggerfactory.getlogger (md5util.class); protected Static Final Char[] hexdigits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '}; /*** MD5 encryption of BINARY data * *@paramData file binaries *@returnMD5 plus password*/ Public StaticString Getmd5bybyte (byte[] data) { Try { Char[] str; MessageDigest mdtemp= Messagedigest.getinstance ("MD5"); Mdtemp.update (data); byte[] MD =mdtemp.digest (); intj =md.length; STR=New Char[J * 2]; intK = 0; for(inti = 0; I < J; i++) { byteBYTE0 =Md[i]; Str[k+ +] = hexdigits[byte0 >>> 4 & 0xf]; Str[k+ +] = hexdigits[byte0 & 0xf]; } return NewString (str); } Catch(Exception e) {logger.error ("Error occurred when making MD5 for data file", E); return NULL; } } /*** File Object * *@paramfile *@return */ Public StaticString getmd5byfile (file file) {FileInputStream fis=NULL; Try{messagedigest MD= Messagedigest.getinstance ("MD5"); FIS=Newfileinputstream (file); byte[] buffer =New byte[8192]; intLength =-1; System.out.println ("Start counting."); while(length = fis.read (buffer))! =-1) {md.update (buffer,0, length); } System.out.println ("It's done."); returnbytestostring (Md.digest ()); } Catch(IOException ex) {Logger.info (Ex.getmessage (), ex); return NULL; } Catch(nosuchalgorithmexception e) {logger.info (E.getmessage (), E); return NULL; } finally { Try{fis.close (); } Catch(IOException ex) {Logger.info (Ex.getmessage (), ex); } } } /*** bytestostring * *@paramData *@return */ Public StaticString bytestostring (byte[] data) { CharHexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' E ', ' F '}; Char[] temp =New Char[Data.length * 2]; for(inti = 0; i < data.length; i++) { byteb =Data[i]; Temp[i* 2] = hexdigits[b >>> 4 & 0x0f]; Temp[i* 2 + 1] = hexdigits[b & 0x0f]; } return NewString (temp); } /*** Main * *@paramargs *@throwsIOException*/ Public Static voidMain (string[] args)throwsIOException {String path= "/path/to/file";//a 323M file path//1. Stream ProcessingSystem.out.println (Getmd5byfile (NewFile (path) + ":" + fileutils.sizeof (NewFile (path)); //2. Common-codecSystem.out.println (Digestutils.md5hex (NewFileInputStream (path) + ":" + fileutils.sizeof (NewFile (path)); //3. Read all in memory byte[] data = Fileutils.readfiletobytearray (NewFile (path)); System.out.println (Getmd5bybyte (data)+ ":" +data.length); }}
Analysis
The above three ways to give some information about the JVM by commenting on the other two, running only one of the ways
Add parameter JVM parameters at runtime:
-xx:+printgc-xx:+printgcdetails-xx:+printgctimestamps-xx:+printgcapplicationstoppedtime
1.8m-buffer Stream Processing
0.151:total time forWhich application threads were stopped:0.0000535 seconds, stopping threads took:0.0000072seconds0.151:total time forWhich application threads were stopped:0.0000428 seconds, stopping threads took:0.0000083secondsconnected to the target VM, address:' 127.0.0.1:63630 ', Transport: ' Socket ' 0.314:total time forWhich application threads were stopped:0.0001357 seconds, stopping threads took:0.0000145seconds0.417:total time forWhich application threads were stopped:0.0001063 seconds, stopping threads took:0.0000169seconds start counting .1.442:total time forWhich application threads were stopped:0.0221453 seconds, stopping threads took:0.0219728seconds finished disconnected from the target VM, address:' 127.0.0.1:63630 ', Transport: ' Socket 'AA3858DFBC48DAAB4B891E112D5396FC:323251542Heap Psyounggen Total 38400K, used 6658K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000) Eden Space 33280K,20% used [0x0000000795580000,0x0000000795c00b00,0x0000000797600000) from Space 5120K,0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000) to space 5120K,0% used [0x0000000797600000,0x0000000797600000,0x0000000797b00000) Paroldgen total 87552K, used 0K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000) object Space 87552K,0% used [0x0000000740000000,0x0000000740000000,0x0000000745580000) Metaspace used 3560K, capacity 4728K, committed 4864K, reserved 1056768Kclassspace used 386K, capacity 424K, committed 512K, reserved 1048576KProcess finished with exit code0
2. Results of Apache Common-codec
0.184:total time forWhich application threads were stopped:0.0000615 seconds, stopping threads took:0.0000078seconds0.185:total time forWhich application threads were stopped:0.0000279 seconds, stopping threads took:0.0000056seconds0.640:total time forWhich application threads were stopped:0.0001445 seconds, stopping threads took:0.0000329seconds0.891:total time forWhich application threads were stopped:0.0001546 seconds, stopping threads took:0.0000383seconds1.904:total time forWhich application threads were stopped:0.0102225 seconds, stopping threads took:0.0101437seconds4.580:total time forWhich application threads were stopped:0.0001097 seconds, stopping threads took:0.0000165SECONDSAA3858DFBC48DAAB4B891E112D5396FC:323251542Heap Psyounggen Total 38400K, used 9508K [0x0000000795580000, 0x0000000798000000, 0x00000007c0000000) Eden Space 33280K,28% used [0x0000000795580000,0x0000000795ec92b8,0x0000000797600000) from Space 5120K,0% used [0x0000000797b00000,0x0000000797b00000,0x0000000798000000) to space 5120K,0% used [0x0000000797600000,0x0000000797600000,0x0000000797b00000) Paroldgen total 87552K, used 0K [0x0000000740000000, 0x0000000745580000, 0x0000000795580000) object Space 87552K,0% used [0x0000000740000000,0x0000000740000000,0x0000000745580000) Metaspace used 3616K, capacity 4792K, committed 5120K, reserved 1056768Kclassspace used 391K, capacity 424K, committed 512K, reserved 1048576KDisconnected from the target VM, address:' 127.0.0.1:63674 ', Transport: ' Socket '
3. Read all in memory and recalculate
0.161:total time forWhich application threads were stopped:0.0000584 seconds, stopping threads took:0.0000190seconds0.161:total time forWhich application threads were stopped:0.0000407 seconds, stopping threads took:0.0000054seconds0.356:total time forWhich application threads were stopped:0.0001168 seconds, stopping threads took:0.0000177seconds0.860: [GC (Allocation Failure) [psyounggen:21892k->4950k (38400K)] 21892k->25438k (125952K), 0.0187028 secs] [ times:user=0.01 sys=0.01, real=0.02secs]0.879:total time forWhich application threads were stopped:0.0189020 seconds, stopping threads took:0.0000150seconds4.411:total time forWhich application threads were stopped:0.0445206 seconds, stopping threads took:0.0435764seconds4.694:total time forWhich application threads were stopped:0.0003998 seconds, stopping threads took:0.0000154seconds5.695:total time forWhich application threads were stopped:0.0001298 seconds, stopping threads took:0.0000841SECONDSAA3858DFBC48DAAB4B891E112D5396FC:323251542Heap Psyounggen Total 38400K, used 22181K [0x0000000795580000, 0x000000079a080000, 0x00000007c0000000) Eden Space 33280K,51% used [0x0000000795580000,0x0000000796653bd0,0x0000000797600000) from Space 5120K,96% used [0x0000000797600000,0x0000000797ad58d0,0x0000000797b00000) to space 5120K,0% used [0x0000000799b80000,0x0000000799b80000,0x000000079a080000) Paroldgen total 863744K, used 827683K [0x0000000740000000, 0x0000000774b80000, 0x0000000795580000) object Space 863744K,95% used [0x0000000740000000,0x0000000772848dd8,0x0000000774b80000) Metaspace used 3663K, capacity 4792K, committed 5120K, reserved 1056768Kclassspace used 393K, capacity 424K, committed 512K, reserved 1048576KDisconnected from the target VM, address:' 127.0.0.1:63717 ', Transport: ' Socket 'Process finished with exit code0
Conclusion
It can be seen that the first two scenarios are desirable, and the third way is that memory consumption is too high.
Comparison of different MD5 calculation methods in Java