Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect/avoid autoboxing in Java?

I'm working on a program that deals constantly with gigabytes of data, mostly primitives and strings. I need to avoid having the primitives converted to Objects by autoboxing as this explodes the heap size and GC-time.

Currently, I make changes and run the code in VisualVM and see I have millions of extra Integers or Shorts, or Objects. Then I step through the code in a debugger into my libraries and JDK classes to find where the boxing occurs. Is there tooling to help with this?

I use Intellij as my IDE. In Intellij, you can use an inspection to find the auto-boxing in your own code, but it doesn't seem to extend to library code. To do so, select from the menu:

Analyzye >> Run Inspection by Name...

Then type in 'auto' at the prompt. An auto-boxing inspection should appear for selection.

However, at this point, I have removed essentially all auto-boxing from my own code. What I need is to be able to find out when i pass a primitive into a library method, is the library code auto-boxing the primitive at any point.

like image 618
L. Blanc Avatar asked Mar 19 '16 23:03

L. Blanc


People also ask

How do I stop Autoboxing in Java?

You would also lose a LOT of other important Java language features by reverting to the Java 1.4 level; e.g. generics, enums, and so on. In short, autoboxing / unboxing is a fundamental part of the modern Java language and it can't be turned off and on at will. Save this answer.

Is Autoboxing and boxing same?

Boxing is the mechanism (ie, from int to Integer ); autoboxing is the feature of the compiler by which it generates boxing code for you.

Why boxing is used in Java?

Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique lets us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.

What is Autoboxing explain in brief?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.


2 Answers

You should look trough your code for any references to Integer, Double etc... Should be easily done with Eclipse or IntelliJ. Do you maybe hold lots of data in Javas build in Maps, Collections etc, with primitive type wrappers that are autoboxed due to generics?

A good way to get rid of this is to use GNU Trove instead of javas built in datastructures: http://trove4j.sourceforge.net/html/overview.html. In provides Maps and Collections that use primitive types instead of generics and reduces memory usage.

Another culprit for creating a lot of object instances could be usage of the InvocationHandler interface. This thing creates an array of Objects for in parameters to methods each time a method is invoked. Proxy objects often make use of this. Do you maybe have third party libs or own code that does this?

Lastly, use a profiler that can show object instances hierarchically. I think VisualVM does this, not sure.

You shouldn't have to resort to C++ as suggested by some. You can design around it.

like image 90
Sason Ohanian Avatar answered Oct 07 '22 01:10

Sason Ohanian


It should be possible to write a simple javaagent that adds logging for valueOf calls (Integer.valueOf) All you need for this task is byte code manipulation library such as Javassist

like image 26
Sami Korhonen Avatar answered Oct 07 '22 01:10

Sami Korhonen