JDK-6306745 : Memory leaks of C-heap allocated ResourceObjs
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-08-05
  • Updated: 2012-10-08
  • Resolved: 2005-09-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b52Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
Certain GrowableArrays are being allocated with the "C_heap" flag set
to true but without the ResourceObj::C_HEAP flag being passed to
operator new for the GrowableArray object itself. This is a problem
because it is very easy to accidentally allocate the GrowableArray
underneath a ResourceMark which is later cleared, which will cause the
storage for the GrowableArray object itself (including nesting, arena,
and data pointer flags) to be overwritten later. This can cause
crashes in product mode and/or assertion failures in debug mode. In
the absence of assertions that the allocation class of the
GrowableArray matches that of its contained data array, these
allocation sites should be fixed to pass ResourceObj::C_HEAP as
argument to operator new. It is possible that these sites are benign
because the allocation is being done at a point where the ResourceMark
nesting is known, but if we add assertion checking later then these
allocation sites will fail.

When making this change the responsible engineer should be careful not
to introduce new memory leaks. In particular, the following cleanup
sequence for these arrays should be obeyed:

  array->clear_and_deallocate();
  delete array; // call destructors, but ResourceObj destructor
                // doesn't free memory which was allocated in 
                // the C-heap with malloc
  FreeHeap(Array);  // ... so use this to free the memory

See Label::free() in assembler.hpp for an example.

The relevant allocation sites for this group's code are attached.
-
-
-
-
-
hotspot/runtime_system:
src/share/vm/runtime/perfData.cpp:537:  _set = new(ResourceObj::C_HEAP) PerfDataArray(length, true);
   - call clear_and_deallocate and delete
src/share/vm/runtime/unhandledOops.cpp:19:  _oop_list = new (ResourceObj::C_HEAP)
   - call operator delete for completeness
src/share/vm/utilities/ostream.cpp:319:  fileStream* file = new(ResourceObj::C_HEAP) fileStream(try_name);
src/share/vm/utilities/ostream.cpp:330:    file = new(ResourceObj::C_HEAP) fileStream(try_name);
src/share/vm/utilities/ostream.cpp:334:    xmlStream* xs = new(ResourceObj::C_HEAP) xmlStream(file);
src/share/vm/utilities/ostream.cpp:539:    defaultStream::instance = new(ResourceObj::C_HEAP) defaultStream();
src/share/vm/utilities/ostream.cpp:551:    fileStream * gclog = new(ResourceObj::C_HEAP)
   - call FreeHeap after operator delete
~

Comments
EVALUATION Fixed two other places too in putback. Reviewed by Steve B.
07-09-2005

EVALUATION I missed 2 - one in perfData.cpp and the other in ostream.cpp with the last putback so this isn't quite fixed.
07-09-2005

EVALUATION Fixed 6306746: Add assertions to GrowableArray and ResourceObj Fixed 6306730: Incorrect management of C-heap GrowableArrays (gc) Fixed 6306736: Incorrect management of C-heap (jvmti) Fixed 6306741: Memory leaks of C-heap allocated ResourceObjs (gc) Fixed 6306743: Memory leaks of C-heap allocated ResourceObjs (jvmti) Fixed 6306745: Memory leaks of C-heap allocated ResourceObjs (runtime) Fixed 6306738: Memory leaks of C-heap allocated ResourceObjs (c2) Require GrowableArrays whose elements are allocated on the C heap to also be allocated on the C heap. This prevents leaking the GrowableArray object itself or worse, having it go out of scope of it's containing ResourceMark. Which is usually why C_heap was used for the arguments themselves. I added a field to ResourceObj to track whether new allocates on c_heap, resource area, or arena, and implemented the ResourceObj::operator delete to call FreeHeap() if the allocation was on C heap. An assert was added to the delete() operator to check that delete can only be called on a C_HEAP allocated object, because it's not needed for resource and arena allocated objects. Also, added a GrowableArray destructor to call clear_and_deallocate() so that all callers need to do is: GrowableArray<T> *ga = new (C_HEAP) GrowableArray<T>(,true); ... delete ga; // call dtor and ResourceObj::operator delete() Also I removed NonPrintingResourceObj because it's supposedly an optimization of ResourceObj. In PRODUCT mode, the virtual print() functions aren't included anyway, so I didn't get the reason for the optimization. Timed runThese -full with fastdebug and it came out about the same, actually it came out faster(?). Also, fixed compilation warning from tagged stack interpreter putback. Fix verified: y Verified by: Fixed asserts that were found with instances of Resource allocated growable arrays with C_heap elements. PRT Other testing: nsk vm.nightly.testlist (derived from vm.quick.testlist) (include jvmti tests) with -client/-server runThese -full -client/-server Initial version reviewed by: Steve B, Alan B, Ken, Mandy, Ramki, John R Final version Reviewed by: Steve B, John C, Steve G, Vladimir, Alan B
07-09-2005