由(XY)对确定的最小二乘拟合线性方程的 regr 斜率(YX)斜率
为了说明如何使用 regr_slope(Y, X),我将它应用于现实世界的问题。在 Java 中,如果你没有正确清理内存,垃圾就会卡住并填满内存。你每小时都会转储有关不同类的内存利用率的统计信息,并将其加载到 postgres 数据库中进行分析。
随着时间的推移,所有内存泄漏候选者将有消耗更多内存的趋势。如果你绘制这个趋势,你会想象一条线向上和向左:
^
|
s | Legend:
i | * - data point
z | -- - trend
e |
( |
b | *
y | --
t | --
e | * -- *
s | --
) | *-- *
| -- *
| -- *
--------------------------------------->
time
假设你有一个包含堆转储直方图数据的表(类的映射到它们消耗的内存量):
CREATE TABLE heap_histogram (
-- when the heap histogram was taken
histwhen timestamp without time zone NOT NULL,
-- the object type bytes are referring to
-- ex: java.util.String
class character varying NOT NULL,
-- the size in bytes used by the above class
bytes integer NOT NULL
);
为了计算每个类的斜率,我们按类分组。HAVING 子句> 0 确保我们只获得具有正斜率的候选者(一条线向上和向左)。我们按斜率降序排序,以便我们得到顶部最大内存增长率的类。
-- epoch returns seconds
SELECT class, REGR_SLOPE(bytes,extract(epoch from histwhen)) as slope
FROM public.heap_histogram
GROUP BY class
HAVING REGR_SLOPE(bytes,extract(epoch from histwhen)) > 0
ORDER BY slope DESC ;
输出:
class | slope
---------------------------+----------------------
java.util.ArrayList | 71.7993806279174
java.util.HashMap | 49.0324576155785
java.lang.String | 31.7770770326123
joe.schmoe.BusinessObject | 23.2036817108056
java.lang.ThreadLocal | 20.9013528767851
从输出中我们看到 java.util.ArrayList 的内存消耗以每秒 71.799 字节的速度增长最快,并且可能是内存泄漏的一部分。