Skip to content

Commit

Permalink
Try to fix memory allocation on jdk11
Browse files Browse the repository at this point in the history
  • Loading branch information
yurem committed Aug 25, 2020
1 parent da8f65d commit 1f52f36
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -655,27 +655,36 @@ private static File prepareTempDir(PluggableBackendCfg backendCfg, String tmpDir
* Calculates the amount of available memory which can be used by this import, taking into account whether
* the import is running offline or online as a task.
*/
private long calculateAvailableHeapMemoryForBuffersAfterGC()
public static long calculateAvailableHeapMemoryForBuffersAfterGC()
{
final Runtime runTime = Runtime.getRuntime();
runTime.gc();
runTime.gc();

// First try calculation based on oldgen size
long availableHeapSize = 0;
final List<MemoryPoolMXBean> mpools = ManagementFactory.getMemoryPoolMXBeans();
for (final MemoryPoolMXBean mpool : mpools)
{
final MemoryUsage usage = mpool.getUsage();
if (usage != null && mpool.getName().endsWith("Old Gen") && usage.getMax() > 0)
{
final long max = usage.getMax();
return (max > SMALL_HEAP_SIZE ? (max * 90 / 100) : (max * 70 / 100));

// Use 70 % of max - used - static limit
availableHeapSize = max * 70 / 100 - usage.getUsed() - 30 * 1024 * 1024;
}
}
// Fall back to 40% of overall heap size (no need to do gc() again).
return (runTime.freeMemory() + (runTime.maxMemory() - runTime.totalMemory())) * 40 / 100;
long runtimeMemory = (runTime.freeMemory() + (runTime.maxMemory() - runTime.totalMemory())) * 40 / 100;

if (availableHeapSize > 0) {
// Provide maximum allowed by both methods
return Math.max(availableHeapSize, runtimeMemory);
}

return runtimeMemory;
}
}

/** Source of LDAP {@link Entry}s to process. */
private interface Source extends Closeable
Expand Down

0 comments on commit 1f52f36

Please sign in to comment.