- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当另一位开发人员正在讨论代码清晰度时。他说使用局部变量会增加内存使用。我们认为它们将被垃圾收集。尤其是如果日志语句调用一个访问数据库/其他外部资源的函数,这尤其是一个坏主意。
但是下面的示例代码似乎支持他的说法 - 即使在使用 jconsole 调用 GC 之后,仍然看到 Worker 类使用的内存比 Worked2 少。有什么想法吗?
可用内存:124629976 - worker 124784720
package test;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ObjectLife {
public static void main(String[] args) {
// simple multi thread to mimic web app
// ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 3, 110, null, null);
Executor pool = Executors.newFixedThreadPool(3);
String type = "1";
if (args.length > 0) {
type = args[0];
}
Work w = null;
if ("1".equals(type)) {
w = new Worker();
} else {
w = new Worker2();
}
w.init(2);
System.out.println("w type " + w.getClass().getName());
Watch.me.print();
pool.execute(w);
pool.execute(Watch.me);
}
}
class Watch implements Runnable {
long prev = 0;
static Watch me = new Watch();
@Override
public void run() {
while (true) {
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
System.out.println("Intrpt thread " + e);
}
print();
}
}
public void print() {
Runtime r = Runtime.getRuntime();
long free = r.freeMemory();
System.out.println("Free " + free + ", delta " + (free - prev));
System.out.println(", av " + r.maxMemory());
prev = free;
}
}
class Work implements Runnable {
double val = 0;
public void init(double val) {
this.val = val;
}
void do2() {
}
@Override
public void run() {
int cnt = 0;
while (++cnt < 175) {
do2();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Intrpt thread " + e);
}
}
System.gc();
System.out.println("Type, v3, : " + this.getClass().getName());
Watch.me.print();
}
}
class Worker extends Work {
public void do2() {
double local = ++val;
double local2 = local * 2;
System.out.println(" local " + local + " double " + local2);
}
}
class Worker2 extends Work {
public void do2() {
System.out.println(" local " + ++val + " double " + (val * 2));
}
}
为什么 Worker 类会占用更多内存 - 即使多次调用 GC 然后将 Jconsole 与进程断开连接并等待几秒钟? (每 2 秒检查并打印一次。
可以看到代码与work的父类(super class)相同,worker除了do2()方法之外都是相同的
注意:我从 jconsole 连接并在工作循环完成后调用 GC。这个 GC 调用确实有效。调用 MX bean,可以看到可用内存下降。
旁注:我什至注意到,如果我断开 jconsole 与我的应用程序的连接,然后等待 4-5 秒 - 可用内存再次增加(我猜是连接到 jconsole 的头)。这时我进行测量。重点是我做了一些工作,等待 JVM 稳定下来然后进行测量。
运行该程序并使用 jconsole 释放内存的视频 https://www.youtube.com/watch?v=MadBdryX8uk&
jconsole 是一个免费工具,JDK 位于 bin 文件夹中。
编辑了类,这里增加了循环次数,现在无论使用哪个类,内存都是相同的!
其他人可以运行它吗?
package test;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* Run with param 1 and 2 or change type to "2" and "1" Available memory changes, used jconsole to change force garbage collection.
*
* See video https://www.youtube.com/watch?v=MadBdryX8uk
*/
public class ObjectLife {
public static void main(String[] args) {
// simple multi thread to mimic web app
// ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 3, 110, null, null);
Executor pool = Executors.newFixedThreadPool(3);
String type = "1";
if (args.length > 0) {
type = args[0];
}
Work w = null;
if ("1".equals(type)) {
w = new Worker();
} else {
w = new Worker2();
}
w.init(2);
System.out.println("w type " + w.getClass().getName());
Watch.me.print();
pool.execute(w);
pool.execute(Watch.me);
}
}
class Watch implements Runnable {
long prev = 0;
private int dieCnt = -1;
static Watch me = new Watch();
@Override
public void run() {
while (true) {
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
System.out.println("Intrpt thread " + e);
}
if (dieCnt > -1) {
dieCnt--;
if (dieCnt == 0) {
System.exit(0);
}
}
print();
}
}
public void print() {
Runtime r = Runtime.getRuntime();
long free = r.freeMemory();
System.out.println("Pr v6 Free " + free + ", delta " + (free - prev) + ", av " + r.maxMemory());
prev = free;
}
public void countDown() {
dieCnt = 3;
}
}
class Work implements Runnable {
double val = 0;
double val3 = 0;
public void init(double val) {
this.val = val;
}
void do2() {
}
@Override
public void run() {
int cnt = 0;
while (++cnt < 475) {
do2();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Intrpt thread " + e);
}
}
System.gc();
System.out.println("Type : " + this.getClass().getName());
Watch.me.print();
System.out.println("oink");
try {
Thread.sleep(9100);
} catch (InterruptedException e) {
System.out.println("Intrpt thread " + e);
}
Watch.me.countDown();
}
}
class Worker extends Work {
public void do2() {
double local = ++val;
double local2 = local * 2;
System.out.println(" local " + local + " double " + local2);
val3 = local2 + 1;
}
}
class Worker2 extends Work {
public void do2() {
System.out.println(" local " + ++val + " double " + (val * 2));
val3 = (val * 2) + 1;
}
}
最佳答案
首先,System.gc() 可能会运行垃圾收集器;或不。这确实不是测试内存消耗的可靠方法。
参见When does System.gc() do anything了解详情。
下一步:请记住,有一个即时编译器。 JIT 最适合“普通 Java”代码。很多时候,当开发人员试图变得“聪明”并编写“更高效的 Java 代码”时 - 他们最终会比以前更糟糕 - 因为 JIT 无法有效地处理这种“特殊”代码。
编写的程序要清晰易读。他们一生中可能会被阅读数百次;可能是由许多不同的人。所以,我想说的是:让你的代码易于理解更为重要。牢记效率是公平的;但只有在遇到实际问题时才开始“优化”——然后进行适当的分析;例如使用分析器。 “避免局部变量以节省内存”不是“一般”应该做的事情。
关于java - 内存使用本地变量而不是内联 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28937190/
我之前已经发布了一些这样的代码,试图在正确的位置获得侧边栏链接并以一种特殊的方式看起来,我决定朝着不同的方向前进。我现在需要的是知道我应该做什么来获得我在侧边栏旁边而不是下方标记为“内容”的 div。
我试图让多个 inline 和 inline-block 组件在 div 中垂直对齐。这个例子中的span怎么就非要往下推呢?我已经尝试了 vertical-align:middle; 和 verti
我试图让多个 inline 和 inline-block 组件在 div 中垂直对齐。这个例子中的span怎么就非要往下推呢?我已经尝试了 vertical-align:middle; 和 verti
我试图让多个 inline 和 inline-block 组件在 div 中垂直对齐。这个例子中的span怎么就非要往下推呢?我已经尝试了 vertical-align:middle; 和 verti
我很困惑...所以我在容器中有一个 UL,当我更改 UL 上方的 DIV 时,它似乎会影响 UL 之后的流程...发生了什么事? DIV 是 block 元素,对吗?和 UL 一样,对吧? 所以在这个
我问这个基本问题是为了澄清事实。都提到了这个问题及其目前接受的答案,这是不令人信服的。然而,投票第二多的答案提供了更好的洞察力,但也不是完美的。。在阅读下面的内容时,请尝试区分内联关键字和“内联”概念
function roll_over(img_name, img_src) { document[img_name].src = img_src; } 我使用此代码来显示 T 恤并在鼠标悬停时显
是否可以在 AngularJS 表达式的内联 if 语句中包含多个语句?例如,以下失败: ng-change="someCondition() ? doA(); doB() : doC()" ng-c
我在 RStudio 中使用 R Markdown 创建一个混合 Markdown 和 R 输出的报告。我知道如何在 Markdown 中使用内联 R 表达式,但我想知道如何进行相反的操作,即在 R
我们无法将表单标签添加到内联 CKEditor来自 chrome 和 IE,但它在 Firefox 中运行良好。如果我们将表单添加到内联 CKEditor,它会删除表单标签。 例如:如果我在 Fire
在我的 HTML 代码中,我有两个输入: Yes No 现在我有一个默认情况下的 div,因为它的样式显示内联,我希望当我单击“否”时,它会使其样式不显示,而"is"则会使其内联,所以我制作了此功
如何在 PowerShell 中创建带有内联 If 的语句(IIf,另请参阅: Immediate if 或 ternary If )? 如果您也认为这应该是 native PowerShell 函数
嗨。我在阅读以下问题后提出这个问题:Question_1和 Question_2 。 Question_1 没有任何合适的答案,Question_2 有替代解决方案,但不是完美的解决方案。 这里我有两
有人可以帮我解决以下语法或告诉我是否可行吗?因为我要修改 if ... else ... 条件。我不想在列表中添加重复的值,但我收到了 KeyError。 其实我不太熟悉这种说法: twins[val
有时我喜欢滥用 python 语法,特别是短的 if block : if True : print 'Hello' else : print 'Bye' 现在我尝试对函数定义做同样的事情: i
我在尝试将 Logo 容器底部对齐到零高度父级时遇到了问题。最好,我想用纯 CSS 来实现这一点。 在附带的 fiddle 中,我想让 control-group 的底部与零高度 panel 元素的顶
我需要内联编写 HTML 和 Javascript 代码,即在 HTML 正文中(需要显示一些随机整数值)我搜索了很多博客,但到目前为止没有找到任何帮助。请指教。 我想实现这个功能: Offer
好吧,这更多的是要求澄清 C++ 的一个特性是如何工作的,而不是一个是否可以的答案。我将从解释我遇到的问题开始,因为直接的答案是它不是一个很好的类设计。 我有一个类正在形成一个无法维护的 if 语句
我正在我的 C# 代码中获取一个数据库行。行包含 3 个不同的标志(3 列具有 true 或 false 值)。这些列中只有一列为真,这将决定该对象的类型。我如何在一行代码中确定该对象的类型。如果所有
在 CSS 中,我如何才能只将电话号码加粗,以便它与声明的其余部分内联,但电话号码是加粗的? 而不是在 HTML 中这样做: › Start posting jobs today– 0
我是一名优秀的程序员,十分优秀!