×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

4个不对,Java developer should know what the given JVM is doing, in this case, let's just talk about the HotSpot. -sxffff(lookingforjob); 1.10 11:11 (#8548294@0) reply more

JVM is a specification. Different vendors implement the spec. Eg: Sun (now Oracle), IBM, BEA (now Oracle), SAP all implement the specification and provide their own JVMs. Sun's specific implementation is called Hotspot. BEA's is called JRockit.

JIT is part of the JVM which takes Java bytecodes and compiles it to the native processor assembly code on the machine where the program is running. Each vendor implements the JIT leveraging unique sophisticated algorithms. For eg: JIT on JRockit is different from Hotspot's JIT.
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / JAVA 再来一题
    String result = foo() + arg;
    result += boo();
    System.out.println("result = " + result);

    假设foo()和 boo()不产生Object,arg 是String,以上代码执行完以后,假设GC 马上触发,JVM 会GC 几个Object?
    • 3个? arg 1个。result 2个
      • 3. but is arg, "result=" and the ("result="+result) . String is immutable object in java. and varaible result is just a ref to arg not a object.
      • 再想想
      • bad questiob again. since jvm handle String diffently from other object. if this is part of larger program. String is not garanteed to be GCed. your context of the variable scope is unclear in this question.
        • 同意,不过string pool本来就是考点之一。题目改一下:假如不考虑String Pool,foo(),boo()只有在返回时分别产生一个String Object,方法内部产生的不算,以上代码执行完以后,假设GC 马上触发,JVM 会GC 几个Object?
          • 补充一下:方法内部产生的不算(这里方法指foo() and boo() only)
            • much better now, 5 is correct and your own question is some what still unclear what you are trying ask. here is the answers and steps:
              String result = foo() + arg; -> 3 created : foo() is one, arg is one. (foo() + arg) is 1
              result += boo(); -> 2 created: boo() is one, (result +boo) is1.
              System.out.println("result = " + result); 2: created "result=" is 1, ("result="+result) is 1
              -->there is total 7 instance of String object now.
              -->if GC is at this point only foo(), boo() and ((foo() + arg))," and (("result = " + result)) and "result = " will be gone.

              what where variable result is pointing to will not get GCed since it is not considered orphan.
              (result +boo) will not get GC since it is stiing being pointed.
              arg is not set to null so jvm will not got GC the refering object

              now pay me with 200K :)
              • 题目不清楚,很不好意思,不过太清楚了就很容易了。你可以提问,你认为清楚了,再回答。
                • already replied with steps and pointed out your problem.
                  you are not doing an actual interview and should be not be requesting questions. from your internet friends.
                  if you are intent to ask a question with clear answers then the question itself should be clear.

                  not meant to be sound offending to anyone. just want to have a better communication when chatting on line.
                • 不动Java,凑热闹,按wxlin2k的"String is immutable object in java."数就是,有string操作就加1个,比如"aaa"+"bbb"是三个。
                  • 考虑String Pool啥地,跟值有关系,数对数错跟程序质量一点关系也没。
                    • yup
                  • yup
    • 你这也叫做Java的题目。Java developers should not be concerned with this. It's the Java language designer's, or to be precise, the JVM's and its implementor's.
      4, including the literal string, if all objects mentioned in the subject are to be GC'ed.
      • 4个不对,Java developer should know what the given JVM is doing, in this case, let's just talk about the HotSpot.
        • 为什么是4个?
          • GC should be transparent to app developer, if not, it is crappy language.
    • > 6
      • 正确,能解晰一下吗?
        • you are wrong. >6 is the object instances created. not GCed.
        • 几多钱?
          • 厉害 快揭晓答案吧
            • 考 stringbuffer呢吧
              • 正确。正确。其实我是在DZone上看到: 5 Coding Hacks to Reduce GC Overhead 想跟大家分享一下。
                • that is why I said yourself already misunderstood your own question. the link is about object created. not GCed. GC will check for reference.
                  • 当然要改一下了,但StringBuilder object 是会被GC的。
                    • please refer to my answers before. the object that is refered by variable result and arg will not be GCed. since the instances are not orphaned.
                      • 好象StringBuffer object 已经不会被String object 引用了吧。
                        • think again
                          • the StringBuilder object will be GCed.
                            String s = s1 + s2
                            Behind the scenes this is translated to:

                            String s = new StringBuilder(s1).append(s2).toString();

                            after the toString() call, nothing references the StringBuilder object anymore.
                            so, the StringBuilder object will be GCed.
                          • 楼主是对的,你错的不小。
                            • think again.
                              • 你的概念嫉妒模糊,所以说你犯大错误了
                                • 说的太对了!
                                • StringBuilder最后的toString()返回的还是一个String对象 这个会被result这个变量所指。arg是另一个指向String的指针。因为这段代码的余下部分如果不是函数结束的话 result和arg就还在当前堆栈里。他们所指向的对象还不具备回收条件
                                  • 要程序员自己用StringBuilder去做合拼优化是java的缺陷,不是特点。不要纠结在这上面。
                                    而且把注意力放在stringbuilder 本来就是错误方向,因为你无法保证java编译器是否会stringbuilder进行优化编译。

                                    在已知的主流 Javac 中(包括ecj),每行的+的确都会产生一个新的StringBuilder,所有你说>6是正确。因为一共有十个对象产生,8个被回收,
                                    但是,由于java language specifation 没有要求这个是必然的转换。
                                    那么就不能保证所有的所有的编译器都会进行同样的转换和产生相同数目的StringBuilder,只能按原题的本来面目就是有中间多少必然会产生的string才是是直接正确的问题和答案,所以我一直只说有两个没回收是重点

                                    所以这个问题我一直都在说是问的不好。因为在开发者的角度,是否和如何使用StringBuilder做字符串合拼的优化应该是JVM和javac要去处理的改进,不是java本身程序员要做的。
                                    • 已经明确这是hotspot,这个java已经自动帮你做了, 程序员不需要自己手动做. 手动做的原因是为了提高performance,那会跟好的Architecture有冲突, 这方面JAVA 已经比C/C++好了很多.
                • 这篇文章说的是stringbuilder,在1.5之前用的是stringbuffer
                  • 两个 class 是一样的,StringBuilder 不 synchronize,其他几乎完全一样。
              • 要是这是这道题的原意,那题目就出错了。
                • Why
                  • 几多钱? -wincity(红卫兵); 15:59 (#8549306@0)
                    • 多多都给
                      • :D 。。。八毛儿和wxlin2k的16:52贴已经解释了。不过,wxlin2k后来给的原因不对。
                        》“假设foo()和 boo()不产生Object”。

                        产不产生Object不重要,重要的是1.有没有产生不被引用的Object,2. 有没有de-reference Object。比如,下面的foo()

                        String foo() {
                        // arr is a member variable of type Object[]
                        arr = null;
                        ......
                        }

                        你说会GC 几个Object?

                        ------------
                        另外,原文也很不严谨,没有考虑到编译器可能的优化,那些没用的StringBuffer和StringBuilder不一定会真的生成。原文建议的solution更是误人子弟。1.可读性比局部优化更重要。2.StringBuilder.append(...)这样的“优化”更是笑话。
                        • 码哥有点抬杠了,这个其实是一道好题。如果你要一道严谨的有标准答案的题,这是一道坏题。但是如果你要一道简单却能测出面试人深浅的题,这是一道绝妙的题。码哥你这个回帖其实也间接地也证明了这是一道好题,你说的优化,dereferencing,可读性,等等,一听就知道是大拿。
                          • 你说抬杠就抬杠吧。
                            来这儿不就是为了抬杠吗? :)
                          • 您是大那,说的太对了!
                            • 码哥知识的渊博和对编程的深刻认识,和你诸多的概念错误,从两个极端证明了这道题的绝妙。不明确的题也有好处,它能测出面试人的品质,比如你做不做合理的假设,你愿不愿意沟通以及沟通能力,你是不是喜欢钻牛角尖,等等等等。有没有人说过你喜欢钻牛角尖?
                              • 不合理的是你们各位大哥啊,把缺陷当技术难点和重点。
                                • 你这位同学思路也模糊啊
                                  这道题考的不是难点和重点,考的是基本概念和知识。immutability,String 的 immutability,String 是怎么运作的,GC 的工作原理,等等,都是重要的基本概念和基本知识。对这些东西一无所知或者一知半解,很可能说明你做码工是出于被迫,而不是出于自愿,更谈不上对编程有热情。一叶而知秋,你不懂这些东西,说明还有其他很多重要的东西你不懂。

                                  做合理的假设是做码工的一个重要品质。码工工作中会遇到成千上万的不确定情况,不能做合理的假设,事事都要刨根问底,你很可能会寸步难行,而且也无端地骚扰别人。这道题里,一个合理的假设就是这段代码运行后所以的东西都变成 out of scope。另一个合理的假设就是 arg 是无关项。

                                  不钻牛角尖也是码工的一个重要品质。码工每时每刻都会面对大量的细节,好的码工知道什么有关,什么无关,能做到对无关项视而不见。不好的码工事无巨细,一视同仁。最坏的码工把眼睛盯在无关项上,既浪费自己和别人的时间,也把自己的脑子搞乱,结果往往是把程序做的莫名其妙的复杂。

                                  为什么 arg 是无关项?请看看下面几种情况(假设楼主的考题是函数 f() 的 body):

                                  f(“1”+1);
                                  f(“abc”);
                                  String joe = “1”+1; f(joe);
                                • 码哥假设arg会被某个函数null就是因为原文的variable scope不清晰。跟我认为是本地变量没有冲突。大家看的角度不一样。但很佩服你居然从我的回复判断我的职业取向。真服了!
                                  • 同学概念是一头雾水啊。null 一个 arg 是笑话,码哥没有说 null arg。说 foo() 和 boo() 也要被 GC 简直就是莫名其妙。谈堆栈也是概念不清的症状。从钻牛角尖看编程的取向不是算命,是经验之谈。
                                    • 其实面试时检测 candidate 是不是喜欢钻牛角尖比问技术问题更重要。建议你以后面试不要钻牛角尖,很容易因此被刷掉的。
                                    • 已经明白你为什么说我概念模糊。赶紧写的答案的确表达不清。我犯的是楼主同样的错误,foo() 和boo()不是对象,但楼主说它返回对象,那么就有两个non-refrenecing-object.。所以这两个会GC。至于堆栈,本地变量的result(或arg)在当前函数结束前当然在stack不在heap.就酱紫
                                      • 你这个同学最大的优点就是固执。
                                        这是码哥的原话:

                                        // arr is a member variable of type Object[]
                                        arr = null;

                                        另外你看这几个 foo() 和 boo():

                                        int foo() { return 3; }
                                        String foo() { return Thread().currentThread().getName(); }
                                        String boo() { return System.getenv("PATH"); }
                                        String boo() { return "ddd"; }
                                        Object boo() { return Locale.ENGLISH; }


                                        再另外 Java 里所有 new 出来的 objects 都在 heap。
                                      • 没有说对象在stack,指向对象的是啥?兵哥您的优点是搞浑水 :)
                                    • 看来你没有看码哥的内容 :)
                                • 要不要顺便算下命?呵呵。
                              • 要更正的一句是, 我是完全认同码哥的回复,只是我说原题的重点搞错了,码哥的回答最合理。因为java language specifation没有要求字符串合拼一定要编译成StringBuilder,我们不能认为SB一定有
                                • 其实JVM 是有规定使用StringBuilder的, 并不是我想出来的.
                                  • 10个8个字符串操作根本不值得考虑性能,只有在大量字符操作时才值得StringBuilder 。when to use StringBuilder in java
                                  • JLS 出处,请注意原文用了"may", 不是”must".所以只是现在的java compiler太懒没有做好优化,但不是“有规定要求”。
                                    • 已经明确是hotspot
                                      • 这点可以认同。但请看看你自己原来的题目哪个地方有这个假设或预设条件。JVM是通称,不能预设是hotspot,,而且这个转换是在comipler不是JVM
                                        • 回复下面的,咱都对,我说编译器没做好优化。你说程序员应该自己动手优化,一体两面
                                          • What complie .java to .class? that is not jvm that is javac complier. think before you replie me
                                            • can be JIT
                                              • wow full stop.无言了。投降,认输了!
                                        • 4个不对,Java developer should know what the given JVM is doing, in this case, let's just talk about the HotSpot. -sxffff(lookingforjob); 1.10 11:11 (#8548294@0) reply more
                                          JVM is a specification. Different vendors implement the spec. Eg: Sun (now Oracle), IBM, BEA (now Oracle), SAP all implement the specification and provide their own JVMs. Sun's specific implementation is called Hotspot. BEA's is called JRockit.

                                          JIT is part of the JVM which takes Java bytecodes and compiles it to the native processor assembly code on the machine where the program is running. Each vendor implements the JIT leveraging unique sophisticated algorithms. For eg: JIT on JRockit is different from Hotspot's JIT.
                                • Java Programming Guidelines: This section covers issues related to Java coding and performance. The guidelines outlined are not specific to GlassFish Server, but are general rules that are useful in many situations.
                                  Use StringBuilder to Concatenate Strings
                                  To improve performance, instead of using string concatenation, use StringBuilder.append().

                                  String objects are immutable – that is, they never change after creation. For example, consider the following code:

                                  String str = "testing";
                                  str = str + "abc";
                                  str = str + "def";
                                  The compiler translates this code as:

                                  String str = "testing";
                                  StringBuilder tmp = new StringBuilder(str);
                                  tmp.append("abc");
                                  str = tmp.toString();
                                  StringBulder tmp = new StringBuilder(str);
                                  tmp.append("def");
                                  str = tmp.toString();
                                  This copying is inherently expensive and overusing it can reduce performance significantly. You are far better off writing:

                                  StringBuilder tmp = new StringBuilder("testing");
                                  tmp.append("abc");
                                  tmp.append("def");
                                  String str = tmp.toString();
    • 此题无解
      foo() bar()不产生object,但返回的object的toString里面产生不产生object呢?没说。这个string + string jvm用stringbuffer来解决,stringbuffer里面还是char[],题目中的的string长度不说,stringbuffer在append的时候可能需要重新创建更大的char[],太多未知条件了,所以说无解,题目很烂。
      • 所以 > 6 是最佳答案
        • 其实 > 9 才是最佳答案
          • 又想了一下, > 12 更好
            • 此题无解 -aaronding(流浪的八毛儿); 16:48 (272 bytes. #8549505@0)。。。。:D
    • Does it make sense to know details about GC? I've been a Java guy for over 15 years, but never ran into a situation related to it.
      • 上面那些是钻牛角尖用的。知道适当的时候用dispose() 就可以了。
    • 每次被问到类似的题目,我就想起孔乙己故事里的回字有几种写法。
      • 严重同意