Skip to content

Commit

Permalink
Added check for GC disable flag
Browse files Browse the repository at this point in the history
fixes #733
  • Loading branch information
amitjoy committed Feb 14, 2024
1 parent 5b747bf commit c3a026c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,5 +622,5 @@ public interface Agent {
/**
* Performs a garbage collection
*/
void gc();
void gc() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.osgifx.console.agent.dto.XHeapUsageDTO.XMemoryPoolMXBean;
import com.osgifx.console.agent.dto.XHeapUsageDTO.XMemoryUsage;

public final class XHeapAdmin {
public final class XJmxAdmin {

private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
private static volatile Object hotspotMBean;
Expand Down Expand Up @@ -110,9 +110,25 @@ public byte[] heapdump() throws Exception {
}
}

public void gc() throws Exception {
initHotspotMBean();
final Class<?> clazzBean = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
final Class<?> clazzVMOption = Class.forName("com.sun.management.VMOption");
final Method method1 = clazzBean.getMethod("getVMOption", String.class);
final Method method2 = clazzVMOption.getMethod("getValue");
final Object vmOption = method1.invoke(hotspotMBean, "DisableExplicitGC");
final Object vmOptionValue = method2.invoke(vmOption);

if (vmOptionValue != null && "true".equalsIgnoreCase(vmOptionValue.toString())) {
logger.atInfo().msg("Explicit GC invocation is disabled").log();
return;
}
System.gc();
}

private static void initHotspotMBean() throws Exception {
if (hotspotMBean == null) {
synchronized (XHeapAdmin.class) {
synchronized (XJmxAdmin.class) {
if (hotspotMBean == null) {
hotspotMBean = getHotspotMBean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
import com.osgifx.console.agent.admin.XDtoAdmin;
import com.osgifx.console.agent.admin.XEventAdmin;
import com.osgifx.console.agent.admin.XHcAdmin;
import com.osgifx.console.agent.admin.XHeapAdmin;
import com.osgifx.console.agent.admin.XHttpAdmin;
import com.osgifx.console.agent.admin.XJmxAdmin;
import com.osgifx.console.agent.admin.XLogReaderAdmin;
import com.osgifx.console.agent.admin.XLoggerAdmin;
import com.osgifx.console.agent.admin.XMetaTypeAdmin;
Expand Down Expand Up @@ -992,7 +992,7 @@ public List<XHttpComponentDTO> getHttpComponents() {
public XHeapUsageDTO getHeapUsage() {
final boolean isJMXWired = di.getInstance(PackageWirings.class).isJmxWired();
if (isJMXWired) {
return di.getInstance(XHeapAdmin.class).init();
return di.getInstance(XJmxAdmin.class).init();
}
logger.atWarn().msg(packageNotWired(JMX)).log();
return null;
Expand All @@ -1004,15 +1004,21 @@ public RuntimeDTO getRuntimeDTO() {
}

@Override
public void gc() {
public void gc() throws Exception {
final boolean isJMXWired = di.getInstance(PackageWirings.class).isJmxWired();
if (isJMXWired) {
di.getInstance(XJmxAdmin.class).gc();
return;
}
System.gc();
logger.atWarn().msg(packageNotWired(JMX)).log();
}

@Override
public byte[] heapdump() throws Exception {
final boolean isJMXWired = di.getInstance(PackageWirings.class).isJmxWired();
if (isJMXWired) {
return di.getInstance(XHeapAdmin.class).heapdump();
return di.getInstance(XJmxAdmin.class).heapdump();
}
logger.atWarn().msg(packageNotWired(JMX)).log();
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,25 @@ private void performGC() {
if (agent == null) {
return;
}
executor.runAsync(agent::gc);
final Task<Void> gcTask = new Task<>() {

@Override
protected Void call() throws Exception {
try {
updateMessage("Performing GC");
agent.gc();
return null;
} catch (final Exception e) {
logger.atError().withException(e).log("Cannot perform GC");
threadSync.asyncExec(() -> {
progressDialog.close();
FxDialog.showExceptionDialog(e, getClass().getClassLoader());
});
throw e;
}
}
};
executor.runAsync(gcTask);
}

private void heapDump() {
Expand Down

0 comments on commit c3a026c

Please sign in to comment.