Non-Allocating Methods

Draft 1 for Discussion

Peter Dibble
September 2004

RTSJ threads executing out of heap context need to use library classes cautiously.  Any objects allocated by methods in these classes will come from the current allocation context. This is not necessarily a ?bad thing,? but unexpected object allocation is a memory leak and can lead to out of memory errors.

In many cases you can tell that a method does no object allocation by inspection.  If a method does not contain the new operator, does not concatenate strings, and does not invoke any other methods, it is a non-allocating method.  If it invokes other methods, the only practical approaches are to assume it is an allocating method or write a tool[1] to traverse all possible call trees for that method.

Since Java is a dynamic language, the tool has to see all the classes that will be in the execution environment, but as an experiment I assumed that the J2SE 1.4.2 classes.jar is the entire execution environment.  This class library includes 66945 non-abstract public or protected methods of which 27295 don?t allocate objects and either invoke no methods or only invoke other methods that cannot allocate.

The following table summarizes the results of this analysis broken down by the main Java packages.

 

Package

Non-Allocating Count

Allocating Count

Package

Non-Allocating Count

Allocating Count

com.sun.accessibility

11

0

com.sun.corba

1357

2630

com.sun.image

31

42

com.sun.imageio

65

155

com.sun.java

768

2309

com.sun.javadoc

28

102

com.sun.jdi

181

212

com.sun.jndi

183

839

com.sun.media

277

295

com.sun.naming

4

20

com.sun.org

45

177

com.sun.rmi

8

8

com.sun.security

46

92

com.sun.swing

20

0

com.sun.tools

1171

3611

java.applet.

21

24

java.awt.

827

1191

java.awt.color

9

35

java.awt.datatransfer

20

76

java.awt.dnd

85

118

java.awt.event

96

57

java.awt.font

109

120

java.awt.geom

324

201

java.awt.im

35

13

java.awt.image

293

447

java.awt.peer

144

0

java.awt.print

33

26

java.beans.

133

125

java.beans.beancontext

44

109

java.beans.

2

24

java.io.

200

499

java.lang.

319

341

java.lang.ref

9

5

java.lang.reflect

63

41

java.math.

17

57

java.net.

218

295

java.nio.

325

821

java.nio.channels

101

58

java.nio.charset

51

40

java.rmi.

3

10

java.rmi.activation

20

40

java.rmi.dgc

5