javamemory-leaksjprofileryourkit

Identify open FileOutputStreams by the process


We have a memory leak in our Java application as the number of FileOutputStream open - closed difference is increasing with time. In our case heap is pretty stable but since FileOutputStreams are open we are facing memory leak in JVM native memory.

Could you please suggest a way by which we can identify the piece of code in the codebase that may be contributing to this leak.


Solution

  • In JProfiler, you can find the allocation spots of unclosed FileOutputStreams like this:

    1. Record allocations and take a heap snapshot
    2. Select all instances of FileOutputStream and create a new object set
    3. Select the "Outgoing references" view
    4. Select the "closed" field of a FileOutputStream instance and invoke "Apply filter->By restricting the selected value", then select "false" in the value dialog.
    5. Go to the "Allocations" view of the heap walker and inspect the tree of allocation call stacks or the allocation hot spots view.

    enter image description here