java.lang.OutOfMemoryError: unable to create new native thread Exception When Using SmbFileInputStream

I am writing an application that copies files from a Samba share using the JCIFC Java CIFS Client Library (which, btw, is a very helpful, widely used, and well developed project).

As usual I am under the gun to get something up and running so I do not have the luxury to explore all of the details as much as I would like.? My application gets a directory listing of a Samba share and then spawns a new thread for each new file (with a limit, of course) which copies the file to the local file system via a SmbFileInputStream and FileOutputStream as follows (without the obligatory try/catch blocks for readability):

(An SmbFile instance is provided by the calling code)

byte[] readBuffer = new byte[1024];
int bytesRead = 0;

File fileDestination = new File(“/tmp/”, “destinationFile”);
FileOutputStream fileDestinationFOS = new FileOuputStream(fileDestination);

SmbFileInputStream smbFIS = new SmbFileInputStream(smbFile);

while ((bytesRead = != -1 ) {
fileDestination.write(readBuffer, 0, bytesRead);


When I run it I get the following Exception:

Exception in thread “Thread-3” java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(
at jcifs.UniAddress.lookupServerOrWorkgroup(
at jcifs.UniAddress.getAllByName(
at jcifs.UniAddress.getByName(
at jcifs.smb.Dfs.getTrustedDomains(
at jcifs.smb.Dfs.resolve(
at jcifs.smb.SmbFile.resolveDfs(
at jcifs.smb.SmbFile.send(
at jcifs.smb.SmbFileInputStream.readDirect(
at com.ngc.amap.mapDaemon.FileScanDaemon$

After considerable searching and testing I determined that the call to jcifs.smb.SmbFile.resolveDfs( was spawning numerous Threads that were consuming all of the RAM availble to naitive thread in my JVM.? Increasing the amount of RAM to the JVM didn’t help.

The solution that I found was to set the following properties in my application prior to the instantiation of any of the jcifs.* classes.? In this case, these properties were set in the main method of my entry point class.

System.setProperty(“jcifs.resolveOrder”, “DNS”);
System.setProperty(“jcifs.smb.client.dfs.disabled”, “true”);

Once set, I was able to run as expected without the OutOfMemoryErrors

Leave a Reply