Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

71
LINES

< > BotCompany Repo | #1018951 // MemoryWarningSystem

JavaX fragment (include)

1  
import javax.management.*;
2  
import java.lang.management.*;
3  
4  
/**
5  
 * from: https://www.javaspecialists.eu/archive/Issue092.html
6  
 * This memory warning system will call the listener when we
7  
 * exceed the percentage of available memory specified.  There
8  
 * should only be one instance of this object created, since the
9  
 * usage threshold can only be set to one number.
10  
 */
11  
sclass MemoryWarningSystem {
12  
  new L<Listener> listeners;
13  
14  
  public interface Listener {
15  
    public void memoryUsageLow(long usedMemory, long maxMemory);
16  
  }
17  
18  
  public MemoryWarningSystem() {
19  
    MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
20  
    NotificationEmitter emitter = (NotificationEmitter) mbean;
21  
    emitter.addNotificationListener(new NotificationListener() {
22  
      public void handleNotification(Notification n, Object hb) {
23  
        if (n.getType().equals(
24  
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
25  
          long maxMemory = tenuredGenPool.getUsage().getMax();
26  
          long usedMemory = tenuredGenPool.getUsage().getUsed();
27  
          for (Listener listener : listeners) {
28  
            listener.memoryUsageLow(usedMemory, maxMemory);
29  
          }
30  
        }
31  
      }
32  
    }, null, null);
33  
  }
34  
35  
  public boolean addListener(Listener listener) {
36  
    return listeners.add(listener);
37  
  }
38  
39  
  public boolean removeListener(Listener listener) {
40  
    return listeners.remove(listener);
41  
  }
42  
43  
  private static final MemoryPoolMXBean tenuredGenPool =
44  
      findTenuredGenPool();
45  
46  
  public static void setPercentageUsageThreshold(double percentage) {
47  
    if (percentage <= 0.0 || percentage > 1.0) {
48  
      throw new IllegalArgumentException("Percentage not in range");
49  
    }
50  
    long maxMemory = tenuredGenPool.getUsage().getMax();
51  
    long warningThreshold = (long) (maxMemory * percentage);
52  
    tenuredGenPool.setUsageThreshold(warningThreshold);
53  
  }
54  
55  
  /**
56  
   * Tenured Space Pool can be determined by it being of type
57  
   * HEAP and by it being possible to set the usage threshold.
58  
   */
59  
  private static MemoryPoolMXBean findTenuredGenPool() {
60  
    for (MemoryPoolMXBean pool :
61  
        ManagementFactory.getMemoryPoolMXBeans()) {
62  
      // I don't know whether this approach is better, or whether
63  
      // we should rather check for the pool name "Tenured Gen"?
64  
      if (pool.getType() == MemoryType.HEAP &&
65  
          pool.isUsageThresholdSupported()) {
66  
        return pool;
67  
      }
68  
    }
69  
    throw new AssertionError("Could not find tenured space");
70  
  }
71  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1018951
Snippet name: MemoryWarningSystem
Eternal ID of this version: #1018951/2
Text MD5: b36c52234a19a554c7bb74878841c110
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-10-18 13:31:59
Source code size: 2526 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 298 / 810
Version history: 1 change(s)
Referenced in: [show references]