xintongsong commented on a change in pull request #9693: [FLINK-13984] Separate 
on-heap and off-heap managed memory pools
URL: https://github.com/apache/flink/pull/9693#discussion_r327957575
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/memory/MemoryManager.java
 ##########
 @@ -394,74 +318,79 @@ public void release(Collection<MemorySegment> segments) {
                        return;
                }
 
-               // -------------------- BEGIN CRITICAL SECTION 
-------------------
-               synchronized (lock) {
-                       if (isShutDown) {
-                               throw new IllegalStateException("Memory manager 
has been shut down.");
-                       }
+               Preconditions.checkState(!isShutDown, "Memory manager has been 
shut down.");
 
-                       // since concurrent modifications to the collection
-                       // can disturb the release, we need to try potentially 
multiple times
-                       boolean successfullyReleased = false;
-                       do {
-                               final Iterator<MemorySegment> segmentsIterator 
= segments.iterator();
+               EnumMap<MemoryType, Long> releasedMemory = new 
EnumMap<>(MemoryType.class);
 
-                               Object lastOwner = null;
-                               Set<MemorySegment> segsForOwner = null;
+               // since concurrent modifications to the collection
+               // can disturb the release, we need to try potentially multiple 
times
+               boolean successfullyReleased = false;
+               do {
+                       Iterator<MemorySegment> segmentsIterator = 
segments.iterator();
 
-                               try {
-                                       // go over all segments
-                                       while (segmentsIterator.hasNext()) {
-
-                                               final MemorySegment seg = 
segmentsIterator.next();
-                                               if (seg == null || 
seg.isFreed()) {
-                                                       continue;
-                                               }
-
-                                               final Object owner = 
seg.getOwner();
-
-                                               try {
-                                                       // get the list of 
segments by this owner only if it is a different owner than for
-                                                       // the previous one (or 
it is the first segment)
-                                                       if (lastOwner != owner) 
{
-                                                               lastOwner = 
owner;
-                                                               segsForOwner = 
this.allocatedSegments.get(owner);
-                                                       }
-
-                                                       // remove the segment 
from the list
-                                                       if (segsForOwner != 
null) {
-                                                               
segsForOwner.remove(seg);
-                                                               if 
(segsForOwner.isEmpty()) {
-                                                                       
this.allocatedSegments.remove(owner);
-                                                               }
-                                                       }
-
-                                                       if (isPreAllocated) {
-                                                               
memoryPool.returnSegmentToPool(seg);
-                                                       }
-                                                       else {
-                                                               seg.free();
-                                                               
numNonAllocatedPages++;
-                                                       }
-                                               }
-                                               catch (Throwable t) {
-                                                       throw new 
RuntimeException(
-                                                                       "Error 
removing book-keeping reference to allocated memory segment.", t);
-                                               }
+                       //noinspection ProhibitedExceptionCaught
+                       try {
+                               MemorySegment segment = null;
+                               while (segment == null && 
segmentsIterator.hasNext()) {
+                                       segment = segmentsIterator.next();
+                                       if (segment.isFreed()) {
+                                               segment = null;
                                        }
+                               }
+                               while (segment != null) {
+                                       segment = 
releaseSegmentsForOwnerUntilNextOwner(segment, segmentsIterator, 
releasedMemory);
 
 Review comment:
   I also feel the logic here is quite complicated to understand. I agree that 
it would be better to keep the current approach to avoid possible regression. 
For the moment, I suggest we explain the approach and performance concerns in 
comments for better maintainability.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to