我有一个单一的记录器类.在它的析构函数中,我调用Close()打印日志的页脚,然后关闭StreamWriter.
public void Close()
{
WriteLogFileFooter();
_logFile.Flush();
_logFile.Close();
}
Run Code Online (Sandbox Code Playgroud)
问题是当从程序中的其他地方调用System.Enviornment.Exit(1)时(我自己没有编写的部分),页脚永远不会打印,我的记录器会因尝试写入封闭流而抛出异常.我只能假设Exit命令导致我的StreamWriter在我的Singleton被破坏之前被关闭.我尝试在我的StreamWriter上使用GC.SupressFinalize(),但这似乎没有帮助.
我正在尝试调查带有枚举键的字典是否仍然在较新版本的.Net中产生垃圾(比如说> = 4)
请参阅Shawn Hargreaves博客文章,了解为何我甚至为此烦恼......(http://blogs.msdn.com/b/shawnhar/archive/2007/07/02/twin-paths-to-garbage -collector-nirvana.aspx)非常具体我知道但是xbox上的垃圾是/可能是一个非常棘手的问题.
我创建了一个小的.Net v4控制台应用程序,比较为Dictionary和Dicationary生成的IL,并注意到两组代码中的"盒子"操作码,这让我很困惑.
.method private hidebysig
instance int32 FindEntry (
!TKey key
) cil managed
{
// Method begins at RVA 0x61030
// Code size 138 (0x8a)
.maxstack 3
.locals init (
[0] int32,
[1] int32
)
IL_0000: ldarg.1
IL_0001: box !TKey <----Hmmmm!
IL_0006: brtrue.s IL_000e
IL_0008: ldc.i4.5
IL_0009: call void System.ThrowHelper::ThrowArgumentNullException(valuetype System.ExceptionArgument)
IL_000e: ldarg.0
IL_000f: ldfld int32[] class System.Collections.Generic.Dictionary`2<!TKey, !TValue>::buckets
IL_0014: brfalse.s IL_0088
Run Code Online (Sandbox Code Playgroud)
https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.box%28v=vs.110%29.aspx
将值类型(在valTypeToken中指定的类型)转换为真实对象引用.
这里的盒子不是堆分配吗?如果没有,那么我怎么知道何时存在可能导致Xbox崩溃的堆分配?(从查看IL)它是否依赖于其他一些上下文?内存分析器(例如CLR Profiler)是唯一可以确定的方法吗?
我正在编写一个系统,它要求我获取对象中的属性值,最好使用反射。这个项目是针对 xbox360 的,它在紧凑的框架上运行,因此有一个缓慢的垃圾收集器 - 这意味着我避免分配是绝对重要的!
我发现做到这一点的唯一方法是:
Foo Something; //an object I want to get data from
PropertyInfo p; //get this via reflection for the property I want
object value = p.GetGetmethod().Invoke(Something, null);
//Now I have to cast value into a type that it should be
Run Code Online (Sandbox Code Playgroud)
我不喜欢这个有两个原因:
是否有一些通用方法可以从属性中获取值,并且不会对基元进行装箱?
编辑::为了回应 Jons 的回答,从他的博客中窃取的代码不会导致分配,问题已解决:
String methodName = "IndexOf";
Type[] argType = new Type[] { typeof(char) };
String testWord = "TheQuickBrownFoxJumpedOverTheLazyDog";
MethodInfo method = typeof(string).GetMethod(methodName, argType);
Func<char, int> converted = (Func<char, int>)Delegate.CreateDelegate
(typeof(Func<char, …Run Code Online (Sandbox Code Playgroud) 我已经发布了我的错误代码的屏幕截图。

heights 输出

请有人可以帮助我吗?
我在Hotspot JVM上运行Java 7 update 45上的应用程序.我试图调整参数以适应低延迟应用程序,下面是我为此次运行设置的JVM参数.GC日志中各个步骤的时间(约25ms)不会累计到暂停时间(> 600ms).我怎么知道是什么导致这个暂停时间如此之高?在大多数收藏中,他们加起来很好,并且在暂停时间目标中.我偶尔会看到如此大的停顿,一次在15-20分钟左右(虽然不是那么规律).
-server -Xmx2g -Xms2g -XX:PermSize = 128m -XX:MaxPermSize = 128m -XX:+ UseG1GC -XX:MaxGCPauseMillis = 30 -XX:ParallelGCThreads = 9 -XX:ConcGCThreads = 4 -XX:G1HeapRegionSize = 4M -XX: InitiatingHeapOccupancyPercent = 45 -XX:+ PrintTLAB -XX:+ AggressiveOpts -Xloggc:/integral/logs/gc.log -verbose:gc -XX:+ PrintTenuringDistribution -XX:+ PrintGCDateStamps -XX:+ PrintAdaptiveSizePolicy -XX:+ PrintGCDetails -XX :+ PrintGCApplicationConcurrentTime -XX:+ PrintGCApplicationStoppedTime -XX:+ PrintSafepointStatistics -XX:PrintSafepointStatisticsCount = 1 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port = 3026 -Dcom.sun.management.jmxremote.local. only = false -Dcom.sun.management.jmxremote.authenticate = false -Dcom.sun.management.jmxremote.ssl = false"
2014-01-28T16:16:12.084+0000: 27507.054: Application time: 8.9506630 seconds
2014-01-28T16:16:12.084+0000: 27507.055: [GC …Run Code Online (Sandbox Code Playgroud) 我正在尝试将“旧方式”循环转换为基于流的方法。该循环采用一大组元素并返回一个落在给定半径内的子集。结果按距离排序,并且结果本身具有方便的计算距离(用于演示)。它以旧方式工作正常,我不需要对它进行 Java8 化。但我真的很想。:-) 如果只是为了能够在这个傻瓜上使用 .parallel() 就好了。
问题是……我的 filter() 使用了一个计算值(距离),然后我需要在后续的 map() 步骤中使用它(以构建“with distance”实例)。假设距离计算很昂贵。这是 Java 7 的方式……向下滚动以查看 getNearestStations() 方法:
public interface Coordinate {
double distanceTo(Coordinate other);
}
public class Station {
private final String name;
private final Coordinate coordinate;
public Station(String name, Coordinate coordinate) {
this.name = name;
this.coordinate = coordinate;
}
public String getName() {
return name;
}
public Coordinate getCoordinate() {
return coordinate;
}
}
public class StationWithDistance extends Station implements Comparable<StationWithDistance> {
private final double distance;
public …Run Code Online (Sandbox Code Playgroud) 我在 Android 上有一个客户端,它使用 Android VPNService 公开的 TUN 接口来拦截包。在这方面,我读取所有传入的数据包并将它们写入 TUN 接口。这基本上意味着我对收到的每个数据包进行写入。这样做的问题是,Android 似乎为每个写入和读取调用分别分配了int[67] 和 int[80],这是大量垃圾,导致大量重复垃圾收集。
这是读取操作的代码,它从我的服务器读取所有传入的数据包。正如评论中提到的,我从一个简单的数据报频道中读到:
public void run() {
ByteBuffer buffer = ByteBuffer.allocate(32767);
while (!Thread.currentThread().isInterrupted()) {
try {
buffer.clear();
// getVpnTunnel is a simple getter for the underlying datagram channel.
int packetSize = conn.getVpnTunnel().read(buffer); // int[80] garbage allocated here
buffer.flip();
if (packetSize < 1) {
// No data, wait then read.
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
continue;
}
switch (...) { …Run Code Online (Sandbox Code Playgroud) 我期待输出2,3,但我得到了垃圾值.为什么?
这是我的代码:
#include <iostream>
using namespace std;
class A
{
public:
int a, b;
A()
{
cout << a << " " << b;
}
A(int x, int y)
{
a = x;
b = y;
A(); // calling the default constructor
}
};
int main()
{
A ob(2, 3);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 易碎队列和终结队列之间有什么区别?一种解决方案:从完成队列过渡到可伸缩队列.net垃圾回收
我认为在加载.yml file库 PyYAML时出现内存泄漏。
我已按照以下步骤操作:
import yaml
d = yaml.load(open(filename, 'r'))
Run Code Online (Sandbox Code Playgroud)
由该方法所使用的存储器(我已经得到它top或htop)从生长60K到160M,而文件的大小是低于1M。
然后,我完成了下一个命令:
sys.getsizeof(d)
Run Code Online (Sandbox Code Playgroud)
并且它返回的值低于400K。
我也尝试将垃圾收集器与 一起使用gc.collect(),但什么也没发生。
如您所见,似乎存在内存泄漏,但我不知道是什么导致了它,也不知道如何释放这么多内存。
任何的想法?
garbage ×10
.net ×3
c# ×3
java ×2
android ×1
c++ ×1
constructor ×1
datagram ×1
dictionary ×1
filter ×1
g1gc ×1
ios ×1
java-7 ×1
java-8 ×1
memory-leaks ×1
objective-c ×1
performance ×1
python ×1
reflection ×1
singleton ×1
thread-sleep ×1
undefined ×1
xbox360 ×1
yaml ×1