Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  is Enum.values() a real memory hog ?  (Read 801 times)
0 Members and 1 Guest are viewing this topic.
Offline javazoid

Junior Devvie




Where's Flender?


« Posted 2018-08-02 08:46:34 »

According to this article, https://www.javacodegeeks.com/2018/08/memory-hogging-enum-values-method.html

"A recent post on the OpenJDK compiler-dev mailing list titled “about Enum.values() memory allocation” observes that “Enum.values() allocates a significant amount of memory when called in a tight loop as it clones the constant values array.” The poster of that message adds that this “is probably for immutability” and states, “I can understand that.” This message also references a March 2012 message and associated thread on this same mailing list."

I've seen many inner loops in graphics/games engines using the Enum.values() call. I just suspect we should avoid such calls. It seems that escape analisys has little to do here.

Offline princec

« JGO Spiffy Duke »


Medals: 1061
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2018-08-02 09:41:37 »

I'd imagine escape analysis has lots to do here, myself, as using something in a loop in this way is a pretty sure-fire EA friendly pattern.
Nonetheless... avoiding allocation unfortunately still remains a performance issue in Java. The problem being that the moment you turn your gaze on it using a profiler all of the clever optimisation that might have made it "go away" is removed and it looks terrible. The only way to really see what it's doing is to look at the output disassembly.

Cas Smiley

Offline javazoid

Junior Devvie




Where's Flender?


« Reply #2 - Posted 2018-08-02 09:58:59 »

Eh, caching values in the enum may be of help in some cases, as the article says.

i.e. in the enum

public Enum TYPE
{
      ...
      
          private static final TYPE[] _values  TYPE.values();

      public static TYPE[] _values()
      {
         return _values;
      }
}

And the use for-loops with the TYPE._values();

Still readable. but using a static, dangerous, not-immutable array

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

« JGO Spiffy Duke »


Medals: 1061
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2018-08-02 10:47:27 »

You have to be particularly scared of yourself if you're worried about writing into your own enum arrays... I'd say you should only really worry about that sort of thing if you're making public API code. If you're making some game or other... it's your code, just remember not to be a dick Smiley

Cas Smiley

Offline javazoid

Junior Devvie




Where's Flender?


« Reply #4 - Posted 2018-08-02 10:53:04 »

not always scared enough Smiley

Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (360 views)
2018-11-16 13:46:03

hadezbladez (194 views)
2018-11-16 13:41:33

hadezbladez (363 views)
2018-11-16 13:35:35

hadezbladez (91 views)
2018-11-16 13:32:03

EgonOlsen (2193 views)
2018-06-10 19:43:48

EgonOlsen (2228 views)
2018-06-10 19:43:44

EgonOlsen (1385 views)
2018-06-10 19:43:20

DesertCoockie (2023 views)
2018-05-13 18:23:11

nelsongames (1677 views)
2018-04-24 18:15:36

nelsongames (2313 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!