lynsrz's blog

By lynsrz, history, 7 years ago, In English

Hello guys , I want to know how do you optimize the runtime memory of a java program? I solved a problem and it took 20100KB , while memory it took for some programmer's code was 0KB .

Also, Is there anyway I could determine the memory usage and time in IntelliJIdea? Thanks in advance :)

  • Vote: I like it
  • +11
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Optimizing the memory requires a good knowledge of the problem. Maybe there is a better way to optimize for a certain problem, but there is no way to do so in general.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

If you have used twice as memory as someone else, while you both implemented the same algorithm and logic, then you can ask about optimizations (for general programming) and how to reduce the amount of memory usage to get to the goal... or better: you have to get worried and consider thinking more on memory optimizations.

Even in this case; it's not in anyways related to Java. As someone else mentioned, you have to work on your solution. (but yes, considering the language structure and specifications)

The small amount of additional memory usage of Java programs may come from OOP, which we do it less in C++ in CP.

If you have used that much memory and someone else used no memory at all, I believe the difference comes from different algorithms being used, O(n) vs O(1) memory for example. But in case you have small difference, you may have used 64-bit integers where 32-bit integers were enough, for example.

»
7 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Why you need optimize, just walk of in 256 MB.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

1) Let the gc do it's work
2) When creating multi-dimensional arrays, say int[i][j], make sure that i <= j.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Do the few extra array references really make a difference?

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It's a good practise to remove references when you don't need an object, especially big one. Also it is natural behavior when you divide your program into functions.

      Usually you can't force GC to collect garbage, so you can't rely on it (although it works fine in most cases)

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    OK. From now on, I will write m[1000][1000] instead of m[1000][200];