Java 非恒定内存使用异常

Java 非恒定内存使用异常,java,caliper,Java,Caliper,我在MeasureMemory打开的情况下进行测试,并出现“非恒定内存使用”异常。 我没有按照错误消息的建议修改Rep。我错过了什么 [caliper] allocating java/lang/String with size 32 bytes [caliper] allocating char array with 16 elements with size 48 bytes [caliper] allocating char array with 16 elements with si

我在MeasureMemory打开的情况下进行测试,并出现“非恒定内存使用”异常。

我没有按照错误消息的建议修改Rep。我错过了什么

[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 16 elements with size 48 bytes 
[caliper] allocating char array with 16 elements with size 48 bytes 
[caliper] allocating java/lang/String with size 32 bytes  
[caliper] allocating char array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 16 elements with size 48 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 16 elements with size 48 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 34 elements with size 88 bytes 
[caliper] allocating char array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 16 elements with size 48 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 16 elements with size 48 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 70 elements with size 160 bytes 
[caliper] allocating char array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating java/lang/String with size 32 bytes 
[caliper] allocating char array with 142 elements with size 304 bytes 
[caliper] allocating char array with 74 elements with size 168 bytes 
[caliper] allocating java/lang/String with size 32 bytes

[caliper] allocating org/apache/commons/collections/map/AbstractReferenceMap$WeakRef with size 32 bytes 
[caliper] allocating org/apache/commons/collections/map/AbstractReferenceMap$ReferenceEntry with size 32 bytes 
[caliper] allocating java/lang/Object array with 0 elements with size 16 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating org/joda/time/DateTime with size 24 bytes 
[caliper] allocating org/hibernate/util/IdentityMap$IdentityKey with size 16 bytes 
[caliper] allocating org/hibernate/event/LoadEvent with size 40 bytes 
[caliper] allocating org/hibernate/engine/EntityKey with size 48 bytes 
[caliper] allocating org/hibernate/event/LoadEvent with size 40 bytes 
[caliper] allocating org/hibernate/engine/EntityKey with size 48 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] allocating java/lang/Object array with 1 elements with size 24 bytes 
[caliper] ...more allocations... 
[caliper] [done measured section] 
[caliper] 482 instance(s) allocated per rep 
[caliper] 0 out of thread instance(s) allocated in 1 reps 
[caliper] [starting measured section] 
[caliper] see first run for list of allocations 
[caliper] [done measured section] 
[caliper] 2966 instance(s) allocated per rep 
[caliper] 0 out of thread instance(s) allocated in 2 reps 
错误:并非所有内部循环的重复都分配相同的次数!reps循环应该使用固定数量的分配。您是否在循环中使用重复次数的值?

现在,我在AllocationMeasurer中对代码进行了注释,该代码抛出了非恒定MemoryUsage(不确定这是否是个好主意!),以便我可以继续。我现在得到负字节分配

0% Scenario{vm=java, trial=0, benchmark=GetCompleteUserInfoByEmail, length=1, trials=5, memory=-Xmx1024M} 5017892000.00 ns; σ=18238565.40 ns @ 3 trials, allocated -45356 instances for a total of -2873264B
50% Scenario{vm=java, trial=0, benchmark=GetUserByEmail, length=10, trials=5, memory=-Xmx1024M} 5044517000.00 ns; σ=25651666.81 ns @ 3 trials, allocated -45816 instances for a total of -2873264B

长度实例B s线性运行时
1-45356.000-2873264.000 5.02==================================
10 -45816.000 -2873264.000 5.04 ==============================

vm:java
试用期:0
基准:GetCompleteServerInfo电子邮件
试验:5次

内存:-Xmx1024M

您正在重复某些函数以精确测量其参数,并且每次运行时它都会执行不同的操作。这使得进行精确测量的尝试失败。请再次检查。我正在测量db中的读取操作,返回的数据集与此相同。请尝试跳过第一次读取。它可能会分配额外的资源或将一些东西放入缓存中。@ndchandar:为什么要使用卡钳来对数据库操作进行基准测试?Caliper是为微基准(单元测试规模)而设计的,而不是集成测试规模的基准。您正在重复某些函数以精确测量其参数,每次运行时它都会做一些不同的事情。这使得进行精确测量的尝试失败。请再次检查。我正在测量db中的读取操作,返回的数据集与此相同。请尝试跳过第一次读取。它可能会分配额外的资源或将一些东西放入缓存中。@ndchandar:为什么要使用卡钳来对数据库操作进行基准测试?卡尺是为微基准(单元测试规模)而设计的,而不是集成测试规模的基准。