Github user mridulm commented on a diff in the pull request: https://github.com/apache/spark/pull/16603#discussion_r98427323 --- Diff: core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java --- @@ -144,23 +148,56 @@ public long acquireExecutionMemory(long required, MemoryConsumer consumer) { // spilling, avoid to have too many spilled files. if (got < required) { // Call spill() on other consumers to release memory + // Sort the consumers according their memory usage. So we avoid spilling the same consumer + // which is just spilled in last few times and re-spilling on it will produce many small + // spill files. + TreeMap<Long, List<MemoryConsumer>> sortedConsumers = + new TreeMap<Long, List<MemoryConsumer>>(); for (MemoryConsumer c: consumers) { if (c != consumer && c.getUsed() > 0 && c.getMode() == mode) { - try { - long released = c.spill(required - got, consumer); - if (released > 0) { - logger.debug("Task {} released {} from {} for {}", taskAttemptId, - Utils.bytesToString(released), c, consumer); - got += memoryManager.acquireExecutionMemory(required - got, taskAttemptId, mode); - if (got >= required) { - break; - } + long key = c.getUsed(); + List<MemoryConsumer> list = null; + if (sortedConsumers.containsKey(key)) { + list = sortedConsumers.get(key); + } else { + list = new ArrayList<MemoryConsumer>(1); + } + list.add(c); + sortedConsumers.put(key, list); + } + } + while (true) { + // Get the consumer using the least memory more than the remaining required memory. + Map.Entry<Long, List<MemoryConsumer>> currentEntry = + sortedConsumers.ceilingEntry(required - got); + // No consumer has used memory more than the remaining required memory. + // Get the consumer of largest used memory. + if (currentEntry == null) { + currentEntry = sortedConsumers.lastEntry(); + } + // No available consumer. + if (currentEntry == null) { + break; + } + List<MemoryConsumer> cList = currentEntry.getValue(); + MemoryConsumer c = cList.remove(cList.size() - 1); + try { + long released = c.spill(required - got, consumer); + if (released > 0) { + logger.debug("Task {} released {} from {} for {}", taskAttemptId, + Utils.bytesToString(released), c, consumer); + got += memoryManager.acquireExecutionMemory(required - got, taskAttemptId, mode); + if (got >= required) { + break; } - } catch (IOException e) { - logger.error("error while calling spill() on " + c, e); - throw new OutOfMemoryError("error while calling spill() on " + c + " : " - + e.getMessage()); } + if (cList.size() == 0) { --- End diff -- Move this right below `MemoryConsumer c = cList.remove(cList.size() - 1);` (line 183) - so that we always remove from list of consumers irrespective of whether spill fails or not.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org