If a program relies on finalize()
to release system resources, or if there is confusion over which part of the program is responsible for releasing system resources, then there exists a possibility for a potential resource leak. In a busy system, there might be a time gap before the finalize()
method is called for an object. An attacker might exploit this vulnerability to induce a denial-of-service attack. Rule The recommendation OBJ02-J. Avoid using finalizers has more information on the usage demerits of using finalizers.
If there is unreleased memory, eventually the Java garbage collector will be called to free memory. However, if the program relies on nonmemory resources like file descriptors and database connections, unreleased resources might lead the program to prematurely exhaust its pool of resources. In addition, if the program uses resources like Lock
or Semaphore
, waiting for finalize()
to release the resources may lead to resource starvation.
...
Code Block | ||
---|---|---|
| ||
public int processFile(String fileName) throws IOException, FileNotFoundException { FileInputStream stream = new FileInputStream(fileName); BufferedReader bufRead = new BufferedReader(new InputStreamReader(stream)); String line; while((line=bufRead.readLine()) != null) { sendLine(line); } return 1; } |
...
Code Block | ||
---|---|---|
| ||
FileInputStream stream = null; BufferedReader bufRead = null; String line; try { stream = new FileInputStream(fileName); bufFreadbufRead = new BufferedReader(new InputStreamReader(stream)); while((line=bufRead.readLine()) != null) { sendLine(line); } } catch (IOException e) { } catch {FileNotFoundException e) { } finally { stream.close(); } |
...