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

4

java.rmi.registry

2

12

java.rmi.server

27

62

java.security.

139

236

java.security.acl

6

21

java.security.cert

131

167

java.security.interfaces

22

2

java.security.spec

36

4

java.sql.

633

42

java.text.

212

294

java.util.

280

860

java.util.jar

7

57

java.util.logging

51

98

java.util.prefs

61

88

java.util.regex

9

21

java.util.zip

40

81

javax.accessibility.

58

103

javax.imageio.

87

192

javax.imageio.event

23

1

javax.imageio.metadata

55

105

javax.imageio.plugins

6

18

javax.imageio.spi

31

66

javax.imageio.stream

40

159

javax.naming.

54

158

javax.naming.directory

31

113

javax.naming.event

18

4

javax.naming.ldap

26

19

javax.naming.spi

10

78

javax.print.

44

52

javax.print.attribute

177

193

javax.print.event

20

1

javax.rmi.CORBA

8

51

javax.rmi.

0

7

javax.security.auth

81

114

javax.sound.midi

107

93

javax.sound.sampled

93

106

javax.sql.

111

0

javax.swing.

1098

2645

javax.swing.border

40

42

javax.swing.colorchooser

43

67

javax.swing.event

74

62

javax.swing.filechooser

18

30

javax.swing.plaf

842

2258

javax.swing.table

62

170

javax.swing.text

808

1643

javax.swing.tree

134

285

javax.swing.undo

15

62

javax.transaction.xa

13

0

javax.xml.parsers

36

17

javax.xml.transform

79

22

org.apache.crimson

207

284

org.apache.xalan

649

1032

org.apache.xml

535

860

org.apache.xpath

472

779

org.ietf.jgss

70

35

org.omg.CORBA

360

546

org.omg.CORBA_2_3

1

18

org.omg.CosNaming

31

198

org.omg.DynamicAny

115

555

org.omg.IOP

19

89

org.omg.Messaging

1

5

org.omg.PortableInterceptor

68

31

org.omg.PortableServer

80

131

org.omg.stub

0

1

org.w3c.dom

976

90

org.xml.sax

98

176

sun.applet.

27

119

sun.applet.resources

0

10

sun.audio.

9

14

sun.awt.

188

220

sun.awt.color

15

14

sun.awt.datatransfer

20

71

sun.awt.dnd

22

17

sun.awt.font

140

161

sun.awt.geom

173

58

sun.awt.im

22

53

sun.awt.image

191

362

sun.awt.print

69

79

sun.awt.resources

10

0

sun.awt.shell

15

38

sun.beans.editors

14

32

sun.beans.infos

0

1

sun.dc.path

7

5

sun.dc.pr

69

18

sun.io.

92

40

sun.java2d.

76

143

sun.java2d.loops

82

118

sun.java2d.pipe

107

164

sun.misc.

203

227

sun.misc.resources

10

0

sun.net.

12

30

sun.net.dns

6

6

sun.net.ftp

0

30

sun.net.smtp

1

8

sun.net.spi

5

0

sun.net.www

87

241

sun.nio.ch

115

243

sun.nio.cs

58

97

sun.print.

141

393

sun.print.resources

10

0

sun.reflect.

83

454

sun.rmi.log

10

14

sun.rmi.registry

1

16

sun.rmi.rmic

166

289

sun.rmi.runtime

11

25

sun.rmi.server

13

108

sun.rmi.transport

54

158

sun.security.acl

16

24

sun.security.action

0

6

sun.security.jgss

162

152

sun.security.krb5

222

291

sun.security.pkcs

28

63

sun.security.provider

82

211

sun.security.tools

13

35

sun.security.util

80

139

sun.security.validator

7

7

sun.security.x509

135

540

sun.text.

42

61

sun.text.resources

1

158

sun.tools.asm

12

37

sun.tools.jar

17

33

sun.tools.java

166

279

sun.tools.javac

17

67

sun.tools.javap

51

97

sun.tools.native2ascii

2

7

sun.tools.serialver

0

8

sun.tools.tree

105

381

sun.tools.util

3

1

sun.util.

1

5

sun.util.calendar

24

19

sun.util.logging

10

0

 



[1] For the curious, the analysis grinds for a long time, but not as long as I?d expected. It takes many minutes, but less than an hour.