4

I use joda time in my code. When I add this line of code, the using memory will be increased 30M!

LocalDateTime _currDate = new LocalDateTime();

I found that nearly all the java programmers recommend to use joda time. so I think maybe I didn't use it in a right way. Anyone knows the reason, please help me. Thank you very much!

Vigor
  • 1,706
  • 3
  • 26
  • 47
  • Are you looking at the memory that the JVM uses within the operating system, or the memory usage within the JVM? In other words: are you looking at `top` or Task Manager, or are you looking at VisualVM? – Barend May 01 '13 at 14:08
  • @Barend I use Task Manager to monitor the memory usage. The weird thing is: when I add this line of code, my app crash. I found the reason of crash is the memory usage reached to the limit level. When I comment this line of code, my app works well. And I found the memory usage decreased by 30M in the task manager. – Vigor May 01 '13 at 14:27

3 Answers3

5

The JVM is pretty sophisticated and just because you instantiate a new object doesn't mean it will consume memory at that line of code. In some cases the JVM will never allocate memory to un-used objects EVEN if you explicitly instantiate them. You'll want to use a memory profiler to see what is exactly going on. Joda is a well built library and it's highly unlikely that this one class instantiation is causing your memory to increase by 30m.

A good profiler will show you how much memory that class is consuming, what's going on in the heap, along with several other useful insights. It could just be the JVM making more room on the heap to account for other things happening in your code.

I'm sure others will chime in on some good profilers but some IDE's will have them built right in. Start there and good luck.

Michael J. Lee
  • 12,278
  • 3
  • 23
  • 39
  • Thank you for the answer. I use Task Manager to monitor the memory usage. The weird thing is: when I add this line of code, my app crash. I found the reason of crash is the memory usage reached to the limit level. When I comment this line of code, my app works well. And I found the memory usage decreased by 30M in the task manager. Do you have any idea about this? – Vigor May 01 '13 at 14:32
  • The Task Manager is not a good indicator. Use a memory profiler. As for the app crashing you'll need to post the error as it may be un-related to a memory issue. – Michael J. Lee May 01 '13 at 15:12
2

I have found the reason of memory consuming for joda time. This is the answer: Android Java - Joda Date is slow according plowman's solution, I solved this problem.

Community
  • 1
  • 1
Vigor
  • 1,706
  • 3
  • 26
  • 47
0

tl;dr

java.time supplants Joda-Time , with a back-port to Android.

java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

Now in maintenance mode, the Joda-Time project also advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

You may see a different memory usage profile with this library, though I have not tested it.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154