attilapiros commented on a change in pull request #34846:
URL: https://github.com/apache/spark/pull/34846#discussion_r769417875



##########
File path: core/src/main/scala/org/apache/spark/util/Utils.scala
##########
@@ -3204,6 +3204,14 @@ private[spark] object Utils extends Logging {
     }
     files.toSeq
   }
+
+  val isG1GarbageCollector: Boolean = {
+    ManagementFactory.getGarbageCollectorMXBeans

Review comment:
       When I looked for how to find out what the garbage collector type is I 
bumped into this several times:
   
   ```java
     HotSpotDiagnosticMXBean diagnostic = 
ManagementFactoryHelper.getDiagnosticMXBean();
   
       VMOption option = diagnostic.getVMOption("UseG1GC");
       if (option.getValue().equals("false")) {
         ...
       }
   ```
   For example at the [OpenJDK 
tests](https://www.programcreek.com/java-api-examples/?code=AdoptOpenJDK%2Fopenjdk-jdk8u%2Fopenjdk-jdk8u-master%2Fhotspot%2Ftest%2Fgc%2Farguments%2FTestG1HeapRegionSize.java
   ). 
   
   Is there any reason why a different solution have been chosen here?
   

##########
File path: core/src/main/scala/org/apache/spark/memory/MemoryManager.scala
##########
@@ -255,7 +256,17 @@ private[spark] abstract class MemoryManager(
     }
     val size = ByteArrayMethods.nextPowerOf2(maxTungstenMemory / cores / 
safetyFactor)
     val default = math.min(maxPageSize, math.max(minPageSize, size))
-    conf.get(BUFFER_PAGESIZE).getOrElse(default)
+    val sizeAsBytes = conf.get(BUFFER_PAGESIZE).getOrElse(default)
+    // If we are using G1 GC, it's better to take the LONG_ARRAY_OFFSET into 
consideration
+    // so that the requested memory size is power of 2 and can be divided by 
G1 region size
+    // to reduce memory waste within one G1 region
+    if (Utils.isG1GarbageCollector &&
+      tungstenMemoryMode == MemoryMode.ON_HEAP &&
+      sizeAsBytes % (1024 * 1024) == 0) {
+      sizeAsBytes - Platform.LONG_ARRAY_OFFSET

Review comment:
       If I get this right in case of G1GC the best would be if we choose a 
pageSize where the following holds:
   
   ```
     G1HeapRegionSize % (pageSize + Platform.LONG_ARRAY_OFFSET) == 0
   ```
   And when When BUFFER_PAGESIZE is not set we are free to choose it as:
   ```
   pageSize = G1HeapRegionSize - Platform.LONG_ARRAY_OFFSET;
   ```
   
   And with the above code we just try to calculate G1HeapRegionSize with our 
own way. But what about accessing this value?
   Like the same way used in the [OpenJDK 
tests](https://www.programcreek.com/java-api-examples/?code=AdoptOpenJDK%2Fopenjdk-jdk8u%2Fopenjdk-jdk8u-master%2Fhotspot%2Ftest%2Fgc%2Farguments%2FTestG1HeapRegionSize.java):
   
   ```java
     HotSpotDiagnosticMXBean diagnostic = 
ManagementFactoryHelper.getDiagnosticMXBean();
     option = diagnostic.getVMOption("G1HeapRegionSize");
   ```
   
   As I see the OpenJDK test was executed like:
   ```
   run main/othervm -Xmx64m TestG1HeapRegionSize 1048576
   ```
   So `diagnostic.getVMOption("G1HeapRegionSize")` gives back the calculated 
region size.
   
   




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to