gpt4 book ai didi

Play 内存过度使用!框架方法调用

转载 作者:行者123 更新时间:2023-12-04 06:00:03 24 4
gpt4 key购买 nike

在我的一个 Controller 中,我有多个 URL,它们最终将以相同的方式呈现。例如,此方法扫描服务器所在的网络,缓存每个连接设备和每个监听特定端口的设备的 String 表示,然后将该信息发送到另一个方法进行渲染:

public static void networkScan(String networkTarget, String port)
{
//These two lists will never have more than 256 total entries
List<InetSocketAddress> listeningDevices;
Map<String, String> allDevices;

...Logic for discovering network devices...

//Store the results in a cache, for history preservation in the browser
Cache.set(session.getId() + "listeningDevices", listeningDevices);
Cache.set(session.getId() + "allDevices", allDevices);
showScan(listeningDevices, allDevices);
}

public static void showScan(List<InetSocketAddress> listeningDevices, Map<String, String> allDevices)
{
render(listeningDevices, allDevices);
}

public static void getCachedScan()
{
List<InetSocketAddress> listeningDevices = (List<InetSocketAddress>)Cache.get(session.getId() + "listeningDevices");
Map<String, String> allDevices = (Map<String, String>)Cache.get(session.getId() + "allDevices");
if(listeningDevices == null)
listeningDevices = new ArrayList<InetSocketAddress>();
if(allDevices == null)
allDevices = new TreeMap<String, String>();

renderScan(listeningDevices, allDevices);
}

这样做会导致 Play 进行一些奇怪的数组复制,最终会占用无限内存。如果我改变我的电话 showScan()简单地 render()并创建一个名为 networkScan.html 的 View ,一切正常,没有内存错误。

根据不同的缓存设置,我还有其他几种方法也使用 showScan。我不想要很多本质上都是彼此副本的 View ,所以我试图通过一种方法来处理一个相应的 View 。

最佳答案

这行不通:

   showScan(listeningDevices, allDevices);
}

public static void showScan(List<InetSocketAddress> listeningDevices, Map<String, String> allDevices)
{

因为 play 会将 listenDevices + allDevices 序列化为 Strings 并尝试从中构建一个 url。

要么直接在 networkScan() 中呈现结果,要么将内容存储在特定键下的缓存中
就像你已经做的那样,然后做类似的事情
public static void networkScan(String networkTarget, String port)
{
//These two lists will never have more than 256 total entries
List<InetSocketAddress> listeningDevices;
Map<String, String> allDevices;



...Logic for discovering network devices...

//Store the results in a cache, for history preservation in the browser
Cache.set(session.getId() + "listeningDevices", listeningDevices);
Cache.set(session.getId() + "allDevices", allDevices);
showScan(session.getId());
}

public static void showScan(String sessionId)
{
List<InetSocketAddress> listeningDevices = Cache.get(sessionId + "listeningDevices");
Map<String, String> allDevices = Cache.get(sessionId + "allDevices");
render(listeningDevices, allDevices);
}

关于Play 内存过度使用!框架方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9022154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com