0

In a Java program which I did not made and cannot change, only write extensions for it, the design forces me to create an object which will never be garbage collected. I tested and I can do it through the use of statics or anonymous object instantiation. I know I can do something like:

public static MyObject eternalObject = new MyObject();

and have it accessible anytime I want on other parts of the program. But I can also make something like:

methodOfBadDesignedProgramWhichIDidNotCreate(new MyObject());

and the program will have the object available to use it.

I have analyzed the heap dump, and in both cases the object never gets garbage collected and lives until the end of the program. The program has a terrible design since it forces you to use a lot of statics for the creation of extensions. Not to mention that most of its methods are statics and has over 2000 static variables.

I've been trying to mitigate memory leaks on it and was wondering if one usage has any advantage over another. Note that contrary to popular belief, in this specific case, anonymous object instantiation does not die at the end of the method execution because the program is so badly designed that the reference is never lost. Even when the object is not in use.

So for an object that is never garbage collected by design, does anonymous object instantiation offer advantages over a static object creation?

P.S. I would understand if this question gets closed for being too localized as it is obvious this is very bad programming design that only a small amount of population would have to deal with so they would not care about it, but I have to deal with this bad design because my boss forces me to do so and doesn't allow me to create a new program from scratch, only create extensions for it. I am just trying to mitigate the memory leaks and do the best quality programming I can on the job. Therefore, I ask. To understand if one use has advantages over another and leave either educated or convinced that there is nothing I can do about this bad designed piece of software for which I cannot change the code or create a new one.

  • Even 2k static objects don't cost that much of RAM, there is no need for you to pass time about that. The very problem would be : are those objects use concurrently and are they thread safe ? – Walfrat Apr 18 '17 at 7:43
  • I understand. Thanks. Yes, they are used concurrently, but they are thread safe. Edit: My observations on the memory behavior show me the variables being accessed from different threads, sometimes 50 threads, and everything is fine. – user5950 Apr 18 '17 at 7:45
  • And the leaks, leaks are problematic when it's connections, files, and everything related to IO, and threads leak, what about those ? If you use Java, Sonar can track it for you. – Walfrat Apr 18 '17 at 7:46
  • I was tracking the leaks with VisualVM. I was able to hunt down some and fix them. I will check with Sonar then. I did not found leaks related to IO or threads, but I did found leaks on objects creation that retained memory. But I fixed those already. Thanks. – user5950 Apr 18 '17 at 7:54
  • 1
    Can you please remove the rant parts, the excuses and the repetition of "I can only write extensions for it" from the question? This reads horrible and distracts from your main question. – Doc Brown Apr 18 '17 at 9:33
0

My guess about what happens in methodOfProgramWhichIDidNotCreate is something like

  void methodOfProgramWhichIDidNotCreate(MyObject m)
  {
      staticMember = m;
  }

where staticMember is a private static member variable, just like your variable eternalObject. If my guess is correct, the answer is simply "no", since both ways to store your object are essentially the same from the garbage collector's point of view.

1

So for an object that is never garbage collected by design, does anonymous object instantiation offer advantages over a static object creation?

I believe you are referring to the following as "static object creation?"

public static MyObject eternalObject = new MyObject();

The thing is that from the Java perspective, an above such object is created at class initialization time, but that is runtime. There really is no such thing as static object creation: all objects are instantiated at runtime. While fields can be declared static and hence statically allocated/created, objects cannot. A static field initializer is executed at runtime (albeit during class loading) very much like other statements.

Thus, from the GC point of view of the language, this object instantiation is the same as any other object instantiation.

(Various low level storage analysis optimizations not withstanding, but optimization could apply to a wide variety of forms including the other you suggest).

  • A nitpick, albeit irrelevant to this question: There really is no such thing as static object creation: all objects are instantiated at runtime ... except string constants, which are simply assumed by the byecode to already exist by the time the class is loaded. There are at least some JVMs that preallocate them in compiled modules (which may be cached between executions) ... I know this because many years ago I wrote one. – Jules Apr 18 '17 at 20:32
  • I am terribly sorry for the late reply. I had no internet access for a good while. Thank you for your answer. I was referring that the object is static. Not really about object creation. I chose a bad wording to describe what I meant. Sorry about that. I should have said: "...advantages over the usage of a static object?..." To refer to the question regarding advantages of using an anonymous object over a static one if any in this specific situation. Once again, thanks for your explanation. It DOES answers what I asked with the wrong wording even though it is not what I meant to ask. Thanks. – user5950 May 8 '17 at 11:49
  • @user5950, no problem. To be clear and reiterate, there is no such thing in Java as a static object; it has only a notion of a static field, which, at best, can be a reference to an allocated object. – Erik Eidt May 8 '17 at 16:14
  • Understood. I used again the word "static object" in that comment I made. I keep making the same mistake on my wording. But I understand the concept you mean. I'll keep repeating myself what you said to not make the same wording mistake in the future when writing. Thank you. – user5950 May 10 '17 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.